Figure 1 Class with Shared PropertiesPublic Class DataClass
Private privateMyData As String = ""
Private privateMoreData As String = ""
Public Event MyDataChanged As EventHandler
Public Event MoreDataChanged As EventHandler
Public Property MyData() As String
Get
Return privateMyData
End Get
Set(ByVal Value As String)
privateMyData = Value
RaiseEvent MyDataChanged(Me, New EventArgs)
End Set
End Property
Public Property MoreData() As String
Get
Return privateMoreData
End Get
Set(ByVal Value As String)
privateMoreData = Value
RaiseEvent MoreDataChanged(Me, New EventArgs)
End Set
End Property
Public Sub Reset()
MyData = ""
MoreData = ""
End Sub
End Class
Figure 3 modGeneral Module modGeneral
Friend DataStuff As DataClass
Friend MyForms As New Collection
Friend localNextData As String
Sub AddForm(ByVal ThisForm As Form)
MyForms.Add(ThisForm, ThisForm.Name)
UpdateControlsNextData(localNextData)
End Sub
Friend Property NextData() As String
Get
Return localNextData
End Get
Set(ByVal Value As String)
localNextData = Value
UpdateControlsNextData(localNextData)
End Set
End Property
Private Sub UpdateControlsNextData(ByVal Value As String)
Dim frm As frmBase
Dim obj As Object
For Each obj In MyForms
frm = CType(obj, frmBase)
If Not IsNothing(frm.txtNextData) Then
frm.txtNextData.Text = Value
End If
Next
End Sub
End Module
Figure 4 modGeneralv2 Friend MyFormsToUpdate As New Collection
Private ControlsToUpdate() As String = _
{"txtCustomer", "txtAddress", "txtName"}
Friend Sub AddFormToUpdate(ByVal ThisForm As Form)
Dim ctrl As Control
Dim i As Integer
Dim AddThisForm As Boolean = False
Try
For Each ctrl In ThisForm.Controls
For i = 0 To ControlsToUpdate.GetUpperBound(0)
If ctrl.Name = ControlsToUpdate(i) Then
AddThisForm = True
Exit For
End If
If AddThisForm Then
MyFormsToUpdate.Add(ThisForm, ThisForm.Name)
Exit For
End If
Next
Next
If AddThisForm Then
If MyFormsToUpdate.Count > 1 Then
UpdateControlsOnAllForms( CType(MyFormsToUpdate(1), Form))
End If
End If
Catch ex As Exception
Throw New Exception( _
"AddForm generated this error: " & ex.Message, ex)
End Try
End Sub
Friend Sub UpdateControlsOnAllForms(ByVal MasterForm As Form)
Dim frm As Form
Dim ctrlMaster, ctrlClient As Control
Dim i As Integer
Try
For Each frm In MyFormsToUpdate
For Each ctrlMaster In MasterForm.Controls
For i = 0 To ControlsToUpdate.GetUpperBound(0)
If ctrlMaster.Name = ControlsToUpdate(i) Then
For Each ctrlClient In frm.Controls
If ctrlClient.Name = ControlsToUpdate(i) Then
ctrlClient.Text = ctrlMaster.Text
Exit For
End If
Next
End If
Next
Next
Next
Catch ex As Exception
Throw New Exception( _
"UpdateControlsNextData generated this error: " & ex.Message, ex)
End Try
End Sub
|