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?

Dani AI

Generated

The duplicate windows mean the same Click handler was being invoked twice (designer + manual wiring is the usual culprit). pointed this out and the quick fix is to remove the duplicate subscription so each button click only creates one form.

Quick checks and fixes:

  • In the Designer, select the button -> Properties -> Events (lightning bolt). If the Click entry already contains your handler name, clear it if you wire the event in code, or remove your extra += in code if you prefer the designer to wire it.
  • If you prefer code-only wiring, a safe guard is to unsubscribe before subscribing; -= is safe if the handler wasn't attached:
button1.Click -= button1_Click;
button1.Click += button1_Click;

Patterns to avoid the same runtime problem and improve UX:

  • Reuse a single instance instead of creating new windows every click:
private Form2 _form2;

private void button1_Click(object sender, EventArgs e)
{
    if (_form2 == null || _form2.IsDisposed)
        _form2 = new Form2();

    _form2.Show();
    _form2.BringToFront();
}
  • Or search existing open forms and bring one to front:
using System.Linq;

var existing = Application.OpenForms.OfType<Form2>().FirstOrDefault();
if (existing != null) existing.BringToFront();
else new Form2().Show();

Extra tips: avoid editing Designer.cs casually—use the Properties window or back up the file first. For modal work use ShowDialog() (it blocks until closed). To prevent accidental re-openings, consider disabling the button while the child form is open and re-enable it on the child form’s FormClosed event. These small patterns reduce bugs and make intent clear for future maintenance.

Recommended Answers

All 2 Replies

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

It works great. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.