public partial class Form1 : Form
{ bool b=false;
private void button1_Click(object sender, EventArgs e)
{
b=true;
}
private void Form1_Load(object sender, EventArgs e)
{
if(b)
{
MessageBox.Show("hello");
}
}
}
What i mean is the if
statement in Form1
doesn't know that the boolean b
is true when i click on the button1
.
b
stays false in Form1
because i declared it as that in the beginning before the events.
So how can i make Form1
here know that b
is true when i click on button1
?
Like how to connect/relate events?
In general how can i make the statements in an event know what happens in other events like here?
CodePudding user response:
The Load
event of a form is the first event to be raised in the form. And it is raised only once in each forms life time.
When a form is loaded, the stuff you declare between events and methods (in the body of your form) are created, then the constructor of the form is invoked (the one with InitializeComponent();
in it), and finally, the loaded event (if there is one) is raised.
Now, the default value of a Boolean
variable is false
(you've even set it to false manualy), so the button1_Click
event which sets your boolean's value to true, will never be raised before the form's loaded event
Ergo what you want can't be achieved, unless, you store the boolean value to the database after each time the button is clicked, and read it from the database in the form upon loading.
I'd appreciate it if you accepted the answer, in case it helped clear things up.