Windows Forms - Loading & Unloading Forms 
Author Message
 Windows Forms - Loading & Unloading Forms

I'm a VB.NET newbie and I'm struggling to figure out how to switch between
forms.  I have a command button on Form1 that when clicked I would like to
have Form1 hidden and Form2 displayed.  How do I go about doing this?  I
have a book on VB.NET on order but only have the help system available right
now and can't seem to find how to go about doing this.  Thanks in advance.


Sat, 04 Sep 2004 02:11:33 GMT  
 Windows Forms - Loading & Unloading Forms
Use the "Hide" method to hide a form and the "Show" method to show it, for
example:

Private Sub MyButton_Click(Object sender,System.EventArgs e) Handles
MyButton.Click
  Me.Hide ' hides the form the button is on
  myForm2.Show ' show the other form
End Sub

"myForm2" should point to an instance of Form2, declared like this:

Dim myForm2 As New Form2()


Quote:
> I'm a VB.NET newbie and I'm struggling to figure out how to switch between
> forms.  I have a command button on Form1 that when clicked I would like to
> have Form1 hidden and Form2 displayed.  How do I go about doing this?  I
> have a book on VB.NET on order but only have the help system available
right
> now and can't seem to find how to go about doing this.  Thanks in advance.



Sat, 04 Sep 2004 02:40:35 GMT  
 Windows Forms - Loading & Unloading Forms
This is easy in vb6, but for some reason they seem to
have made it harder.

in the button code you need to create a new instance of
your form, then use the ShowDialog method

Example

Assume the form u want to display is called FORM2

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

   'create a new instance for your form
   Dim ShowForm2 as new FORM2

   'show the form
   ShowForm2.ShowDialog()
End Sub

Hope this helps

Roland

Quote:
>-----Original Message-----
>I'm a VB.NET newbie and I'm struggling to figure out how
to switch between
>forms.  I have a command button on Form1 that when

clicked I would like to
Quote:
>have Form1 hidden and Form2 displayed.  How do I go

about doing this?  I
Quote:
>have a book on VB.NET on order but only have the help

system available right
Quote:
>now and can't seem to find how to go about doing this.  
Thanks in advance.

>.



Sun, 05 Sep 2004 03:26:45 GMT  
 Windows Forms - Loading & Unloading Forms

Quote:
> This is easy in vb6, but for some reason they seem to
> have made it harder.

The very fact that you do not realize why they made it 'harder' is the main
reason why they made it like it is.

See in VB6 there was a global variable in the project that represented each
form. These were not instanciated at program startup. Global variables of
this nature are questionnable to begin with. For example, how would you show
two instance of the same form using this technique?

VB6 went further in it's quest to make things easier by automatically
instanciating a form object when the un-instanciated variable was accessed.
Although it makes things easier it's a very awkward way of doing things. One
case where this can get very dangerous and completly loose a newbie is this:

- You have a main form called frmMain.
- You have another form called frmInput which contains an editbox, an OK
button, a Cancel button and finally a public member variable of boolean type
which is called blnOkPressed.
- On OK, blnOkPressed= true, and Unload Me is called
- On Cancel, blnOkPressed= false, and Unload Me is called

In the main form, some bit of code would call this to get a value :

Public sub MySub
    frmInput.Show vbModal
    if frmInput.blnOkPressed then
        MsgBox frmInput.myEditBox.Text
    end if
End Sub

Try it and see what it does. That message box will NEVER be shown. Why?
Because Unload Me uninstanciated the form (you loose any data inputed in the
form). That ain't too bad, cause it's your error, you actually called Unload
me, and you should have called Hide... This is where it gets tricky... The
'if frmInput.blnOkPressed' should in my opinion throw some kind of
exception, but instead it creates a new instance without anyone's consent,
and blnOkPressed is initialized to false, and everything runs seemingly
well........

This is the kind of error a newbie can spend alot of time
debugging..........

In the new VB, YOU instanciate any form you want to show. If you don't the
program blows in your face and you clearly see your error. There's no more
global variable either which is great cause promoting the use of global
variables to newbies really isn't all that great unless you plan on a Cobol
carreer or something :)

So there is a reason why they've made it 'harder'. I was extremely happy to
see this 'feature' removed cause it was simply a ver bad habit even if you
know what you're doing!

Alex.



Wed, 08 Sep 2004 10:21:07 GMT  
 Windows Forms - Loading & Unloading Forms

Quote:
> See in VB6 there was a global variable in the project that represented
each
> form. These were not instanciated at program startup. Global variables of
> this nature are questionnable to begin with. For example, how would you
show
> two instance of the same form using this technique?

Just declare a variable of that form type. Set it to new and call show just
like you would in VB.NET.

A Form in VB 6 is still a class and can be treated like an VB6 class. There
is an Initialize and Terminate event just like normal classes, but as you
mentioned, they have an automagical instance accessible through the form
name. It's nothing but a compiler trick.

Cal



Wed, 08 Sep 2004 14:18:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Unloading Forms causing a Form Load Event???

2. Unloading Forms causing a Form Load Event???

3. Unloading or hiding a form in the form's load event

4. Unloading modal owner form also unloads modal form?

5. Loading and Unloading Forms

6. Save/restore form position on load/unload

7. One more form load unload question

8. VBDOS - proj FORM load/unload memory problem

9. Checking if a form is Loaded/Unloaded

10. Load/Unload form with variable

11. Load and after that Unload the same form

12. Help with unloading and Loading Forms

 

 
Powered by phpBB® Forum Software