Hello,

I am opening a new Form in my application and want to pass along a variable to Form2.

I think I am pass along it correctly but are not sure how to receive it in Form2.

How will that be possible ?

String^ ForwardVariable;
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
		ForwardVariable= "dummyString";

		Form2^ form2 = gcnew Form2;
		form2->Show(ForwardVariable);
}

Here is how I did it in MFC (have not used managed CLR but it might be the same). In Form2 create a public variable that holds whatever you want to pass it, then in Form1, just after gcnew line, set the value of the variable. Once Form2 starts it can move the value of that variable anywhere it wants, such as in edit box or somewhere else.

Thank you..

I did try your idéa but did not get it to work.
I did create a public String in Form2 and then tried to call that public string after gcnew just before opening the form but I get the compile error that ForwardVariable isn´t declared in Form1.

I am not sure if Form1 cant call that public variable or if there could be something else missing.

//In Form2
public: String^ ForwardVariable;

//In Form1
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{
    Form2^ form2 = gcnew Form2;
    ForwardVariable= "dummyString";
    form2->Show();
}

Here is how I did it in MFC (have not used managed CLR but it might be the same). In Form2 create a public variable that holds whatever you want to pass it, then in Form1, just after gcnew line, set the value of the variable. Once Form2 starts it can move the value of that variable anywhere it wants, such as in edit box or somewhere else.

Probably should be form2->ForwardVariable= "dummyString";

Yes, that did compile when I changed it like that but when trying to get the public variable in the Load_Event in Form2, this MessageBox shows an emtpy string.
It should show: "dummyString", I beleive I still am missing some detail out.

What I do is to first press button1 to set the variable and then pressing button2 to open the form.

(In Form2)
public: String^ ForwardString; 
private: System::Void Form2_Load(System::Object^  sender, System::EventArgs^  e) 
{
		 MessageBox::Show(ForwardString);
}


(In Form1)
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
		 Form2^ form2 = gcnew Form2;
		 form2->ForwardString = "dummyString";
}

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
		 Form2^ form2 = gcnew Form2;
		 form2->Show();
}

I did set a Global String variable in Form1 for button1 and then when pressing button 2, I took that String variable and put it to the public forwardVariable and then it worked great.

Thank you for help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.