Event Question

  • Thread starter Thread starter Tim Gallivan
  • Start date Start date
T

Tim Gallivan

Hello,

I have a question: There is a form (Form1) with a text box, which is used
for logging/display purposes. When the project starts up, Form1 starts Class
A on it's own thread. When a timer in Class A elapses, it creates an
instance of Class B, which, in turn will create several instances of Class
C. Each of classes A, B and C need to report back to Form1.

In my design, when Class C (for example) raises an event, it is handled by
B, which raises an event handled by Class A, which raises an event which is
handled by Form1. Although it works, I'm certain I must be violating some
OOP principle here, or at least, I'm re-inventing the wheel.

Could someone please point me at the correct way of "bubbling" the event in
Class C back to Form1. Your time is greatly appreciated.

Thanks in advance,
Tim
 
I believe I've answered my own question, but if this is not the way, please
reply:

Public Class Form1
Inherits System.Windows.Forms.Form

Implements IHandleC1Lib
Implements IHandleC2Lib

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
Dim one As New C1(Me)
one.StartC1()

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TextBox1.Location = New System.Drawing.Point(0, 0)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(292, 273)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub ShowMsg(ByVal sMsg As String)
TextBox1.AppendText(sMsg & vbCrLf)
End Sub

Public Sub AddHandlers(ByVal cl As C1) Implements
IHandleC1Lib.AddHandlers
AddHandler cl.Message, AddressOf ShowMsg
End Sub

Public Sub RemoveHandlers(ByVal cl As C1) Implements
IHandleC1Lib.RemoveHandlers
RemoveHandler cl.Message, AddressOf ShowMsg
End Sub

Public Sub AddHandlers(ByVal cl As C2) Implements
IHandleC2Lib.AddHandlers
AddHandler cl.Message, AddressOf ShowMsg
End Sub

Public Sub RemoveHandlers(ByVal cl As C2) Implements
IHandleC2Lib.RemoveHandlers
RemoveHandler cl.Message, AddressOf ShowMsg
End Sub
End Class

Public Class C1

Private m_handler As IHandleC1Lib
Public Delegate Sub MessageToFormEventHandler(ByVal sMessage As String)
Public Event Message As MessageToFormEventHandler

Public Sub New(ByVal handler As IHandleC1Lib)
m_handler = handler
m_handler.AddHandlers(Me)
RaiseEvent Message("C1 created ...")
End Sub

Public Sub StartC1()
For iCt As Int16 = 0 To 4
RaiseEvent Message("C1 creating C2 ...")
Dim two As New C2(m_handler)

Next

m_handler.RemoveHandlers(Me)
End Sub
End Class

Public Class C2

Private m_handler As IHandleC2Lib
Public Delegate Sub MessageToFormEventHandler(ByVal sMessage As String)
Public Event Message As MessageToFormEventHandler

Public Sub New(ByVal handler As IHandleC2Lib)
m_handler = handler
m_handler.AddHandlers(Me)
RaiseEvent Message("C2 created ...")
End Sub

Public Sub StartC2()
RaiseEvent Message("New C2 started ...")
m_handler.RemoveHandlers(Me)
End Sub
End Class

Public Interface IHandleC1Lib

Sub AddHandlers(ByVal cl As C1)
Sub RemoveHandlers(ByVal cl As C1)

End Interface

Public Interface IHandleC2Lib

Sub AddHandlers(ByVal cl As C2)
Sub RemoveHandlers(ByVal cl As C2)

End Interface
 
Back
Top