Ever wanted to pass data to a new form while it's opening? E.g. if you want some variables or text in the new form to be initialized when you do: theForm.Show(). Here's how to do it!
Use the constructor in the second form!
The constructor is a special sub named new, like this Public Sub New which runs whenever an object is created from a class. Since a form is a class it has a constructor and you should use the constructor to initialize your object - in this case a form.
You might object that you never saw a constructor in a form. That's because VB is nice enough to make you one - without showing it. VB creates a default no argument constructor for every class you make, including forms. Since it's a no-arg-constructor it doesn't need arguments and you don't even have to call it when you create a form. That's because VB is nice enough to do that for you when you use the syntax Form2.Show().
Other classes on the other hand does need that you call their constructors - even if you don't actually see it. As an example we could look at creating a List (it's a kind of collection, like an array) e.g: Dim aList As New List(Of Integer) or on two rows Dim aList As List(Of Integer) and aList = New List(Of Integer).
All in all, there's a sub called New in every class, including forms - even if you don't see it - it is the constructor of the class and it is run first of all when an object is created from the class. You can use this knowledge and make another constructor New that do take parameters and use the parameters to initialize your object.
Please note that when you create a constructor (that takes parameters) the old invisible constructor disappears! If you also want a no argument constructor you'll have to make one in this case.
Not really - look at the example below.
Create a new project consisting of two forms (Form1 and Form2)
Form 1 consists of a form with 3 buttons on it (Button1, Button2 and Button3).
Form 2 consists of a form with 1 textbox on it (TextBox1).
Here's code for Form1, replace everything in the forms code with this.
Option Explicit On Option Strict On Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'The "good old way" '(Actually this uses the no-args-constructor too, but that's another story.) Form2.Show() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Using a no-args-constructor, i.e the "Public Sub New()" Dim aForm As New Form2 aForm.Show() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'Using a constructor that takes arguments, i.e the "Public Sub New(ByVal theText As String)" 'Doing this on two lines to emphasize the use of New. Dim anotherForm As Form2 anotherForm = New Form2("Hello there!") anotherForm.Show() End Sub End Class
Here's code for Form2, replace everything in the forms code with this.
Option Explicit On Option Strict On Public Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TextBox1.Text="Text from form load" End Sub Public Sub New() 'Call to MyBase.New must be the very first in a constructor. MyBase.New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. TextBox1.Text = "New without parameters" End Sub Public Sub New(ByVal theText As String) 'Call to MyBase.New must be the very first in a constructor. MyBase.New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. TextBox1.Text = theText End Sub End Class
Clicking Button1 is the good old way of doing things. Nothing strange here, right.
Clicking Button2 uses the no argument constructor (the Public Sub New()) in Form2 which you can see because the () after the New is empty (it has no arguments).
Clicking Button3 uses the constructor that utilizes arguments. You'll notice that whatever you write in the call will show up in the textbox.
Now, I told you that the constructor is run first of all. Well, what about Form2_Load you might think. Try un commenting the line TextBox1.Text="Text from form load" and run the program again...
I hope this explains how to pass parameters to a form. I hope this also explains a little on constructors and the special sub "New" and why you have to write ...As New... when you Dim some things.
This leaves only to comment on the statement MyBase.New() that I use.
This is not strictly necessary because VB is nice enough to do that for you. I do it in order to keep track of what's happening. If you omit it it is done implicitly for you. What it does is that it calls the constructor (the sub "New") of the base class of your form. Since your form is a special case of the class "Form" ("Form" is the base class that you inherit your form from) you can use the constructor from the class "Form" to take care of initializing the parts of your form that are common to all forms. (Hmmm... This was probably not very clear but never mind, you can use MyBase.New() or not).
PS: The statement "because VB is nice enough to do that for you" used at a few places in this text should be interpreted as a slight irony...