Multiple Forms with .NET
How do I create two forms with a Button on each, such that when I click the
button on the first Form the event will display the second Form. Now when
I click the Button on the second Form the event will display the first Form.
I tried the following with no sucses:
1- Created Form1.
2- From the Project\Add menue added Form2
Form2 formtwo = new Form2();
Form1 formone = new Form1();
I GET AN ERROR: STACK OVER FLOW.
Re: Multiple Forms with .NET
You need to declare private variables in each form as type Form1 and Form2,
and link an eventhandler from Form1 to Form2.
//I assume you added button1 to Form1 in the designer
private Form2 form;
//Instantiate the 'form' variable, and pass it a reference to Form1
private void Form1_Load(object sender, System.EventArgs e)
form = new Form2(this);
//Add a new public event
public event EventHandler ButtonClicked;
//Fire the evetn when the button is clicked
private void button1_Click(object sender, System.EventArgs e)
//in the designer, add a label (label1)
private Form1 form;
//Create a new constructor in Form2 that takes a Form1 as a parameter
public Form2 (Form1 theForm) :
form = theForm;
//Add a new EventHandler for when the ButtonClicked event fires
//Add an EventHandler to Form2
private void form1_ButtonClicked(object sender, EventArgs e)
label1.Text = "Form1 Clicked";
Now run the pgm, and click Form1's button. Form2's label should change.
"John O." <OTABACHIANO@NIMO> wrote:
> How do I create two forms with a Button on each, such that when I click
>button on the first Form the event will display the second Form. Now when
>I click the Button on the second Form the event will display the first Form.
> I tried the following with no sucses:
> 1- Created Form1.
> 2- From the Project\Add menue added Form2
> Form2 formtwo = new Form2();
> Form1 formone = new Form1();
> I GET AN ERROR: STACK OVER FLOW.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center