[Solved] c# prevent duplicating forms [closed]


This should work (haven’t tested it though)

    public static bool _Invoked;
    Form2 f2 = new Form2();

    private void button1_Click(object sender, EventArgs e)
    {
        if (!_Invoked)
        {
            _Invoked = true;
            f2.Show();
        }
        else if (_Invoked)
        {
            f2.BringToFront();
            _Invoked = false;
        }
    }

Add a comment for further clarification

EDIT:

Just tested this and its working

    Form2 f2 = new Form2();
    bool _Clickone = false;
    private void button1_Click(object sender, EventArgs e)
    {
        if (!_Clickone)
        {
            _Clickone = true;
            f2.Show();
        }
        else
        {
            f2.WindowState = FormWindowState.Normal;
            f2.ShowInTaskbar = true;
            f2.BringToFront();
        }
    }

Then handle the Form Closing event of the second for m

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
    }

3

solved c# prevent duplicating forms [closed]