Hide- Unload form

New1

Member
Joined
Dec 5, 2005
Messages
9
Programming Experience
1-3
Hi,
I have two forms, Form1 and Form2.
I want to load Form2 from Form1 and to hide, unload Form1.
(and of course, the opposite direction, from Form2 to Form1)
I know how to call one form from another, but my problem is that the first one still remains (unactivated) behind the second. (background)



In VB6 it was easy

Form2.show()
Unload Form1
Form1.Hide

In vb.net?

Thanx in advance
NEW1
 
I also had this problem and Me.Hide() was not working, to fix it I did the following:

Example project has 2 forms, class, and module.

Class Code:

Public
Class MyApplicationContext
Inherits ApplicationContext
PublicSubNew()
MyBase.New()
switchTo(
Nothing, modForm.Forms.frmMain)
EndSub
PrivateSub switchTo(ByVal sender As Form, ByVal formName As Forms)
IfNot (sender IsNothing) Then
sender.Dispose()
EndIf
'Form case statement are adding in here
SelectCase formName
Case modForm.Forms.frmMain
Dim frmMain As frmMain = New frmMain
AddHandler frmMain.switchTo, AddressOf switchTo
AddHandler frmMain.Closed, AddressOf OnFormClosed
frmMain.Show()
Case modForm.Forms.frmApp
Dim frmApp As frmApp = New frmApp
AddHandler frmApp.switchTo, AddressOf switchTo
AddHandler frmApp.Closed, AddressOf OnFormClosed
frmApp.Show()
'Case modForm.Forms.frmSet 'Example
'Dim frmSet As frmSet = New frmSet
'AddHandler frmSet.switchTo, AddressOf switchTo
'AddHandler frmSet.Closed, AddressOf OnFormClosed
'frmSet.Show()
EndSelect
EndSub
PrivateSub OnFormClosed(ByVal sender AsObject, ByVal e As EventArgs)
ExitThread()
EndSub
End
Class

Module Code:

Public
Module modForm
PublicEnum Forms
'Add in your Forms here
frmMain = 0
frmApp = 1
'frmSet = 2 ' Example
EndEnum
PublicSub Main()
Dim context As MyApplicationContext = New MyApplicationContext
Application.Run(context)
EndSub
End
Module

Form1 (fmrMain) Code:
PublicClass frmMain
Inherits System.Windows.Forms.Form
-- Windows Form Designer generated code --
PublicEvent switchTo(ByVal sender As Form, ByVal formName As Forms)
PrivateSub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Close()
EndSub
PrivateSub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
RaiseEvent switchTo(Me, modForm.Forms.frmApp)
EndSub
End
Class


Form2 (frmApp) Code:

Public
Class frmApp
Inherits System.Windows.Forms.Form
-- Windows Form Designer generated code --
PublicEvent switchTo(ByVal sender As Form, ByVal formName As Forms)
PrivateSub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Close()
EndSub
PrivateSub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'RaiseEvent switchTo(Me, modForm.Forms.frmSet) ' Example
EndSub
End
Class

Last you need to set project properties to start with Sub Main()

I also attached the project.

Hope this help!
Mythol

 

Attachments

  • Form Switch.zip
    44.7 KB · Views: 42
i simply do this to open a new form and hide the first one:
VB.NET:
Dim Frm2 As New Form2
Frm2.Show()
Me.Hide
havnt had any trouble yet
 
First of all, i'd like to thank all of you!

Mythol, i copied without mistakes the blocks of code you sent, but when i click (in form1) the button NEXT, nothing happens. I am sure that threre are no errors copying your code to 2 forms, 1 module and 1 class. Moreover, I couldn't open the project that you sent me, because i have installed Visual Studio 2002 (you probably have a newer version).There is no compatibility...
Any idea?
Thanx again
NEW1
 
Furthermore, the code
Dim Frm2 As New Form2
Frm2.Show()
Me.Hide
isn't exactly what i need at this time, because is hides the previous form but it doesn't unload it. So,in a project, i have to call many forms (form1->form2-->form3 etc) closing the previous and releasing the resources they keep. (need of clearing the memory)
 
to open a form from a form (closing the 1st form) would require that your application be started from Sub Main() and keeping track of which form is open, then opening the next one from that sub when a form is closed

in Sub Main() i would suggest using Application.Run(FormHere) to run each form because when the form is closed, it'll return to that line in Sub Main()
 
Back
Top