A simple question..how do I load another form

diverdan

Active member
Joined
Jan 2, 2006
Messages
34
Location
London UK
Programming Experience
Beginner
Hi there

As you can tell from my question im very new to vb.net

I have been playing around and have 2 forms.

The first form is form1.vb and the 2nd is form2.vb.

I tried adding a button to the form and added this code to it but it doesn't work.

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
        Me.Hide()

    End Sub

Can someone please tell me what I need to change.

Thanks for your help
 
Hi again,

I have another question regarding loading another form.

The above method worked fine but I have just been playing around with the properties of a project and changed the first form to load when the project opens.

When I try use the above code to load a form the editor does not reference my other form automatically.

How do I use this code to reference another form?

Any tips would be great.


Thanks again!!
 
Hi Dan :)
if you wanna open a form from your main form ( eg : open Form2 from Form1 ) & them to ' talk ' to each other etc...
you need to modify the constructor of the other forms ( not the main form you open, just the others )
eg:
VB.NET:
[FONT=Courier New]'/// modify the constructor of the other form ( in this case Form2 )[/FONT]
[SIZE=2][COLOR=#0000ff][FONT=Courier New]Public [/FONT][/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Form2[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]   Inherits[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] System.Windows.Forms.Form[/FONT]
[FONT=Courier New]   #[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]Region[/COLOR][/SIZE][SIZE=2] " Windows Form Designer generated code "[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]   [COLOR=seagreen]'/// this will allow you to access whichever form called you ( eg : Form1 )[/COLOR][/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]   Private[/COLOR][/SIZE][SIZE=2] frm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Form[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]   Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Form)[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]       MyBase[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New].New()[/FONT]
[FONT=Courier New]       frm = f[/FONT]
[/SIZE][SIZE=2][COLOR=#008000][FONT=Courier New]       'This call is required by the Windows Form Designer.[/FONT]
[/COLOR][/SIZE][SIZE=2][FONT=Courier New]       InitializeComponent()[/FONT]
[/SIZE][SIZE=2][COLOR=#008000][FONT=Courier New]       'Add any initialization after the InitializeComponent() call[/FONT]
[/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=seagreen]   '/// All the rest of the Form2 construction would be here ...[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=seagreen]   '/// then to access Form1 ( from a button_click in this case ) ...[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#0000ff]   Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Button1.Click[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]       [COLOR=seagreen]'/// get hold of the form that called you, in this case Form1, so we make a direct cast of it[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]       Dim[/COLOR][/SIZE][SIZE=2] theForm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form1 = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New](frm, Form1)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]       [COLOR=seagreen]'/// set the  caption of the calling Form, to something, so you can see what happens ...[/COLOR][/FONT]
[FONT=Courier New]       theForm.Text = "*** testing 123 ***"[/FONT]
 
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/FONT][/SIZE][/FONT]
to call / create the Form2 from your opening form ( Form1 ) ...
VB.NET:
[FONT=Courier New][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Button1.Click[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] frm2 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form2([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New])[/FONT]
[FONT=Courier New]   frm2.Show()[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/FONT]
 
dynamic sysop said:
Hi Dan :)
if you wanna open a form from your main form ( eg : open Form2 from Form1 ) & them to ' talk ' to each other etc...
you need to modify the constructor of the other forms ( not the main form you open, just the others )
eg:
VB.NET:
[FONT=Courier New]'/// modify the constructor of the other form ( in this case Form2 )[/FONT]
[SIZE=2][COLOR=#0000ff][FONT=Courier New]Public [/FONT][/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Form2[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Inherits[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] System.Windows.Forms.Form[/FONT]
[FONT=Courier New]  #[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]Region[/COLOR][/SIZE][SIZE=2] " Windows Form Designer generated code "[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]  [COLOR=seagreen]'/// this will allow you to access whichever form called you ( eg : Form1 )[/COLOR][/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Private[/COLOR][/SIZE][SIZE=2] frm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Form[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Form)[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]      MyBase[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New].New()[/FONT]
[FONT=Courier New]      frm = f[/FONT]
[/SIZE][SIZE=2][COLOR=#008000][FONT=Courier New]      'This call is required by the Windows Form Designer.[/FONT]
[/COLOR][/SIZE][SIZE=2][FONT=Courier New]      InitializeComponent()[/FONT]
[/SIZE][SIZE=2][COLOR=#008000][FONT=Courier New]      'Add any initialization after the InitializeComponent() call[/FONT]
[/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=seagreen]  '/// All the rest of the Form2 construction would be here ...[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=seagreen]  '/// then to access Form1 ( from a button_click in this case ) ...[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Button1.Click[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]      [COLOR=seagreen]'/// get hold of the form that called you, in this case Form1, so we make a direct cast of it[/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]      Dim[/COLOR][/SIZE][SIZE=2] theForm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form1 = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New](frm, Form1)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]      [COLOR=seagreen]'/// set the  caption of the calling Form, to something, so you can see what happens ...[/COLOR][/FONT]
[FONT=Courier New]      theForm.Text = "*** testing 123 ***"[/FONT]
 
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/FONT][/SIZE][/FONT]
to call / create the Form2 from your opening form ( Form1 ) ...
VB.NET:
[FONT=Courier New][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] Button1.Click[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] frm2 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form2([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New])[/FONT]
[FONT=Courier New]  frm2.Show()[/FONT]
[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/FONT]

Just as an added footnote. It would be better to overload the contructor rather than change the existing one. You could use it either way then.
 
Back
Top