Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Passing Values Between Forms  »  Passing Values from Sibling to Sibling
 »  Home  »  .NET Newbie  »  Passing Values Between Forms  »  Passing Values from Sibling to Sibling
 »  Home  »  Windows Development  »  Passing Values Between Forms  »  Passing Values from Sibling to Sibling
 »  Home  »  Windows Development  »  Win Forms  »  Passing Values Between Forms  »  Passing Values from Sibling to Sibling
Passing Values Between Forms
by Colin Mackay | Published  04/07/2007 | .NET Framework .NET Newbie Windows Development Win Forms | Rating:
Colin Mackay

Originally from Edinburgh, Scotland, I have worked with Microsoft Visual C++ since about Version 2.1. I have been playing with the .NET Framework and C# since it was in beta but have been using it commercially since late 2002.

I originally started programming when I was about 9 years old, on a Sinclair ZX Spectrum with an amazing 48K memory! Natuarally I went for a computing degree. After leaving university I co-founded a company that developed a GIS product but am now working for an insurance company in Glasgow.

I am very active in the user community. I am a core member of Scottish Developers, and am responsible for arranging developer events in Glasgow and the west of Scotland. I am also a Member of the British Computer Society, a Code Project MVP 3 years running and now a Microsoft MVP.

Outside of software development, I am also the Vice President of Education of the Edinburgh Branch of Toastmasters International, an occasional cyclist and amature photographer.


View all articles by Colin Mackay...
Passing Values from Sibling to Sibling

The next most common request seems to be passing a value between two siblings. That is, two child classes sharing the same parent class. This also demonstrates passing between the child and the parent.

First, it is important to note that in most cases the two child classes should probably know nothing about each other. If the two child classes start to communicate directly with each other the coupling in the application increases and this will make maintenance of the application more difficult. Therefore the first thing to do is to allow the child classes to raise events when they change.

In order to raise events, create a delegate to define the signature of the method that will be called when the event is raised. It is a good practice to conform to the same style as the delegates used in the .NET Framework itself. For example, if an event handler for the Click event of a Button is created then it is possible to see that the signature of the method created by the IDE has two parameters, sender and e. sender is an object and is a reference to the object that generated the event. e is the EventArgs (or a something derived from EventArgs).

With that in mind, the delegate created here will have sender and e parameters as well. But for the e parameter a new class will be created to store the specific details of the event.

Public Class ValueUpdatedEventArgs
    Inherits System.EventArgs
    ' Stores the new value
    Dim _newValue As String
    ' Create a new instance of the 
    ' ValueUpdatedEventArgs class.
    ' newValue represents the change to the value.
    Public Sub New(ByVal newValue As String)
        Me._newValue = newValue
    End Sub
    ' Gets the updated value
    ReadOnly Property NewValue() As String
            Return Me._newValue
        End Get
    End Property
End Class

Next the delegate needs to be defined.

Public Delegate Sub ValueUpdated(ByVal sender As Object, 
    ByVal e As ValueUpdatedEventArgs)

Now that the foundation of the mechanism for informing other objects of changes is written, the child classes can implement the raising of events so that observers (i.e. interested parties) can be notified when the value changes.

Next, when the child class is updated it needs to inform its observers (the interested parties, in this case it will be the parent and sibling forms). The following code shows what happens when the text is changed on the form.

Private Sub uxMyValue_TextChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles uxMyValue.TextChanged
    Dim newValue As String
    newValue = Me.uxMyValue.Text
    Dim valueArgs As ValueUpdatedEventArgs
    valueArgs = New ValueUpdatedEventArgs(newValue)
    RaiseEvent ValueUpdated(Me, valueArgs)
End Sub

Now that the child form can inform others of the changes and what the changes are, the parent needs to open the child form and register as an observer so that it can be informed when changes are made. The parent will also ensure that other siblings that are interested get added as observers too.

Private Sub uxOpenChildA_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles uxOpenChildA.Click
    ' Create the child form
    Me.childA = New ChildAForm
    ' Add a handler to this class for when child A is updated
    AddHandler childA.ValueUpdated, AddressOf ChildAValueUpdated
    If Not Me.childB Is Nothing Then
        ' Make sure that the siblings are informed of each other.
        AddHandler childA.ValueUpdated, _
            AddressOf childB.ChildAValueUpdated
        AddHandler childB.ValueUpdated, _
            AddressOf childA.ChildBValueUpdated
    End If
    ' Show the form
End Sub

All that remains is to implement the event handlers.

Private Sub ChildAValueUpdated(ByVal sender As Object, 
        ByVal e As ValueUpdatedEventArgs) Handles childA.ValueUpdated
    ' Update the value on this form (the parent) with the 
    ' value from the child.
    Me.uxChildAValue.Text = e.NewValue
End Sub

And similarly in the child class.

Public Sub ChildBValueUpdated(ByVal sender As Object, _
        ByVal e As ValueUpdatedEventArgs)
    Me.uxOtherValue.Text = e.NewValue
End Sub

The code for this example is in the accompanying zip file. The project is called SiblingToSiblingCS (for the C# version) and SiblingToSiblinVB (for the VB.NET version)

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.1 out of 5
 90 people have rated this page
Article Score34470
Sponsored Links