-1

if the event happens again and again and the previously opened Form has not been closed yet? Then problem exists because we want to close the previous open form before opening a new form. For that i made a function

    frmItem a = new frmItem();
    public bool CheckForm()
    {
        Form form;

        form = Application.OpenForms[a.Text];
        if (form == null)
            return true;
        else
            return false;
    }

and on btn_Click i wrote this code

private void buttonItem4_Click(object sender, EventArgs e)
    {
        frmAssets formSecond = new frmAssets();
        if (CheckForm())
        {
            formSecond.MdiParent = this;
            formSecond.Show();
        }
        else
        {
            MessageBox.Show("Form Already Open Exists!");
        }
    }

this shows me message "Form Already Open Exists!" if i have already opened forms, works fine! but then how should i close the already opened forms before opening a new form... ? I tried a.close(); by replacing MessageBox.Show("Form Already Open Exists!"); but no sake.

3
  • You can use form.ShowDialog() instead of form.Show()
    – Mehmed
    Aug 6, 2015 at 17:45
  • Why can't you just use formSecond.ShowDialog()? Alternatively, use a form-level property and assign it null. Then when you launch the form, give that property a value and when it closes re-assign it null. When they click the button, check if it's null or not and either show the already open form or open a new instance.
    – sab669
    Aug 6, 2015 at 17:45
  • You need to understand the scope of each form you declare. Move the form variable outside of your button click and check if the variable is still shown instead of using your check form function.
    – Marko
    Aug 6, 2015 at 17:47

1 Answer 1

6

Checking trough the Application.OpenForms to check if is open, and manipulating the MdiParent is pretty dangerous, especially the MdiParent as it manipulates low-level WinAPI.

I would suggest to pass the instance of the form to be checked and closed to the parent form, then you can check on the instance if it is opened, and close it.

And as pointed out in comments, instead of this MdiParent and Show calls, please make a ShowDialog() call.

2
  • I did this, my problem solved but still it just override the existing form and not being closed. public static bool FormIsOpen(String wForm) { if (Application.OpenForms.Count > 1) { var p = Application.OpenForms.Cast<Form>().First(x => x.Name != "frmMain"); p.Close(); } return false; } see more on second comment Aug 7, 2015 at 10:27
  • and the btn_Click behind code private void buttonItem4_Click(object sender, EventArgs e) { if (Program.FormIsOpen("frmAssets")) { return; } var frm = new frmAssets(); frm.TopLevel = false; frm.Dock = DockStyle.Fill; frm.TopMost = true; frm.WindowState = FormWindowState.Maximized; frm.MdiParent = this; frm.Show(); } Aug 7, 2015 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.