I have a solution in Visual Studio 2013 which contains two Forms. I want when a button pressing in Form2, the variable flag_fb
is updated and I use its value in Form1. Is there any way to do this? Thanks.
Asked
Active
Viewed 1,781 times
1

IndustProg
- 627
- 1
- 13
- 33
-
What do you mean with "variable `flag_fb`"? Is it a member of Form1? – helb Jan 15 '15 at 07:02
-
Show us you code. What did you try? – Carlos Landeras Jan 15 '15 at 07:02
-
yes, flag_fb is a member of Form1. But it does not matter. I want to update a variable such as flag_fb when pressing a button in Form2 and use its updated value in Form1. – IndustProg Jan 15 '15 at 07:12
-
when it is raining a flag in form1 is set. when it is set Form2 opens to alarm the user weather status. I want by pressing a button in Form2 flag_fb chenge to inform me user knows the weather status. – IndustProg Jan 15 '15 at 07:43
-
1You could use a public static variable/property or use a separate application wide static class entirely to hold those variables,and then update the value of from anywhere. – MrClan Jan 15 '15 at 09:04
-
1possible duplicate of [How do you pass an object from form1 to form2 and back to form1?](http://stackoverflow.com/questions/4887820/how-do-you-pass-an-object-from-form1-to-form2-and-back-to-form1) – Joe Jan 15 '15 at 10:48
2 Answers
2
Method1 : using parameterized constructor to pass the variables between forms
create a parameterized constructor for Form1
and call the Form1 parameterized constructor from Form2 :
//form1 code
bool flag_fb =false;
public Form(bool flag_fb)
{
this.flag_fb = flag_fb;
}
call the Form1
parameterized constructor from Form2
as below:
//form2 code
Form1 form1=new Form1(flag_fb);
from1.Show();
Method2 : create your variable flag_fb
as public static
variable in Form2
so that it willbe accessible from Form1
aswell.
//Form2 code
public static bool flag_fb = true;
To access the flag_fb variable
from Form1
just use className as below:
//Form1 code
bool form2flagValue = Form2.flag_fb ;

Sudhakar Tillapudi
- 25,935
- 5
- 37
- 67
1
Something like this should also work.
// Open form2 from form1
using (Form2 form2 = new Form2())
{
if (form2.ShowDialog() == DialogResult.OK)
{
m_myVal = form2.flag_fb;
}
}
You should make sure flag_fb
is public member variable of Form2
, and
also make sure it is set to desired value when user clicks OK for instance.

Giorgi Moniava
- 27,046
- 9
- 53
- 90