Multiple Forms with .NET


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Multiple Forms with .NET

  1. #1
    John O. Guest

    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

    Class Form1
    Form2 formtwo = new Form2();
    button1_click
    formtwo.Show();
    formtwo.BringToFront();


    Class Form2
    Form1 formone = new Form1();
    button1_click
    formone.Show();
    formone.BringToFront();

    I GET AN ERROR: STACK OVER FLOW.


  2. #2
    Greg Guest

    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.

    Form1
    ------
    //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);
    form.Show();
    form.BringToFront();
    }

    //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)
    {
    if(ButtonClicked!=null)
    ButtonClicked(this, EventArgs.Empty);
    }



    Form2
    ------
    //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) :
    this()
    {
    form = theForm;

    //Add a new EventHandler for when the ButtonClicked event fires
    form.ButtonClicked+=new EventHandler(form1_ButtonClicked);
    }

    //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.


    -Greg



    "John O." <OTABACHIANO@NIMO> wrote:
    >
    > 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
    >
    >Class Form1
    > Form2 formtwo = new Form2();
    > button1_click
    > formtwo.Show();
    > formtwo.BringToFront();
    >
    >
    >Class Form2
    > Form1 formone = new Form1();
    > button1_click
    > formone.Show();
    > formone.BringToFront();
    >
    > I GET AN ERROR: STACK OVER FLOW.
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center