Where Did I Come From?

Where Did I Come From?

Passing values between forms is one of the trickiest bits of a multi form application for new VB .Net programmers, but that’s partly because there are so many ways to handle it.  When a form needs to know where it came from, there is a property of the form that can provide just that information, if it’s set up properly.

The property that can be used to get a reference to the form’s creator is Owner.  This can be set up as a reference to the form that created the property at the time that the form is instantiated.  When code in the form executes, it can use the Owner property to get a reference to that creating form.  When the form is instantiated and displayed, use code like this in the creating form:

 Dim frm as Form2 = New Form2
 frm.Owner = Me
 frm.Show

Within Form2 (the form being created), a reference to the calling form can be obtained like this:

 Dim MyParent as Form1
 MyParent = DirectCast(Me.Owner,Form1)

and the variable MyParent can be used to update controls on the parent or to pass values back to properties of the parent.

 MyParent.TextBox1.Text = EmployeeName
 MyParent.ChildStatus = 1

Controls are declared Friend by default, so Textbox1 is accessible from the child form.  In this example, ChildStatus would be a Public (or Friend) field or property of Form1.

This process would not be considered very robust, as it requires both forms to be aware of details that ought to be internal to the other form.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s