SolvedHow to pass variables to Windows Forms

Posts 1–15 of 20 · Page 1 of 2
How to pass variables to Windows Forms
Hi guys,
i am trying to send the contents of a variable to a windows form.
currently i am exploring the label1 as possible way but so far no luck.

any ideas?
thanks in advance.
Add a parameter to the Form Constructor.

If C++ (C++ section):
Code:
Form1(String ^text)
{
...
}
ON call:

Code:
... = gcnew Form1("balls");
This is one of many ways, not the best I guess, and kinda limited, but does the job if you need something simple
I don't know what you are talking about. You need to be much more descriptive.

You can post message to a specified window's callback routine by using the PostMessage API. That is all I can give you without more description.

If you want to share memory, you can use a named pipe with some synchronization objects as well.

Mind you, Brinuz might've interpreted your question correctly and you should try his advice.
Yeah sorry guys not enough info.

basically i have some code which includes a lot of IF statements. based on the results of the IF statements i want to send some text to a windows form label
i have tried brinuz's method but so far haven't had much luck but that could be down to my lack of knowledge.

if the above isn't possible in c++ i am more than willing to port my existing code to c# so if its easier in c# then no problems.

Quote Originally Posted by sakinoakura View Post
Yeah sorry guys not enough info.

basically i have some code which includes a lot of IF statements. based on the results of the IF statements i want to send some text to a windows form label
i have tried brinuz's method but so far haven't had much luck but that could be down to my lack of knowledge.

if the above isn't possible in c++ i am more than willing to port my existing code to c# so if its easier in c# then no problems.

It is the same in C++ / C#, was just wondering because of the code I typed.

Let me ask you this, is the 2nd form already created when you change the value? Or do you want to create a 2nd form with the value from the main form?
Kinda confused here.
Quote Originally Posted by 'Bruno View Post


It is the same in C++ / C#, was just wondering because of the code I typed.

Let me ask you this, is the 2nd form already created when you change the value? Or do you want to create a 2nd form with the value from the main form?
Kinda confused here.
to be honest Brinuz I am totally new to visual c++ with a very limited knowledge of c++ so the example you gave may well work.

the code snippet you provided was shortened a little and of course with my limited knowledge all i could do was try (in this case fail) to fill in the missing gaps.

anyway thanks for getting back its appreciated.

if you could provide a full code example it would be highly appreciated.
sorry to sound such a noob
So pretty much all you have to do is create a 2nd form (I guess you already did that.. duh )

On the second form Contructor (The function with the same name of the form), you could change it from (for example):
Code:
		Form2(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
TO:

Code:
		Form2(String ^text)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			this->label1->Text = text; //Parameter
		}
You see that it has parameters(^text) now, that in this case is a string, and that after the initialization of Form2, the label that it has in it (label1) will have its text changed to the parameter (^text) string/text.

So now, whenever you create/open that form, it will need that parameter for the label.

example on a button click from Form1 (Main form):

Code:
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 Form2 ^newForm = gcnew Form2(this->textBox1->Text);
				 newForm->ShowDialog();
			 }
As you see, on the button click (button1), im creating a new form of the type Form2 (The form that has the code above), with the paremeter (...) with the text of a random textbox.
(which will then be applied to the label after initializing)
So pretty much all you have to do is create a 2nd form (I guess you already did that.. duh )
hehe i had no idea a second form was required
thanks for getting back with a solid example. there's no way any amount of experimenting would have taken me down that path.

i am still experimenting will get back asap.
You should get a C++ book... Seems like you don't know the basics of it...
Quote Originally Posted by MarkHC View Post
You should get a C++ book... Seems like you don't know the basics of it...

actually i've been reading up and watching theNewBoston tutorials.
but there's a world of difference between coding c++ console apps and making the transition into gui/net framework based stuff.
That's right that happened to me too. I solved the problem by also learning theNewBoston C# tutorials. You can mix the two languages together like if you create a DLL made from C++ and include it to C# project.
I just realized something, you are trying just to copy the content of a variable to a simple control within your form... Right?? For some reason I thought you wanted just to get something from another different form.

So all you have to do is:

Code:
String ^hp = "50";
label1.text = hp;
or

Code:
String ^btnText = "Click Me!";
button1.text = btnText;
Quote Originally Posted by 'Bruno View Post
I just realized something, you are trying just to copy the content of a variable to a simple control within your form... Right?? For some reason I thought you wanted just to get something from another different form.

So all you have to do is:

Code:

String ^hp = "50";
label1.text = hp;
or

Code:
String ^btnText = "Click Me!";
button1.text = btnText;
yeah right Brinuz thats's exactly what i want.
i have tried your new method ...was really excited thinking how easy it was
tried building ....yep you guessed it
i have 2 errors now


Error 1 error C2065: 'label1' : undeclared identifier
Error 2 error C2228: left of '.text' must have class/struct/union

so close but yet so far.
well you obviously need to have the control on the form.. the label.. (like the picture you posted of the form)

beware of the name that you gave it. (default name is label1.. etc)
Thanks for the help Brinuz,
i can now open one form from another and send messages between boxes, click a button to say "Hello World" have a click counter on any form, create a time based progress bar and much much more.
Sadly for now sending a simple text message onto the form dynamically (no user clicky clicky control) just aint happening.

I do appreciate your efforts, i came away from this with more knowledge than i came into it,
cheers.
Posts 1–15 of 20 · Page 1 of 2

Post a Reply

Similar Threads

Talk with us