By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

VB.NET and FORMS (Form.Show, Form.Hide ... )

P: n/a

I'm moving from Vb6 to VB.Net. I have a feeling this has come up
before...

The VS.Net MSDN file seems to state that the following should work:

Form1.Show
Form1.Visible = True
Form1.Hide
Form1.Visible = False
Load (Form1)
Unload (Form1) or perhaps Close (Form1)

Yet, as you know, none of these work. I also checked a book on
converting VB6 to VB.Net, and it also seems to say that the above is
correct. So I'm guessing my syntax is wrong. Corrections will be
appreciated, particularly since I've been pulling my hair out doing
this and I'm starting to run low on hair...

Thanks

Jul 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Dim f As New System.Windows.Forms.Form()

f.lblDesc.Text = "Did you instantiate your new form like this?"

f.Show() or...

f.showdialog() for modal form

Me.Close() from within form for modal or...

f.Close() or...

f.Hide()

f.dispose()

<ge***********@lycos.com> wrote in message
news:sg********************************@4ax.com...

I'm moving from Vb6 to VB.Net. I have a feeling this has come up
before...

The VS.Net MSDN file seems to state that the following should work:

Form1.Show
Form1.Visible = True
Form1.Hide
Form1.Visible = False
Load (Form1)
Unload (Form1) or perhaps Close (Form1)

Yet, as you know, none of these work. I also checked a book on
converting VB6 to VB.Net, and it also seems to say that the above is
correct. So I'm guessing my syntax is wrong. Corrections will be
appreciated, particularly since I've been pulling my hair out doing
this and I'm starting to run low on hair...

Thanks

Jul 21 '05 #2

P: n/a
Hi Genetic,

I'm guessing that you are trying to do those things from another Form. In
which case it <has> come up before and will again (and again!!)

Form1 in VB6 can be used to get at the instantiated Form. Form1.Visible is
the Visible property of the actual Form.

Form1 in VB.NET is the <class> from which instances of the form can
created. Form1 as an <object> no longer applies. Form1.Visible fails because
it is asking for the Visible property of the class, and the class doesn't have
one - it is only availble to instances of the class.

If you want to access your 'Form1' from a different Form, (or another
class, for that matter) you need to have a reference to the instance of that
Form1. This can be stored globally in a module or passed into the other
Form/class.

If this explanation only serves to raise more questions, ask away. :-)

Regards,
Fergus
Jul 21 '05 #3

P: n/a
Maybe you can answer a question for me, Fergus?
Not meaning to but in on someone elses thread, but its kinda related ...

I suggested
Form.Close() or ...
Form.Hide()
Form.Dispose()

Form.Close() is the method I have been taught and that I use, however, I
have run across several code samples (written by people a hell of a lot more
knowledge than me) that have used the Hide() > Dispose() method.
Whats the difference?
..
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
Hi Genetic,

I'm guessing that you are trying to do those things from another Form. In which case it <has> come up before and will again (and again!!)

Form1 in VB6 can be used to get at the instantiated Form. Form1.Visible is the Visible property of the actual Form.

Form1 in VB.NET is the <class> from which instances of the form can
created. Form1 as an <object> no longer applies. Form1.Visible fails because it is asking for the Visible property of the class, and the class doesn't have one - it is only availble to instances of the class.

If you want to access your 'Form1' from a different Form, (or another
class, for that matter) you need to have a reference to the instance of that Form1. This can be stored globally in a module or passed into the other
Form/class.

If this explanation only serves to raise more questions, ask away. :-)

Regards,
Fergus

Jul 21 '05 #4

P: n/a
Hi What-a-Tool,

You should call Close, because this is the overload and ruler <grins>

Okay, seriously though. Calling Close will call Dispose, but will also fire
off some form events, such as the Closing and Closed event.

As for 'Hide', this simply sets the Visible property of the form to false.

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

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"
"What-a-Tool" <Fr***********************@cox.net> wrote in message
news:WGFdb.28628$0Z5.24119@lakeread03...
: Maybe you can answer a question for me, Fergus?
: Not meaning to but in on someone elses thread, but its kinda related ...
:
: I suggested
: Form.Close() or ...
: Form.Hide()
: Form.Dispose()
:
: Form.Close() is the method I have been taught and that I use, however, I
: have run across several code samples (written by people a hell of a lot
more
: knowledge than me) that have used the Hide() > Dispose() method.
: Whats the difference?
: .
: "Fergus Cooney" <fi******@tesco.net> wrote in message
: news:ep**************@TK2MSFTNGP10.phx.gbl...
: > Hi Genetic,
: >
: > I'm guessing that you are trying to do those things from another
Form.
: In
: > which case it <has> come up before and will again (and again!!)
: >
: > Form1 in VB6 can be used to get at the instantiated Form.
: Form1.Visible is
: > the Visible property of the actual Form.
: >
: > Form1 in VB.NET is the <class> from which instances of the form can
: > created. Form1 as an <object> no longer applies. Form1.Visible fails
: because
: > it is asking for the Visible property of the class, and the class
doesn't
: have
: > one - it is only availble to instances of the class.
: >
: > If you want to access your 'Form1' from a different Form, (or
another
: > class, for that matter) you need to have a reference to the instance of
: that
: > Form1. This can be stored globally in a module or passed into the other
: > Form/class.
: >
: > If this explanation only serves to raise more questions, ask away.
:-)
: >
: > Regards,
: > Fergus
: >
: >
:
:
Jul 21 '05 #5

P: n/a
Hi What-a-Tool,

Tom's said it. :-)

It would never occur to me to use Hide and then Dispose (why use two lines
when one will do). I guess I would if I wanted to skip raising those close
events. But why? Let's ask these people with a hell of a lot more knowledge!!
;-)

Regards,
Fergus

ps Any relation to Peter-o-Tool, or is it just a rampant resemblance?
Jul 21 '05 #6

P: n/a
: ps Any relation to Peter-o-Tool, or is it just a rampant resemblance?

ROFLM*O

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

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:Om**************@TK2MSFTNGP11.phx.gbl...
: Hi What-a-Tool,
:
: Tom's said it. :-)
:
: It would never occur to me to use Hide and then Dispose (why use two
lines
: when one will do). I guess I would if I wanted to skip raising those close
: events. But why? Let's ask these people with a hell of a lot more
knowledge!!
: ;-)
:
: Regards,
: Fergus
:
: ps Any relation to Peter-o-Tool, or is it just a rampant resemblance?
:
:
Jul 21 '05 #7

P: n/a
101 code samples - How to Multi-Threading - form main at the bottom.
I'm thinking it's pretty safe to say whoever wrote that has a hell of a lot
more knowledge than me, as I don't have any code samples anywhere on the
web, nevermind published by Microsoft.
I've seen it in other samples too, but this is a recent one to me, that
jumped right to mind.

No relation to Peter O'Toole.
What-a-Tool - I'm a Tool and Die Maker by proffession - thats the main
reference.
Saying "What-a-Tool" in reference to someone is also like saying
"What-a-shithead". It's a derogatory comment to make about someone, and it
made me smile to take it as a screen name.
Kinda along the same lines as "Genetic.error" I suppose.
Name is Sean, and thanks for enlightening me.
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:Om**************@TK2MSFTNGP11.phx.gbl...
Hi What-a-Tool,

Tom's said it. :-)

It would never occur to me to use Hide and then Dispose (why use two lines when one will do). I guess I would if I wanted to skip raising those close
events. But why? Let's ask these people with a hell of a lot more knowledge!! ;-)

Regards,
Fergus

ps Any relation to Peter-o-Tool, or is it just a rampant resemblance?

Jul 21 '05 #8

P: n/a
Hi Sean,

|| What-a-Tool - I'm a Tool and Die Maker

And here we were thinking that you're a superstud -
you should have kept quiet. ;-)

|| 101 code samples

Close() is used throughout the examples while Hide() and Dispose() only
occurs once. I'd guess that it's spurious. Maybe the code was adapted from a
different language where there are different coding styles (eg, WinAPI
upbringing). Or perhaps that is that author's style. I can't see it as being
much deeper than that.

Regards,
Fergus
Jul 21 '05 #9

P: n/a


Thank you What A Tool and Fergus Cooney. Yes, I have more
questions...

1. This little gem in form declarations:
Public fMAIN As New System.Windows.Forms.Form
plus this in a button click event
fMAIN.Hide()
Hid the form the old fashioned way - it rebooted my computer. Crude
but effective ;) Is there an issue involved with hiding the startup
form ?

2. How do I refer to the other forms? In other words, assuming I
successfully hide the startup form, how do I show another form?

I tried this in form declarations for form1 (non startup form):
Public fONE As New System.Windows.Forms.Form
and then tried this
fONE.Show
from my startup form. I ended up with "Name fONE is not declared".
I also tried:
Public f1 As New System.Windows.Forms.Form
in a module. At this point, the module doesnt know what form I'm
referring to, since i'm not decalring inside a form. So I added:
f1 = form1
And I got "declaration expected" for f1 (which I just declared) .
So the confusion continues. Thank you for your explanations, even if I
dont have it fixed, I at least have a better idea as to way it's
broke. ;)

Thanks


On Sun, 28 Sep 2003 13:11:51 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote:
Hi Genetic,

I'm guessing that you are trying to do those things from another Form. In
which case it <has> come up before and will again (and again!!)

Form1 in VB6 can be used to get at the instantiated Form. Form1.Visible is
the Visible property of the actual Form.

Form1 in VB.NET is the <class> from which instances of the form can
created. Form1 as an <object> no longer applies. Form1.Visible fails because
it is asking for the Visible property of the class, and the class doesn't have
one - it is only availble to instances of the class.

If you want to access your 'Form1' from a different Form, (or another
class, for that matter) you need to have a reference to the instance of that
Form1. This can be stored globally in a module or passed into the other
Form/class.

If this explanation only serves to raise more questions, ask away. :-)

Regards,
Fergus


Jul 21 '05 #10

P: n/a
Instead of a start up form, start with Sub Main, instantiate and show your
main form from there?

<ge***********@lycos.com> wrote in message
news:fm********************************@4ax.com...


Thank you What A Tool and Fergus Cooney. Yes, I have more
questions...

1. This little gem in form declarations:
Public fMAIN As New System.Windows.Forms.Form
plus this in a button click event
fMAIN.Hide()
Hid the form the old fashioned way - it rebooted my computer. Crude
but effective ;) Is there an issue involved with hiding the startup
form ?

2. How do I refer to the other forms? In other words, assuming I
successfully hide the startup form, how do I show another form?

I tried this in form declarations for form1 (non startup form):
Public fONE As New System.Windows.Forms.Form
and then tried this
fONE.Show
from my startup form. I ended up with "Name fONE is not declared".
I also tried:
Public f1 As New System.Windows.Forms.Form
in a module. At this point, the module doesnt know what form I'm
referring to, since i'm not decalring inside a form. So I added:
f1 = form1
And I got "declaration expected" for f1 (which I just declared) .
So the confusion continues. Thank you for your explanations, even if I
dont have it fixed, I at least have a better idea as to way it's
broke. ;)

Thanks


On Sun, 28 Sep 2003 13:11:51 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote:
Hi Genetic,

I'm guessing that you are trying to do those things from another Form. Inwhich case it <has> come up before and will again (and again!!)

Form1 in VB6 can be used to get at the instantiated Form. Form1.Visible isthe Visible property of the actual Form.

Form1 in VB.NET is the <class> from which instances of the form can
created. Form1 as an <object> no longer applies. Form1.Visible fails becauseit is asking for the Visible property of the class, and the class doesn't haveone - it is only availble to instances of the class.

If you want to access your 'Form1' from a different Form, (or another
class, for that matter) you need to have a reference to the instance of thatForm1. This can be stored globally in a module or passed into the other
Form/class.

If this explanation only serves to raise more questions, ask away. :-)
Regards,
Fergus

Jul 21 '05 #11

P: n/a
Hi Genetic,

|| fMAIN.Hide() - Hid the form the old fashioned way - it rebooted
|| my computer. Crude but effective ;)

ROFL.
Strange though - hiding the startup Form shouldn't present any problems.
System.Windows.Forms.Form is a class. In any class, the variables that you
declare belong to that class. You can't get to those variables from outside
the class without referencing the class - either using the class name or an
instantiated object of that class.

In other words, if you declare
Public fONE As New System.Windows.Forms.Form
in the class form1 (non startup form) and call
fONE.Show
from your startup form, it <will indeed> say "Name fONE is not declared.
This is because the startup form doesn't have a variable called fONE - you
declared it in form1. You need a form1 to access the variable fONE.

There. I'm sure that's cleared that up!!

|| I also tried: Public f1 As New System.Windows.Forms.Form
|| in a module.

That's fine.

|| At this point, the module doesnt know what form I'm referring to.

Actually you're still mostly fine. The variable f1 has been given an
actual Form to show because you used the New keyword. You could say f1.Show
and it would work (and give you a blank form). But that declaration is only
'mostly' because you're after a form1 not a blank form.

|| since i'm not decalring inside a form.

Strictly speaking, you can declare a form variable anywhere that you can
define variables. Declaring form variables inside Form classes is not
obligatory.

|| So I added:
|| f1 = form1 'Compiler error.
|| And I got "declaration expected" for f1 (which I just declared) .

This is again correct. Read my last post again and see if it makes anymore
sense. I'll carry on anyway, though. "form1" is the name of the <class>. You
can't Show, Hide or do <anything> with a form1 - because it's the name of the
class. But you can make <instances> of form1. These will be actual Forms that
you <can> show and hide, etc.
f1 = form1 won't give you a form1 but you were close. You needed to add
the New keyword to make an instance.
f1 = New form1

================================================== ======
This issue is a major stumbling block for many programmers coming from VB6
to VB.NET.

Forms are no longer just there. They have to be managed. What's worse is
that I'm not quite telling the truth. The .NET designers, in an effort to be
helpful, have hidden some of the code that, if it had to be done manually,
would make things clearer.

In C#, if you want a form, you have to code its declaration and its
instantiation and then show it. All C# programs start with their "Sub Main".
This creates the startup form and off you go. Being explicit means less
confusion.

In VB, if you don't use Sub Main as your starting point, the compiler
effectively creates it for you - behind the scenes. And thus the confusion
starts! Here you have an obviously visible form which has appeared out of
nowhere. There is no variable with which to access it from outside the form.
There is little (to a newcomer from VB6) to say that this Form1 is <not> the
form that you can see, but is the class (template) for the form.

Create a project with a single form, Form1 and this is what you get
(simplified)

Class Form1
Sub Main
Dim f As New Form1
Application.Run (f)
End Sub

Sub New
Sub Form1_Load
End Class

But you never get to see the Sub Main as this is done for you and hidden.
In VB6, Form1 would be the class <and> the instance. In VB.NET, Form1 is the
class <only> while f, the hidden variable, is the instance.

Let's put Sub Main into a module:

Module LetsGo
Public f As New Form1
Sub Main
Application.Run (f)
End Sub
End Module

Class Form1
Sub New
Sub Form1_Load
End Class

Now you have a global variable, f, which is the actual form. You can
access this variable from anywhere and use it to Show and Hide, etc.

Ok, that's enough for now.

Have a read of this and read my last post again to get as much sense out
of them as possible. If possible get a beginning book on VB.NET so that you
can read what I've been saying in another way. The distinction that you're
after is that Forms and Form classes are now totally distinct where once they
were seemingly interchangeable.

Come back with any more questions. We'll get you there! :-)

Regards,
Fergus
Jul 21 '05 #12

P: n/a

Hi Fergus

Thank you for your help. Unfortunately, it's still not working, which
I dont understand, since your explanation made sense.

-----------------------------

Regarding the startup form:

Public fSTART As New System.Windows.Forms.Form

Private Sub Pic1_Click... Handles Pic1.Click

fSTART.Hide()
MsgBox("Something worked right !!!")

End Sub

Well this isnt giving me any errors! The message box even appears. The
problem is that when that sub finishes running, the form is visible.

-----------------------------

On to calling forms from other forms:
In other words, if you declare
Public fONE As New System.Windows.Forms.Form
in the class form1 (non startup form) and call
fONE.Show
from your startup form, it <will indeed> say "Name fONE is not declared.
This is because the startup form doesn't have a variable called fONE - you
declared it in form1. You need a form1 to access the variable fONE.
Okay, so form1.fONE.Show ? That's what I thought, but i was obviously
wrong. Error is: "Reference to a non shared member requires an object
reference". I have a feeling I was close here...
There. I'm sure that's cleared that up!!
No, but I appreciate the effort. :-)

-------------------------------

On to placing form declarations in the module:
f1 = form1 won't give you a form1 but you were close. You needed to add
the New keyword to make an instance.
f1 = New form1
In the module, this:
Public f1 As New System.Windows.Forms.Form
f1 = NEW form1
caused "Declaration Expected". Unfortunately, NEW didnt solve the
problem.

________________

SO, I'm still lost. I appreciate your input and patience. I'm open to
more suggestions :)

------------------------------
This issue is a major stumbling block for many programmers coming from VB6
to VB.NET.


No doubt !!!
Jul 21 '05 #13

P: n/a
Don't know if this is what you want, or if it will help, but this worked for
me.
Added a Sub Main Module as startup item.
frmStrt is shown and hidden from SubMain.
frmMain is Hidden and reshown from a button_Click event.
Hope this helps. Sean

Module StartUp

Sub Main()

Dim frmMain As New frmMain()

Dim frmStrt As New frmStrt()

frmStrt.Show()

System.Threading.Thread.CurrentThread.Sleep(5000)

frmStrt.Hide()

MessageBox.Show("#1 Hidden")

frmStrt.Close()

'Show Main Form

frmMain.ShowDialog()

End Sub

End Module

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Me.Hide()

MessageBox.Show("#2 Hidden")

Me.Visible = True

End Sub



<ge***********@lycos.com> wrote in message
news:6e********************************@4ax.com...

Hi Fergus

Thank you for your help. Unfortunately, it's still not working, which
I dont understand, since your explanation made sense.

-----------------------------

Regarding the startup form:

Public fSTART As New System.Windows.Forms.Form

Private Sub Pic1_Click... Handles Pic1.Click

fSTART.Hide()
MsgBox("Something worked right !!!")

End Sub

Well this isnt giving me any errors! The message box even appears. The
problem is that when that sub finishes running, the form is visible.

-----------------------------

On to calling forms from other forms:
In other words, if you declare
Public fONE As New System.Windows.Forms.Form
in the class form1 (non startup form) and call
fONE.Show
from your startup form, it <will indeed> say "Name fONE is not declared. This is because the startup form doesn't have a variable called fONE - youdeclared it in form1. You need a form1 to access the variable fONE.


Okay, so form1.fONE.Show ? That's what I thought, but i was obviously
wrong. Error is: "Reference to a non shared member requires an object
reference". I have a feeling I was close here...
There. I'm sure that's cleared that up!!


No, but I appreciate the effort. :-)

-------------------------------

On to placing form declarations in the module:
f1 = form1 won't give you a form1 but you were close. You needed to addthe New keyword to make an instance.
f1 = New form1


In the module, this:
Public f1 As New System.Windows.Forms.Form
f1 = NEW form1
caused "Declaration Expected". Unfortunately, NEW didnt solve the
problem.

________________

SO, I'm still lost. I appreciate your input and patience. I'm open to
more suggestions :)

------------------------------
This issue is a major stumbling block for many programmers coming from VB6to VB.NET.


No doubt !!!

Jul 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.