Determine if Form is already loaded [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I have a main form (main part of my application called frmApp) and there is a button the user can press that opens another form (frmHistory).
Problem is, the user can press this button multiple times and, as my code is, that will spawn multiple instances of the frmHistory form (which is undesirable).

So, I need a way to determine IF the form is already loaded/showing - and if so not spawn another instance of it...

Currently my code looks like:
Code:
private void mnDBInfo_Click(object sender, System.EventArgs e)
{	// Display History Form
	frmHistory pagefrmHistory = new frmHistory();
	pagefrmHistory.Show();
}
So this code doesn't check to see if an instance of frmHistory is already showing - can I just check to see if pagefrmHistory == null?

Any hints/thoughts would be appreciated, Thanks,
 
PlausiblyDamp said:
If you declare the pagefrmHistory variable outside of the routine then you would be able to just check if it is null or not.

I originally thought the same thing so I gave it a quick test. By creating the frmHistory in the constructor of frmApp. Then whenever I clicked a button I checked if this object was null if it was I added code to re-create it. The problem was that when the frmHistory is closed the original object doesn't appear to be null. Maybe I missed something obvious I don't know but the way I got around it was todo this.

C#:
		public class frmApp : System.Windows.Forms.Form
		{
			frmHistory pagefrmHistory;
			
			public frmApp()
			{
				pagefrmHistory = new Form2();
			}
		}

		public class frmHistory : System.Windows.Forms.Form
		{
			public frmHistory()
			{
			}

			private void frmHistory_Closing(object sender, System.ComponentModel.CancelEventArgs e)
			{
				// add this method as the form closing event
				// essential prevents it being closed, simply hides it from view
				this.Hide();
				e.Cancel = true;
			}
		}
 
You would need to check if it is null or if it has been disposed rather than just null. Also if you are hiding the form or anything similar that would also need to be taken into account.
 
PlausiblyDamp said:
You would need to check if it is null or if it has been disposed rather than just null. Also if you are hiding the form or anything similar that would also need to be taken into account.

I realised the object was disposed rather than null but wasn't sure how to check for this (I've since realised controls have an IsDisposed property). What I was suggesting was to simply create the frmHistory when you create frmApp, this way you don't need to worry about whether it is created or not you simply call frmHistory.Show(), if the Form is already visible this will have no effect.

Alternatively you could do as PlausibleDamp suggests and declare the form outside of the routine then add if(pagefrmHistory == null || pagefrmHistory.IsDisposed) to your mnDBInfo_Click event.

NB. Its important to check if the Form is null before checking if its disposed, as you can't check the .IsDisposed property of a null Form.
 
why are you first declaring pagefrmHistory as frmHistory ....
frmHistory pagefrmHistory;
then when making the new instance of it, declaing it as Form2? ...
pagefrmHistory = new Form2();
shouldn't pagefrmHistory = new frmHistory(); ?
you need to follow PlausiblyDamp's advice of checking if it's null
the way to do this is with a Boolean value, which you would set when changing the properties ( created / visible ) of the form
 
Wouldn't a singleton sort of approach work just fine here?
Visual Basic:
[U][i][COLOR=Blue]Private[/COLOR][/i][/U] Sub New()
'...
End Sub
 
Dim _Instance As MyForm
Public Shared ReadOnly Property Instance As MyForm
    Get
        'Create a new instance if there isn't one already or there is
        'one but it's disposed
        If _Instance Is Nothing OrElse _Instance.Disposed Then
            _Instance = New MyForm
        End If

        'Return the instance
        Return _Instance
    End Get
End Property
 
Back
Top