Bodokh
Bodokh

Reputation: 1076

How to fire a formload

If i wanted to do a button click i would write:

this.button1.PerformClick();

Let me rephrase my question, I want to create a button that will reload the whole form when clicked, it is possible to close and open the form?

Upvotes: 0

Views: 74

Answers (3)

Josh Anderson
Josh Anderson

Reputation: 438

Assuming you have something like this:

MyForm_Load(object sender, EventArgs e)
{
 //code to load/reload form goes here
}

You can make a method like this:

private void LoadData()
{
 //code to load/reload form goes here
}

And then you can call the same method from both events like so:

MyForm_Load(object sender, EventArgs e)
{
 LoadData()
}
button1_Click(object sender, EventArgs e)
{
 LoadData()
}

It's a bit cleaner than having one event call another.

Upvotes: 1

Ali Alavi
Ali Alavi

Reputation: 2467

var myFrom = new Form1();
myForm.Show();

If you just want to call the code in Form_Load for the current form, you can just call it (as you do with other functions), since Form_Load is only fired once for each new form.

Upvotes: 2

Szymon
Szymon

Reputation: 43023

You have your answer on MSDN:

Form.Load Event

Occurs before a form is displayed for the first time.

So you need to show your form.

But I guess your intention is to execute the code that you have in a method handling Form.Load event. In this case, it would be better to extract that code into a separate method and call that method from both Form.Load event handler method and your button Click event handler method.

Upvotes: 2

Related Questions