locked
Notifying parent form from child form - Generalised RRS feed

  • Question

  • Take a scenario where I have three forms - A, B and C. Each of these forms has a text box (could have more than one) There is another form X which has a button. The form X can be called from A, B or C.

    Now, when I call form X from, say A, form X gets displayed. When I click the button X, a value needs to be written on a textbox of the calling form, i.e. A. Similarly, if B calls X, then on click on the button in X, a value should be written in the textbox of B.

    How do I go about doing this? I want it to be generalised, and shouldn't be restricted to just three forms. Should I be passing calling form name and text box name into which it should be updated? What do I do after this?
    Wednesday, March 3, 2010 5:50 PM

Answers

  • If you want it to be "generalized", then you should design the implementation on an interface, not a class.
    Use events in the child forms to notify the parent.
    Use properties in the child forms for the parent to notify children.

    Public Delegate Sub MessageDelegate(ByVal sender As Object, ByVal e As MessageEventArgs)
    
    Public Class MessageEventArgs : Inherits EventArgs
        Public Message As String
        Public Sub New(ByVal msg As String)
            Me.Message = msg
        End Sub
    End Class
    
    Public Interface IController
        Event ControllerEvent As MessageDelegate
        Sub FireControllerEvent(ByVal msg As String)
    End Interface
    
    Public Class Controller : Implements IController
        Public Event ControllerEvent As MessageDelegate Implements IController.ControllerEvent
        Public Sub FireControllerEvent(ByVal msg As String) Implements IController.FireControllerEvent
            Me.OnControllerEvent(New MessageEventArgs(msg))
        End Sub
        Protected Overridable Sub OnControllerEvent(ByVal e As MessageEventArgs)
            RaiseEvent ControllerEvent(Me, e)
        End Sub
    End Class
    
    '*************************************************************************'
    
    Public Class Form1 'this class is the master form
        Private msgController As IController
        Private msgValue As Integer = 0
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.msgController = New Controller()
            AddHandler msgController.ControllerEvent, AddressOf msgController_ControllerEvent
            Dim childForm As New Form2(Me.msgController)
            childForm.Show()
        End Sub
    
        Private Sub msgController_ControllerEvent(ByVal sender As Object, ByVal e As MessageEventArgs)
            Me.msgValue += 1
            Me.Text = e.Message & " " & Me.msgValue
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.msgController.FireControllerEvent("Form1 fired the event. ")
        End Sub
    
        Private Sub Me_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
            RemoveHandler msgController.ControllerEvent, AddressOf msgController_ControllerEvent
        End Sub
    
    End Class
    
    '*************************************************************************'
    
    Public Class Form2 'this class is the child form
        Private msgController As IController
        Private msgValue As Integer = 0
        Public Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
        End Sub
    
        Public Sub New(ByVal controller As IController)
            Me.New()
            Me.msgController = controller
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.msgController.FireControllerEvent("Form2 fired the event. ")
        End Sub
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler msgController.ControllerEvent, AddressOf msgController_ControllerEvent
        End Sub
    
        Private Sub msgController_ControllerEvent(ByVal sender As Object, ByVal e As MessageEventArgs)
            Me.msgValue += 1
            Me.Text = e.Message & " " & Me.msgValue.ToString()
        End Sub
    
        Private Sub Me_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
            RemoveHandler msgController.ControllerEvent, AddressOf msgController_ControllerEvent
        End Sub
    End Class
    
    '*************************************************************************'
    
    
    



    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 3, 2010 5:57 PM