Hi all,
I am developping an application in VB .Net 2003 with 4 forms. I have crated a main form with some menus. One of those menus should show another form. The problem is that when i referred to the name of this form, i can't acces the "show" method. The only way i found is to create a new instance of my form in the main form code but each time i use the show methos a new window is displayed.
If anyone could help :-(
PS: I think i missed something somewhere.

How to display a form from another one
lorijean44
There are two ways to do this:
1.) Create one instance of each form in the main form, and just call it's Show() method when needed. This is good if you only ever need one instance of the form -- you'll get the same form every time, so any changes you make to it will be reflected on it.
2.) Create a new instance of each form in the appropriate menu event. Good for when you need a fresh copy of the window every time.
Here are examples for both:
Method #1:
Public Class MyForm
Inherits System.Windows.Forms.Form
Dim first as MyFirstForm
Dim second as MySecondForm
Dim third as MyThirdForm
Dim fourth as MyFourthForm
Public Sub New()
MyBase.New()
first = new MyFirstForm()
second = new MySecondForm()
third = new MyThirdForm()
fourt = new MyFouthForm()
End Sub
' In each menu's Click event, you'd just call the appropriate form's Show() method.
' Here is an example for the first form.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
first.Show()
End Sub
End Class
Method #2:
Public Class MyForm
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
End Sub
' In each menu's Click event, you'd instantiate the appropriate form and call the
' appropriate form's Show() method. Here is an example for the first form.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
Dim f as new MyFirstForm()
f.Show()
End Sub
End Class
Hope this helps. Let me know if I missed anything.
-Ari
Chad Bumstead
Before I add some example code, there are a few caveats I need to add:
1.) This only works with method #1 -- you must have a persistant instance of the form hanging around. If you instantiate the form every time you need it, this becomes a moot point since the form variable will go out of scope at the end of the event call and the form will disappear anyway.
2.) Don't handle the closing event on your main form -- if you use this code on your main form, your application will never exit by clicking on the X. If you do use this code, you're going to need a button or menu item somewhere that will exit the application.
Ok, an example. I'm only adding the bare minimums necessary for the class, so things like InitializeComponent and whatnot are assumed to be there:
Public Class MyClass
Inherits System.Windows.Forms.Form
' Your normal class stuff goes here
Dim f as MyLoginForm
Public Sub New()
MyBase.New()
f = new MyLoginForm()
End Sub
Public Sub Login()
f.Show()
End Sub
End Class
Public Class MyLoginForm
Inherits System.Windows.Forms.Form
' Normal class stuff, including constructor, goes here
Private Sub MyLoginForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.Hide()
e.Cancel = True
End Sub
End Class
That should do it. Hope this helps.
-Ari
David Pinero
So my next question will be, how is it possible to close a form without delting the instance.
Anyway thank you very much for the help ;-)