How to re-open a form after closing?

DoJa

Active member
Joined
Mar 23, 2014
Messages
33
Programming Experience
1-3
I have a line
C#:
frmSelectmember.Default.Close();
to close a form (closing here is preferable to hiding) but I can't see how to reload/create/open the form. In vb calling form.show after closing would cause it to automatically reload without doing anything special but in c# it seems you have to be a bit more explicit.
 
In VB, you can use the class name to access the default instance. Behind the scenes, that will create a new instance if there is no existing one or the existing one has been disposed. VB makes it appear that you can Show a form a second time after you Close it but you can't; it's just that VB creates the new one for you automatically. In C#, you must create the instance for yourself. VB used to be like that too and, to be honest, I am not the only one who wishes that it still was. Default instances cause as many issues as they solve.

Anyway, the specifics of what you should do depend on the circumstances but you could put code like this in one form to display one and only one instance of another form:
private SomeForm formInstance = null;

private void ShowSomeForm()
{
    if (formInstance == null || formInstance.IsDisposed)
    {
        formInstance = new SomeForm();
    }

    formInstance.Show();
    formInstance.Activate();
}
The test for null handles the first instance and the test for IsDisposed handles subsequent instances. The call to Show handles displaying a new instance and the call to Activate handles focusing an existing instance.
 
Back
Top Bottom