Windows Develop Bookmark and Share   
 index > Windows Forms General > How do i pass control to another form in a button click event
 

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 
MigrationUser 1  Friday, June 04, 2004 12:11 PM
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.

MigrationUser 1  Friday, June 04, 2004 5:26 PM
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
MigrationUser 1  Friday, June 04, 2004 9:59 PM
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. 
MigrationUser 1  Saturday, June 05, 2004 10:22 AM
OOPS I thought you wanted something else.  I thought you wanted to pass "A" cotrol not control.  :) 
MigrationUser 1  Sunday, June 06, 2004 12:31 AM

You can use google to search for other answers

Custom Search

More Threads

• CancelButton Property closes the Form
• Turning a certain point on form a specified color in MSVC++.NET
• Changing WindowState has unexpected result
• how to get the history of visited webpages from a tabbed webbrowser?
• Debugging script code with Visual Studio in a Windows Forms Webbrowser control
• Property Grid Custom Dropdown not behaving as I expected
• Opeing default web browser to an URL automatically
• Draw only region that needs to be drawn
• how to make a MDI form look like a normal form
• I can't reach a program with using keyboard