Seeking for Volunteer C++ Windows Form Application Tutor

Currently, I am doing small application for my Master Degree project, I have done small part on my program using Win32 C++ Console, But now I need to migrate my code into Windows Form Application (GUI) which has to be written in C++ as well so that it look more user friendly.
I am using Microsoft Visual Studio 2008 with .NET Framework 2.0. OpenCV 1.1 as additional external library.
Currently, I face trouble with the coding. I am not so sure how I can integrate my program with GUI or Windows Form Applications. When looking into some example code, it seem that I have to add in Thread Class. What is that and how i can use it? "Delegate" what is all this. I never learn all this during my Bachelor course.

I tried this simple program like if we click the button2, the "Title" Text will change to "ABC". But when I try to build with the code below, it crashed.
Crash Error Message :
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
Anyone could help me?

Currently, I am doing small application for my Master Degree project, I have done small part on my program using Win32 C++ Console, But now I need to migrate my code into Windows Form Application (GUI) which has to be written in C++ as well so that it look more user friendly.
I am using Microsoft Visual Studio 2008 with .NET Framework 2.0. OpenCV 1.1 as additional external library.
Currently, I face trouble with the coding. I am not so sure how I can integrate my program with GUI or Windows Form Applications. When looking into some example code, it seem that I have to add in Thread Class. What is that and how i can use it? "Delegate" what is all this. I never learn all this during my Bachelor course.

I tried this simple program like if we click the button2, the "Title" Text will change to "ABC". But when I try to build with the code below, it crashed.
#pragma once
#include "highgui.h"
#include "cv.h"
#include "numExpression.h"
#define NUM "Global"
namespace TestApp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
///
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
///
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
///
/// Clean up any resources being used.
///
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Threading::Thread^ trd;
protected:
private:
///
/// Required designer variable.
///
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(255, 37);
this->label1->Name = L"label1";
this->label1->RightToLeft = System::Windows::Forms::RightToLeft::No;
this->label1->Size = System::Drawing::Size(27, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Title";
this->label1->TextAlign = System::Drawing::ContentAlignment::TopCenter;
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 282);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(156, 65);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(191, 20);
this->textBox1->TabIndex = 2;
//
// button2
//
this->button2->Location = System::Drawing::Point(272, 282);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 3;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(359, 317);
this->Controls->Add(this->button2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"TestApp";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: void testF()
{
/* for(int i = 0; i < 10; i++)
MessageBox::Show("Loop number " + i);*/
label1->Text = "ABC";
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
trd = gcnew System::Threading::Thread (gcnew System::Threading::ThreadStart (this, &Form1::testF));
trd->Start();
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
Crash Error Message :
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
Anyone could help me?
Last edited by Ch1n^anN on Thu Aug 05, 2010 8:38 pm; edited 4 times in total










