Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
Private Sub AddDirtyEvent(ByVal ctrl As Control)

        'Add an event to keep track of when changes are made to the controls on the form

        For Each c As Control In ctrl.Controls
            'MsgBox(c.ToString)
            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
Posted
Updated 28-Jan-22 8:14am
v2
Comments
[no name] 28-Jan-22 14:37pm    
Create a static (global) dirty switch / method that the user controls can set / call. You're not clear on whether you save "all" tabs if any one is changed.
PJ Arends 28-Jan-22 15:23pm    
What I have done in the past is simply enumerate all child controls and do a check sum of the data they contain. Grab and save a checksum at the start. If any controls are added or removed, or any data is changed then the checksum will be different. If the user undoes any changes then the checksum will not have changed.
theskiguy 1-Feb-22 15:34pm    
Thanks for the ideas but I decided to create a function to loop through the Is_dirty variables in each user control on each tab to see if any are dirty. It is working good at the moment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900