Newbie: How to use a variable from a different form?
-
Hi! I have a textbox where I write my namn and click OK. Then a another form loads up. Then my name should appear in a label. But I cant seem to reach my variable (name) from form1 to use it in form2. Any suggestions?
Newbie untill I die! :-)
-
Hi! I have a textbox where I write my namn and click OK. Then a another form loads up. Then my name should appear in a label. But I cant seem to reach my variable (name) from form1 to use it in form2. Any suggestions?
Newbie untill I die! :-)
Hy, The easiest way to do what you need is to create a public static variable that contains the TextBox's text. I'll give you an example code. Suppose you have Form1 with TextBox1 and Form2 with Label2. When you click Button1 from Form1 you show Form2 with the text on the Label2.
//in the first form
public static string textBoxText = "";private void Button1_Click(object sender, EventArgs e)
{
textBoxText = TextBox1.Text;
Form2 f = new Form2();
f.ShowDialog();
}//in the second form
private void Form_Load(object sender, EventArgs e)
{
Label2.Text = Form1.textBoxText;
}Hope it helps you.
Do your best to be the best
-
Hy, The easiest way to do what you need is to create a public static variable that contains the TextBox's text. I'll give you an example code. Suppose you have Form1 with TextBox1 and Form2 with Label2. When you click Button1 from Form1 you show Form2 with the text on the Label2.
//in the first form
public static string textBoxText = "";private void Button1_Click(object sender, EventArgs e)
{
textBoxText = TextBox1.Text;
Form2 f = new Form2();
f.ShowDialog();
}//in the second form
private void Form_Load(object sender, EventArgs e)
{
Label2.Text = Form1.textBoxText;
}Hope it helps you.
Do your best to be the best
public static string textBoxText = ""; that one was missing, well it was not decleared where you had put it, thanks!
Newbie untill I die! :-)
-
public static string textBoxText = ""; that one was missing, well it was not decleared where you had put it, thanks!
Newbie untill I die! :-)
Why dont you make a seperate class file of the same namespace, then declare all your global variables in that class and then access those variables from any Form of your application... i.e. class myGlobalVars { public static string textBoxContent; .......... .......... } Now access these variables from any Form like this, string getTExtBoxContent = myGlobalVars.textBoxContent;
-
Why dont you make a seperate class file of the same namespace, then declare all your global variables in that class and then access those variables from any Form of your application... i.e. class myGlobalVars { public static string textBoxContent; .......... .......... } Now access these variables from any Form like this, string getTExtBoxContent = myGlobalVars.textBoxContent;
Yes, I have thougth about that...but I only have one variable...so I think its a little to much work for only one.
Newbie untill I die! :-)
-
Yes, I have thougth about that...but I only have one variable...so I think its a little to much work for only one.
Newbie untill I die! :-)
:) still better than spending hours to find out more efficient ways. :):) isn't it ? When I started learning C#, I used to use File.WriteAllText(), File.ReadAllText to communicate between forms and that was the easiest solution for me that time and my application survived without any problem. Anyway, best of luck.