Search code examples
c#winformsbuttonform-load

Reload a Form through another Form by Button click


I have two forms: Form1 and Form2.

On Form1 I have a button. And through that button I want to reload the Form2. On From2_Load event I've some code which will update the picture on every load.

But I'm unable to accomplish the task. I'm using this code to reload, but failed to do that.

This is my 'On button click' event code:

Form2 frm = new from2();
frm.refresh();

I don't want to hide the Form2. I just want to close it and reopen it on each click.


Solution

  • As you said you have some code in your Form_Load event... Why not do this?

    private void form2_Load()
    {
    Mycodes()///call it if required
    }
    
    public void MyCodes() 
    {
      // Move codes from load event to here
    }
    
    // Now from Form1
    public class form1
    {
     Form2 frm = new Form2() ///make sure u declare it in class level
    private void Bttn_Click()
    {
        frm.MyCodes();
    }