How to hide a form and show it again from another form.

quickbasic22

Member
Joined
Jun 16, 2004
Messages
6
Location
United States, Florida
Programming Experience
1-3
This troubled me for quite awhile.

if you hide a form and goto another this is how you show that same
instance of form1 again without creating a new instance losing your data.

class form1
inherits windows.forms.form
dim secondform as form2

button1_Click(sender ,e)
me.hide
secondform = new form2
secondform.FirstForm = me secondform.show

end sub

end class


class form2
inherits windows.forms.form
public FirstForm as form1

button1_Click(sender,e)
me.hide
FirstForm.show

end sub

end class

Basically it creates form2 before it leaves form1. It Passes a reference of form1 to form2 before it leaves form1. So in secondfirm, FirstForm equals the form you left.
Edited on 2/17/2006
 
Last edited:

Jonatan

Member
Joined
Jun 8, 2004
Messages
11
Location
Cuba
Programming Experience
3-5
Using the Tag property

You should use the Tag property from one form to reference the other, as shown below:

VB.NET:
 [size=2][color=#800080]
Public[/color][/size][size=2] [/size][size=2][color=#800080]Class[/color][/size][size=2] Form1

[/size][size=2][color=#800080]Inherits[/color][/size][size=2] System.Windows.Forms.Form

[/size][size=2][color=#800080]Dim[/color][/size][size=2] frm2 [/size][size=2][color=#800080]As[/color][/size][size=2] [/size][size=2][color=#800080]New[/color][/size][size=2] Form2

[/size][size=2][color=#008000] [/color][/size][size=2].[/size][size=2][/size][size=2][color=#800080]Private[/color][/size][size=2] [/size][size=2][color=#800080]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#800080]ByVal[/color][/size][size=2] sender [/size][size=2][color=#800080]As[/color][/size][size=2] System.Object, [/size][size=2][color=#800080]ByVal[/color][/size][size=2] e [/size][size=2][color=#800080]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#800080]Handles[/color][/size][size=2] Button1.Click

frm2.Show()

[/size][size=2][color=#800080]Me[/color][/size][size=2].Hide()

[/size][size=2][color=#800080]End[/color][/size][size=2] [/size][size=2][color=#800080]Sub

[/color][/size][size=2][/size][size=2][color=#800080]Private[/color][/size][size=2] [/size][size=2][color=#800080]Sub[/color][/size][size=2] Form1_Load([/size][size=2][color=#800080]ByVal[/color][/size][size=2] sender [/size][size=2][color=#800080]As[/color][/size][size=2] [/size][size=2][color=#800080]Object[/color][/size][size=2], [/size][size=2][color=#800080]ByVal[/color][/size][size=2] e [/size][size=2][color=#800080]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#800080]Handles[/color][/size][size=2] [/size][size=2][color=#800080]MyBase[/color][/size][size=2].Load

frm2.Tag = [/size][size=2][color=#800080]Me

[/color][/size][size=2][/size][size=2][color=#800080]End[/color][/size][size=2] [/size][size=2][color=#800080]Sub

End[/color][/size][size=2] [/size][size=2][color=#800080]Class

[/color][/size]
Now the other form is very simple:
VB.NET:
[size=2][color=#800080]Public[/color][/size][size=2] [/size][size=2][color=#800080]Class[/color][/size][size=2] Form2

[/size][size=2][color=#800080]Inherits[/color][/size][size=2] System.Windows.Forms.Form[/size]
[size=2][size=2][color=#800080]Private[/color][/size][size=2] [/size][size=2][color=#800080]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#800080]ByVal[/color][/size][size=2] sender [/size][size=2][color=#800080]As[/color][/size][size=2] System.Object, [/size][size=2][color=#800080]ByVal[/color][/size][size=2] e [/size][size=2][color=#800080]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#800080]Handles[/color][/size][size=2] Button1.Click

[/size][size=2][color=#800080]CType[/color][/size][size=2]([/size][size=2][color=#800080]Me[/color][/size][size=2].Tag, Form1).Show()

[/size][size=2][color=#800080]Me[/color][/size][size=2].Hide()

[/size][size=2][color=#800080]End[/color][/size][size=2] [/size][size=2][color=#800080]Sub

End[/color][/size][size=2] [/size][size=2][color=#800080]Class

[/color][/size][/size]
Done!
 
Top Bottom