How do i pass control to another form in a button click event

Hi I am new to windows forms. How do I pass control to anohter from in a buttons click event. 

eg 

When you click on a button located on form 1. It then shows form 2. 

how do you do this. 

Thanks 


Answer this question

How do i pass control to another form in a button click event

  • Kwen.de.Mike

    In the click event, instantiate the form and then show it.  That will automatically pass control to the new form.

    Example:
    public void HandleClick(object sender, EventArgs e)
    {
       Form2 f = new Form2();
       f.Show();
    }

    Hope this helps.

    -Ari

  • J4m3sB

    OOPS I thought you wanted something else.  I thought you wanted to pass "A" cotrol not control.  :) 
  • Gurunath

    Thanks I have got that to work. But the problem now is that form 1 is still visible when form2 opens. 

    How can I make form2 open in the same location as form1 and form1 dissapear. 

    So eg like what you see in a nomal app setup. Enter data on one form click next and then another form opens so you can enter data in and then so on. 

  • JasonMcC

    Dozens of ways to do this here are two.

    REALLY Easy Way
    Place a button on form 1
    In the click event put the code
    m_Form2.Controls.Add(this.button1);

    Done
    Next

    Place a public method on form 2 something like this

    public void InsertControl(Control c)
    {
    this.Controls.Add(c);
    c.Visible = true;
    }

    then on form 1 place a button.
    On the click event of the button put the code.

    m_Form2.InsertControl(this.button1);


    the button shoots to form 2 and is gone from form 1.  Probably missing some cleanup, but you get the picture.  The second could be useful to send anything to form 2.


  • How do i pass control to another form in a button click event