Click here to Skip to main content
15,886,422 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionHaving a program create its own variables? [modified] Pin
TabascoSauce14-Mar-09 21:33
TabascoSauce14-Mar-09 21:33 
AnswerRe: Having a program create its own variables? Pin
N a v a n e e t h15-Mar-09 5:41
N a v a n e e t h15-Mar-09 5:41 
GeneralRe: Having a program create its own variables? [modified] Pin
TabascoSauce15-Mar-09 11:36
TabascoSauce15-Mar-09 11:36 
QuestionUpdating form controls outside of form file Pin
Lavake14-Mar-09 11:14
Lavake14-Mar-09 11:14 
AnswerRe: Updating form controls outside of form file Pin
N a v a n e e t h14-Mar-09 16:28
N a v a n e e t h14-Mar-09 16:28 
GeneralRe: Updating form controls outside of form file Pin
Lavake15-Mar-09 4:06
Lavake15-Mar-09 4:06 
GeneralRe: Updating form controls outside of form file Pin
N a v a n e e t h15-Mar-09 5:21
N a v a n e e t h15-Mar-09 5:21 
QuestionProblem to communicate between forms [modified] Pin
roshihans14-Mar-09 3:44
roshihans14-Mar-09 3:44 
Hi,

I have a problem to pass some data between forms. Let's say I have 2 form, FormMain and FormChild, FormChild will be called from FormMain. What I want to do is simple. Whenever I change the textBox1's text in FormChild, the textBox1's text in FormMain will also contain the same text. After googling for a while, I got 2 solution : by using a delegate or directly instantiate FormMain in FormChild. Both solution unfortunately is in C#, so I need to convert it first. Well, I thought at first that there will be no problem at all to convert it, but that is just plain wrong. Both of the solution I get is just give me some error. Here is the converted source code

1.using delegate

in Form Main.h :

#include "Form Child.h"

namespace Delegates {


	public delegate void AddTextChangeDelegate(String^ item);
	
	public ref class FormMain : System::Windows::Forms::Form
	{
	public:
		FormMain()
		{
			formChild = gcnew FormChild();
		}

	// declare as global variable	
	private:
		FormChild^ formChild;

	private: 
		System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
		{			
			formChild->AddTextChangeCallback = gcnew AddTextChangeDelegate(&this->AddTextChangeCallbackFn);
			formChild->Show();
		}

	private:
		System::Void AddTextChangeCallbackFn(String^ str)
		{
			textBox1->Text = str;
		}
	};
}


in Form Child.h

namespace Delegates {
	
	public ref class FormChild : System::Windows::Forms::Form
	{
	// declaring delegate, and get "error C2143: syntax error : missing ';' before '^' "
	// the compiler seems doesn't recognize this
	public:
		AddTextChangeDelegate^ AddTextChangeCallback;
	
	// user click add button and the delegate executed
	private:
		System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
		{
			AddTextChangeCallback(textBox1->Text );
		}
	
	};
}


2. directly instantiate FormMain

the solution I get (in C#) is just simply declare FormMain as a global variable in FormChild, then exposed the textBox in FormMain as a public. But when I do the same thing in C++, the compiler give an error "error C2143: syntax error : missing ';' before '^' ". so probably I just need to include the Form Main.h in Form Child.h, which give me another error "fatal error C1014: too many include files : depth = 1024" (is it because both form include each other's header?, so that is like making some cyclic dependency thing? I'm not sure why). After googling once more, I found that it is still "possible" to declare FormMain instance in Form Child.h. Include Form Main.h in the Form Child.cpp and that's it.

in Form Child.cpp

#include "StdAfx.h"
#include "Form Child.h"
#include "Form Main.h"

System::Void FormChild::btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
{
	
	FormMain^ formMain = gcnew FormMain();
	formMain->textBox1->Text = this->textBox1->Text;
	// I also have change it to this but the FormMain textBox1's text doesn't change
	// FormMain formMain;
	// formMain.textBox1->Text = this->textBox1->Text;
	// this will give memory access error in the runtime
	// FormMain^ formMain;
	// formMain->textBox1->Text = this->textBox1->Text;
}


But then another problem comes out. The instance declared in Form Child.cpp is not the same instance as the previous FormMain. So, what is the correct way to solve this problem? Is the methods mentioned above was wrong? Or perhaps I make a wrong conversion, misplaced the delegate or something? I have search the other solutions in the internet, but a lot of it is either in C# or VB. I could not find it for C++ .NET.
Also is there any way around to access the instance of FormMain from FormChild in the above case?

Thanks
AnswerRe: Problem to communicate between forms Pin
N a v a n e e t h14-Mar-09 6:00
N a v a n e e t h14-Mar-09 6:00 
GeneralRe: Problem to communicate between forms Pin
roshihans14-Mar-09 16:08
roshihans14-Mar-09 16:08 
GeneralRe: Problem to communicate between forms Pin
N a v a n e e t h14-Mar-09 16:21
N a v a n e e t h14-Mar-09 16:21 
GeneralRe: Problem to communicate between forms Pin
roshihans14-Mar-09 17:33
roshihans14-Mar-09 17:33 
GeneralRe: Problem to communicate between forms Pin
N a v a n e e t h15-Mar-09 5:14
N a v a n e e t h15-Mar-09 5:14 
GeneralRe: Problem to communicate between forms Pin
roshihans15-Mar-09 18:12
roshihans15-Mar-09 18:12 
QuestionDelegates for instance methods between AppDomains... Pin
MrBhbk11-Mar-09 10:28
MrBhbk11-Mar-09 10:28 
QuestionSingle Textbox EventHandler for all Pin
Badboy22TR9-Mar-09 14:23
Badboy22TR9-Mar-09 14:23 
AnswerRe: Single Textbox EventHandler for all Pin
ky_rerun10-Mar-09 5:00
ky_rerun10-Mar-09 5:00 
AnswerRe: Single Textbox EventHandler for all Pin
Badboy22TR10-Mar-09 12:07
Badboy22TR10-Mar-09 12:07 
QuestionUpdating a label's text automatically? (New programmer) Pin
TabascoSauce9-Mar-09 12:50
TabascoSauce9-Mar-09 12:50 
GeneralRe: Updating a label's text automatically? (New programmer) Pin
Luc Pattyn9-Mar-09 13:09
sitebuilderLuc Pattyn9-Mar-09 13:09 
GeneralRe: Updating a label's text automatically? (New programmer) Pin
TabascoSauce9-Mar-09 14:10
TabascoSauce9-Mar-09 14:10 
QuestionInt64 minus.... Pin
Thilek8-Mar-09 6:46
Thilek8-Mar-09 6:46 
AnswerRe: Int64 minus.... Pin
Mark Salsbery8-Mar-09 9:19
Mark Salsbery8-Mar-09 9:19 
AnswerRe: Int64 minus.... Pin
Dave Doknjas8-Mar-09 13:56
Dave Doknjas8-Mar-09 13:56 
Question(C++/CLI) "FileNotFoundException" raised in "Activator::GetObject()" method of Remoting programming Pin
zkii4-Mar-09 3:08
zkii4-Mar-09 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.