Coping with more than one form - i'm new to this stuff

  • Thread starter Thread starter mark_t1980
  • Start date Start date
M

mark_t1980

Hi guys,

This is probably a really simple silly question, but I can't seem to
find a way round it.

I want to display a separate form when I click on a link label on my
main form. I've managed that, but it displays a second form when you
click again because the click event is creating a new instance of the
form each time. How do I go about creating one instance of the form
and displaying it with the click event? This needs to work again
after the form has been closed. The GC seems to dispose of the form
after you close it so calling show() again is obviously a bad idea.
Is there a way of using the visible property when you click on X to
hide the form rather than disposing of it or are there better
ways...I expect there are...please enlighten me. I feel stupid! :(
 
What you need is a global variable that holds the reference the form. When
the user clicks the button check to see if the variable is nothing/null and
if so then create an instance of the form and show it.

If the variable is not nothing then just show the form. You are most likely
seeing a Dispose since the variable holding the form instance goes out of
scope.

Lloyd Sheen

mark_t1980 said:
Hi guys,

This is probably a really simple silly question, but I can't seem to
find a way round it.

I want to display a separate form when I click on a link label on my
main form. I've managed that, but it displays a second form when you
click again because the click event is creating a new instance of the
form each time. How do I go about creating one instance of the form
and displaying it with the click event? This needs to work again
after the form has been closed. The GC seems to dispose of the form
after you close it so calling show() again is obviously a bad idea.
Is there a way of using the visible property when you click on X to
hide the form rather than disposing of it or are there better
ways...I expect there are...please enlighten me. I feel stupid! :(



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
Hi!

Thanks for the advice. I had tried this, but somehow checking for the
null reference doesn't work. The code I have is as follows:

private AutoPlate.frmPrdDatEnt fPrdDatEnt; //My global variable decl.

this.fPrdDatEnt = new frmPrdDatEnt(); //Assign variable ref. to form
inst.
//Click event code is then...
private void lnkProdDatEnt_LinkClicked(object sender,
System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
if (fPrdDatEnt == null)
{
InitialiseScreen();
fPrdDatEnt.Show();
}
else
fPrdDatEnt.Show();
}

The InitialiseScreen() function just has one line in it which is:
frmPrdDatEnt fPrdDatEnt = new frmPrdDatEnt();
//to create a new instance of the form after it has been closed.
Unfortunately, the form is opening once, but then once closed upon
trying to open it again, it crashes with a 'Cannot access a disposed
object named "frmPrdDatEnt"' exception. It would seem it's still
going through the else of the if statement...any ideas why?

Thanks guys...
 
OK...I realised the separate function to create a new instance was
silly cos that just goes out of scope. It does seem though, that when
the GC disposes of the form it doesn't set the reference variable to
null. Is there anything else I can do to test for if the form has
been disposed of?

Thanks...
 
Back
Top