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.
form.ShowDialog()
instead ofform.Show()
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.