0

I am building a simple program with 3 Forms. The first Form has 2 Buttons and the other 2 are blank. I used this code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button2.Click +=new EventHandler(button2_Click);
            button1.Click +=new EventHandler(button1_Click);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form3 a = new Form3();
            a.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 b = new Form2();
            b.Show();
        }
    }

When Button 1 is clicked, Form 2 should open. And when Button 3 is clicked Form 3 should Open. Although the forms open when the button is clicked, but when they open it opens 2 identical Form 2 or Form 3 windows. I cannot seem to find the problem. Is the issue in the way I tell my Forms to open?

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by jmurph333
1

It sounds like you've added the event handler twice. Try removing lines 6 and 7 and see what happens. I suspect you'll see it open just one form.

Most likely you created the event handlers in Visual Studio, which automatically adds them to the button events. You can check this by looking in the file Form1.Designer.cs and expanding the " Windows Form Designer generated code" region. You'll find all the controls and event handlers that the system creates. You should find lines that look just like your lines 6 and 7.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.