Passing data between forms in VB.Net
This page provides a tutorial example for communications between Forms in Visual Basic.Net.
Today I read a question with a nice challenge. The writer of the post has a listbox on one form. If the user clicks on an item in the list, a second form must be filled with some record with detail information about the item clicked. The question was, how to tell the second form which item was clicked on the first.
Visual Basic and objects - steps to the solution
The steps to the solution I use for these situation are the following:
- Visual Basic.Net works with classes.
- Classes can have properties, methods and events. Every object that owns an instance of that class can use the Public (or Friend) properties and methods.
- A form is also an class, defined by Visual Basic.
- But as a programmer you are allowed to create extra public (and private, and friend) properties, methods and events for a predefined classes, also for the form class.
- If one form must receive information from another form, you can use this principle.
The solution - define the right methods for passing data between forms
Let's call the form, which must have the power to send information to the other form, the Primary Form. The other form is the Secundary Form, which shows information depending on the situation or status of the Primary Form.
The Primary Form is responsible for the existence of the Secundary form. This can be done by defining a variable of type "SecundaryFormType". Before actually showing the Secundary Form, the Primary Form can set properties or call methods. In these functions, defined on the Secondary Form, all the neccessary code can be handled (retrieving records, loading images, etc.).
Example of passing data between forms in Visual Basic.net
In the following example I use two forms: class PrimaryForm and class SecondaryForm. On the PrimaryForm I have three radiobuttons: rb1, rb2 and rb3. On the SecondaryForm I have two labels, one is called lblOptionPrimary.
If you click on one of the radio buttons, lblOptionPrimary will display which radio button is clicked. Here you can see how it looks:

Code for the Primary Form: Sending data to another form
Public Class PrimaryForm
Private MySecundaryForm As SecundairForm
Private Sub rb1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rb1.CheckedChanged
ShowSecundair(1)
End Sub
Private Sub rb2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rb2.CheckedChanged
ShowSecundair(2)
End Sub
Private Sub rb3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rb3.CheckedChanged
ShowSecundair(3)
End Sub
Private Sub ShowSecundair(ByVal nr As Integer)
'Handle the action for all radio buttons
'Make sure the Secundary form is loaded If MySecundaryForm Is Nothing Then MySecundaryForm = New SecundairForm MySecundaryForm.Show() End If
'Check if secundary form is closed by user If MySecundaryForm.IsHandleCreated = False Then MySecundaryForm = New SecundairForm End If
'Sub ShowPrimaryOption shows choosen option MySecundaryForm.ShowOption = nr
'Sub SetPosition positions the SecundaryForm on some position on the screen 'In this example, it will be placed just beneath the primary form MySecundaryForm.SetPosition(Me.Left, Me.Top + Me.Height)
'At last, show the form MySecundaryForm.Show()
End Sub
End Class
Code Secundary Form: receiving data from another form
Public Class SecundairForm
Private MyOption As Integer 'Private variable, not accessible from outside
Private Sub UpdateOptionPrimary()
'Update text which option is pressed on the primary form
Select Case MyOption Case 1 lblOptionPrimary.Text = "First" Case 2 lblOptionPrimary.Text = "Second" Case 3 lblOptionPrimary.Text = "Third" End Select
End Sub
Public Property ShowOption() As Integer 'Sets or Gets the Private variable, which option must be shown
Get ShowOption = MyOption End Get Set(ByVal nr As Integer) MyOption = nr UpdateOptionPrimary() End Set
End Property
Public Sub SetPosition(ByVal x As Integer, ByVal y As Integer)
'Move the form to some position Me.Top = y Me.Left = x
End Sub
End Class
What if there is no strict relation between the two forms?
If the two forms don't have such a strict relationship between each other as in this example, you have to figure out something more complicated.
Communication between forms - Several forms to send data, one data receiving form
If, for example, you have several "Primary Forms", and just one Secundary Form, you can use a Secondary form which is defined as Global on the application level. All objects can send information to this global form to show it's data.
Communication between forms - Several forms to send data, several data receiving form
If you got yourself into real trouble, you could have several sending forms and several receiving forms. I can't figure out a practical application for this, but well... let me know...
Visual Basic can handle this as well, at least, I can make up some solution. In this case you have to define a Public Function/Sub in a module, which can do the administration. If the secondary form used is depending on the data or its type, let the public function examine which form has to be used.
|