C# switching between forms calls Form_Load

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Olrik
    New Member
    • Jul 2008
    • 2

    C# switching between forms calls Form_Load

    I'm having a problem with c# when switching forms. It seems that when i switch from one form to the other they always call Form_load, which I don't want.
    Im using form.show() and form.hide() so I don't understand why they are calling form_load.

    This is the code:

    from form1

    private void btnModify_Click (object sender, EventArgs e)
    {
    Form mod = new modifyForm();
    mod.Show();
    this.Hide();
    }//end btnModify_Click

    from modifyForm:

    private void btnCancel_Click (object sender, EventArgs e)
    {
    Form toMain = new Form1();
    this.Close();
    toMain.Show();
    }

    I'm just hiding and showing Form1 but it calls Form1_Load each time.
    Can I get around this? I've tried using form.visible=tr ue and form.visible=fa lse but the result is the same.
  • Olrik
    New Member
    • Jul 2008
    • 2

    #2
    I've figured it out.
    Here is the answer for anyone that may have the same problem.

    this is the new code I'm using:


    from Form1:

    private void btnModify_Click (object sender, EventArgs e)
    {
    Form mod = new modifyForm();
    mod.Owner = this;
    mod.Show();
    this.Hide();
    }//end btnModify_Click

    from modifyForm:

    private void btnCancel_Click (object sender, EventArgs e)
    {
    this.Owner.Show ();
    this.Close();

    }

    Worked like a charm

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Yes, well since you were creating a new isntance of the Form everytime, it was to be expected that the form_load() method would be called each time.
      You would probably eventually run out of memory since the one form was only being hidden, and then user used again.

      Comment

      Working...