Simple problem...

  • Thread starter Thread starter Sender
  • Start date Start date
S

Sender

Hello VB.Net gurus/MVPs/MCPs.....here is another simple/basic query from a
beiggner....

I have a VB.Net Project with 2 forms form1 (default) and form2. On form1
there is button button1 which opens the form2. This is what I wrote to open
form2:

Dim frm as New form2()
(The above line I wrote outside any procedure)

On Button1 I wrote the following code:
frm.show

Everything work fine when I click on this button to open form2. However, the
problem occurs when I close form2 (by clicking on the close button of the
form2) and try to open form2 again by clicking on button1 on form1. That
means eveything works fine for the first time but not afterwards. Please
help!

Thanks in advance!
 
Hi Sender,

Your form has most probable gone down the pan. Has been disposed and the bin
men (Garbage Collector) has come and taken it away.

Instead of instantiating the form outside of the procedure, instantiate it
before you call frm.Show:

frm = New Form2()
frm.Show()

But what happens if you click the button again, and the form is already
being shown?? Panic!! Chaos!!

To resolve this, you can put some simple error trapping code in:

If Not frm.Visible Then
frm = New Form2()
frm.Show()
End If

If the form is currently visible, then it won't do anything.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Sender said:
Hello VB.Net gurus/MVPs/MCPs.....here is another simple/basic query from a
beiggner....

Posts are usually addressed to _all_ readers.
I have a VB.Net Project with 2 forms form1 (default) and form2. On form1
there is button button1 which opens the form2. This is what I wrote to open
form2:

Dim frm as New form2()
(The above line I wrote outside any procedure)

On Button1 I wrote the following code:
frm.show

In the procedure, type this code:

\\\
Dim f As New Form2()
f.Show()
///

Or even shorter:

\\\
Call (New Form2()).Show()
///

Then remove the variable declaration "outside" the procedure.
 
Hey Big Sender,

Dim frm as New form2 (outside any procedure) will create a new form -
lovely form - complete with a Windows handle and evrything it needs. :-)

frm.show (within Click) will show that Form. Everything's still lovely.
:-)

Me.Close (within the form2) will Close <and Dispose> that form and release
the Windows handle and anything else that it was using. That's ok, that's
cool. :-)

frm.show number 2 (within Click) will try and show a form that's had its
guts removed. Oh, dear - not lovely at all. :-(((

Solutions:
1) If you need to keep form2 alive - hide it rather than close it.

2) If you don't, create a new one in Click.

Dim frm as form2 (Outside)

Sub Clicky_Click
frm = New form2
frm.Show

Everything's lovely again ;-))

Regards,
Fergus
 
Hey Big Sender,
Dig this blender... Rainbow suspenders....
Sub Clicky_Click
frm = New form2
frm.Show

Everything's lovely again ;-))

You seem in a cheerful mood, a piece of VB.NET code works for you?

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Hi Tom,

I'm in a silly mood. ;-))

Maybe it's 'cos the heating's finally got there and I can LMAO instead of
freezin' it. It can't be the chocolate because I'm only just about to scoff
that.

Regards,
Fergus

ps. I missed the frm.Visible bit - well spotted that man.
 
If Not frm.Visible Then
frm = New Form2()
frm.Show()
End If

Hi Tom,
When I try that I get this "An unhandled exception of type
'System.NullReferenceException' occurred"
Do you know what I do wrong?
Cor
 
Well spotted...

Oops - Apart from the 'System.NullReferenceException', that is. ;-))
 
Tom Spink said:
Your form has most probable gone down the pan. Has been disposed and the bin
men (Garbage Collector) has come and taken it away.

Instead of instantiating the form outside of the procedure, instantiate it
before you call frm.Show:

frm = New Form2()
frm.Show()

But what happens if you click the button again, and the form is already
being shown?? Panic!! Chaos!!

To resolve this, you can put some simple error trapping code in:

If Not frm.Visible Then
frm = New Form2()
frm.Show()
End If

This will throw an exception if no instance of the form is "loaded".
If the form is currently visible, then it won't do anything.

:-)
 
Hi Herfried, Yikes!! Good obersvation.

Alternate:

If frm Is Nothing Then
frm = New Form2()
End If

If Not frm.Visible Then
frm.Show()
End If


--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Hi Tom,

|| Hi Herfried, Yikes!! Good obersvation.
____^

Is 'ober'svation some sort of over-seeing? ;-)

Regards,
Fergus
 
Hi Fergus,

I've had an awful cold for the last few days... It's messing with my mind.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Hi Tom,

Here's to your better health [raises glass].

I like that typo, though. ;-)

Regards,
Fergus
 
;-) Übersehen

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Back
Top