Passing a value from a parent to a child class is hopefully one of the easiest techniques and is the one I'll start with. The idea is simple. The parent class needs to pass some value to the child class. In this example, I have set up a simple parent form with a TextBox control where the user can type any value they like, then they can open the child form and see that value appear there.
First of all, the child class needs some way to receive the value from the parent. I've set up a property to receive the value and it will set the Text value of a TextBox on the child form. In reality this could set a field value or get passed to another object and so on.
Public WriteOnly Property ValueFromParent() As String
Set(ByVal Value As String)
Me.uxParentValue.Text = Value
End Set
End Property
Passing the value on construction
To make the initialization of the class easier, the constructor will take a parameter which will be the current value to be passed. The constructor uses the property so that the code can be reused. Admittedly, in this example there is only one line in the property, but as an element of future proofing it means that there is the possibility of adding more functionality to the property in future should it need. That additional functionality will be implemented without having to change a lot of existing code.
Public Sub New(ByVal initialValue As String)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
ValueFromParent = initialValue
End Sub
Now, in order for the parent to pass the value to the child, it only has to set the property. For example, when using the child form as a dialog the value from the parent can be passed as follows:
Private Sub uxOpenDialog_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles uxOpenDialog.Click
' Get the value that the child will be initialised with
Dim initialValue As String
initialValue = Me.uxUserResponse.Text
Dim childForm As ChildForm
' Create the child form.
childForm = New ChildForm(initialValue)
' Show the child dialog.
childForm.ShowDialog()
End Sub
Passing the value while the form is open
Of course, it may be necessary to pass a value when the child form is already open. This is also possible if the parent form stores a reference to the child form and then uses that to pass updates to the child form using the property in the child form that was set up earlier.
In my example, the parent is going to store a reference to the child as a field in the ParentForm class like this:
Private uxChildForm As ChildForm
When creating the child form it is therefore important to pass the reference to the child form into the field variable. For example:
Private Sub uxOpenForm_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles uxOpenForm.Click
Me.uxChildForm = New ChildForm
Me.uxChildForm.ValueFromParent = Me.uxUserResponse.Text
Me.uxChildForm.Show()
End Sub
Finally, whenever the parent needs to tell the child that the value is updated it can do so just by passing the new value to the property that was set up in the child form. For example:
Private Sub uxUserResponse_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles uxUserResponse.TextChanged
If Not Me.uxChildForm Is Nothing Then
Me.uxChildForm.ValueFromParent = Me.uxUserResponse.Text
End If
End Sub
The code for this is in the uploaded zip file. The projects are ParentToChildCS (for C# developers) and ParentToChildVB (for VB developers).