School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,380 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,537 people online right now. Registration is fast and FREE... Join Now!




Passing parameters to forms while creating them.

 
Reply to this topicStart new topic

> Passing parameters to forms while creating them., Utilizing the constructor of a form (updated)

Rating  4
jens
Group Icon



post 5 Mar, 2009 - 01:46 PM
Post #1


Hi!

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 smile.gif 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.

Complicated?
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.
vb

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.
CODE

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).

Regards
/Jens

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... wink2.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

AdamSpeight2008
Group Icon



post 5 Mar, 2009 - 08:13 PM
Post #2
Another great tutorial. icon_up.gif
FYI
Another way to define the constructor with/without parameters is to use optional
CODE

Public Sub New (Optional Value As String=Nothing)
' I tend to use Nothing as the default value if its not present.
' Remember its different from "" as "" be a value input.
' So remember to check.


This post has been edited by AdamSpeight2008: 5 Mar, 2009 - 08:13 PM
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 09:30PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month