Code for the object
Public Class formObject
Public Name As String
Public Type As String
Public Control As Control
Public Tag As String = ""
Sub New(ByVal name As String, ByVal type As String, ByVal control As Control, ByVal tag As String)
Me.Name = name
Me.Type = type
Me.Control = control
If Not String.IsNullOrEmpty(tag) Then
Me.Tag = tag
End If
End Sub
End Class
'at the form level:
Dim formObjects As New List(Of formObject)
Dim bDirty As Boolean
Sub populateFormObjectsCollection()
Dim o As formObject
'load the form fields into a collection
For Each c As Control In Me.Controls
Select Case Microsoft.VisualBasic.Left(c.Name, 3).ToUpper
Case "TXT", "DDL", "BTN"
o = New formObject(c.Name, Microsoft.VisualBasic.Left(c.Name, 3).ToUpper, c, c.Tag)
formObjects.Add(o)
End Select
Next
For Each o In formObjects
Select Case o.Tag.ToUpper
Case "KEY"
Case "NOENABLE"
Case Else
AddHandler o.Control.TextChanged, AddressOf TextChangedEvent
AddHandler o.Control.Validating, AddressOf ValidatingEvent
End Select
Next
End Sub
Code for the event handler
Private Sub TextChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)
Select Case Me.FormState
Case formStateType.clean, formStateType.populated
bDirty = True
Me.txtStatus1.Text = "Dirty"
End Select
End Sub
Private Sub ValidatingEvent(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
If bDirty Then
'the user has changed the value in a field other than Product Number
Select Case Me.FormState
Case formStateType.loading
Exit Sub
Case formStateType.clean
globalMessageBox("Please choose a Project first")
Case formStateType.populated
setFormState(formStateType.dirty)
Case formStateType.dirty
End Select
bDirty = False
End If
End Sub