Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello Frnds
I want to know how to detect is form already opened. If form is opened then close it and Reopen IT.

In my application I am using Single form to view customer information and create new customer Information.
I want to do if user has already opened a customer form and again tries to open IT then Previously opened form should be close and new instance of form will be opened .
Plz Help Me.

Nitin bhoyate
Equilux Technologies
Posted
Comments
CS2011 27-Feb-11 2:49am    
if you can tell if there is any specific reason to close and reopen the form i'll be able to help you in a better way....meanwhile you can google singletone class and see if it helps...

Look at this code.

Basically i have a form with 2 buttons on it, one runs a test, the other opens an instance of an other form.

Using the OpenForms collection we can check if the form already is open;

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonOpenOtherForm_Click(object sender, EventArgs e)
        {
            Form theOtherOne = new FormTheOtherForm();
            theOtherOne.Name = "SomeFormName";
            theOtherOne.Show();

        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            foreach (Form aForm in Application.OpenForms)
            {
                if (aForm.Name.Equals("SomeFormName"))
                {
                    MessageBox.Show("SomeFormName Already Open, closing and repopening next");

                    aForm.Close();

                    buttonOpenOtherForm.PerformClick();
                    break;
                }
            }
        }
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-11 2:30am    
It will work, my 4, but hiding a form is much better. I show all options in my Answer, please, see. Thank you.
--SA
DaveAuld 27-Feb-11 2:51am    
The Op asked about closing and reopening, not hiding, he is maybe wanting to close the form and repopen it to allow it to re-initialise/reset what it does.
Sergey Alexandrovich Kryukov 1-Mar-11 2:56am    
I Answer for real help, to give an advice, not for answering.
Direct answer, very typically, in not most helpful.
--SA
nitin bhoyate 27-Feb-11 2:42am    
Thanks
CS2011 27-Feb-11 3:16am    
Good Answer...My 5
First of all, even though a form can be closed, there is not such thing as "Open" operation (and, hence, it is not possible to know if a form is "already opened" :-). An instance of a Form is instantiated via its constructor, then shown, then closed.

Everything depends on how you close the form. I think the best way of closing is hiding the form; to do this, override :

C#
protected override void OnFormClosing(FormClosingEventArgs e) {
    if (e.CloseReason == CloseReason.UserClosing) {
        e.Cancel = true;
        Hide();
    } else
        base.OnFormClosing(e);
}


You can get the same effect by handling the event FormClosing, but override is implemented easier.

For more detail on what happens on closing the form, please see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx[^]. As you can see, hiding the form is the recommended way of handling this situation. Note, that a closed form is disposed (see below).

In this way, only one operation is needed: Form.Show.

If you need the form to be really closed, you can test the predicate Form.IsDisposed. To show the form again, you will need to instantiate an instance of the form again and then show new instance, as a disposed cannot be shown. Another way is checking Application.OpenForms and trying to find a form in question in the collection.

I do not recommend closing of the form by the user; hiding is much better. When a form is hidden, it preserved its status and status of all the controls, when a form is shown again. Don't think you save memory on closing the form. You don't.

Don't forget the ownership relationship between forms. It is better to make non-main forms owned by main forms:

C#
Form MyOwnedForm = new CustomerInformationForm();
MyOwnedForm.Owner = MyMainForm;
MyOwnedForm.ShowInTaskbar = false; //also very useful: only one task bar icon per application
MyOwnedForm.Show(MyMainForm);


This will guarantee coherent activation of the forms of the same application, should be used with ShowInTaskbar = false for consistency.

A final recommendation: it's the best to keep number of forms per application to bare minimum, with one more complex form being the best option. Use tabbed UI, docking UI or something like that.

—SA
 
Share this answer
 
Comments
nitin bhoyate 27-Feb-11 2:43am    
Thanks
Sergey Alexandrovich Kryukov 1-Mar-11 2:54am    
You're welcome. I seriously advise to follow this: don't close the form, this is the best option in practice.
--SA
DaveAuld 27-Feb-11 2:54am    
Don't agree with "it is not possible to know if a form is already opened" what do you think the OpenForms collection of the Application class does? You can also use this to then access each form obejct and test if it is visible etc....
Olivier Levrey 27-Feb-11 10:29am    
I agree with DaveAuld about OpenForms.
But I agree with SA's suggestion about hiding the form is better than closing and reopening it. If OP wants to reinitialize the form, then he can add an Init method called from within the constructor and the VisibleChanged event for example.
Voted 4.
Sergey Alexandrovich Kryukov 27-Feb-11 12:28pm    
You're right. This is just the rant on terminology from my side :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900