I am working on updating a WinForms app to incorporate a custom user control. The old version of the app had a Tab Control with a fixed number of tabs. Each tab was used for entering in pages of similar data. Each tab page had a datagridview, textbox, button, picturebox and some labels. The improvement I am making is to no longer have a limitation on the number of tab pages. With this in mind, I created a user control that includes the same controls that were on my original tab. When the user wants to add an additional tab page, they can hit a button and a new tab is generated in the tab control with the new user control embedded on the page. All this logic is working properly at the moment.
My old version of the app had some custom "dirty" events to keep track of changes that were made by the user. When changes where done to the controls, the event would fire and set a boolean variable called Is_Dirty = true. I then used this variable to make sure the user saved their work. This all worked great.
Now that I'm using a user control, I am having trouble trying to figure out how to implement this same logic with the unknown number of user controls that I may be present. In the past, I added all my events at Form load but this won't work anymore because new tab pages can be added at anytime by the user. I was thinking of creating an Is_dirty boolean property in my User Control that keep track of each user control. I think this would work but I would have to loop through all the tabs to see if any were dirty or not. I have to think there is an easier/better way to handle this scenario but I can't find anything similar. Here is the code I used prior to implementing the user control.
What I have tried:
Private Sub AddDirtyEvent(ByVal ctrl As Control)
For Each c As Control In ctrl.Controls
If TypeOf c Is TextBox Then
Dim tb As TextBox = CType(c, TextBox)
AddHandler tb.TextChanged, AddressOf SetIsDirty
End If
If TypeOf c Is ComboBox Then
Dim cb As ComboBox = CType(c, ComboBox)
AddHandler cb.SelectedIndexChanged, AddressOf SetIsDirty
End If
If TypeOf c Is DataGridView Then
Dim DGV As DataGridView = CType(c, DataGridView)
AddHandler DGV.CellEndEdit, AddressOf SetIsDirtyForDGVCellEvent
AddHandler DGV.RowsAdded, AddressOf SetisDirtyForDGVRowsAddedEvent
AddHandler DGV.RowsRemoved, AddressOf SetisDirtyForDGVRowsRemovedEvent
End If
If c.Controls.Count > 0 Then
AddDirtyEvent(c)
End If
Next
End Sub
Private Sub SetIsDirty(ByVal sender As System.Object, ByVal e As System.EventArgs)
Is_Dirty = True
End Sub
Private Sub SetIsDirtyForDGVCellEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Is_Dirty = True
End Sub
Private Sub SetisDirtyForDGVRowsAddedEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs)
Is_Dirty = True
End Sub
Private Sub SetisDirtyForDGVRowsRemovedEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs)
Is_Dirty = True
End Sub