Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:


private void mnDBInfo_Click(object sender, System.EventArgs e)
{ // Display History Form
frmHistory pagefrmHistory = new frmHistory();
pagefrmHistory.Show();
}
[/Code]

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,

Posted
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.

 

	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;
		}
	}

Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

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

Posted
Well spotted, that was just a typo, I was testing it with the default form names and went through changing them for the post, I just missed one.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

Wouldn't a singleton sort of approach work just fine here?

[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

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...