BobSki
BobSki

Reputation: 1552

Validate if any changes were made in TabPage

So I'm wondering if someone can give me a good idea how to do something. I have a tabControl in my application - I load up a page - TabPage1 - with about 25-30 fields. On loading all the data - i run a loop to save each control value to .tag. I also have a function called isDirty() that basically checks each control's ctr.tag.tostring <> ctr.text. I'm having a hard time figuring out how to build a quick handler to check all controls on the form. I tried using TagPage1.Validating, however that doesn't do anything.

my isDirty() function looks like this...

    Private Function isDirty() As Boolean

    isDirty = False

    For Each ctr As Control In TabPage1.Controls
        If TypeOf ctr Is TextBox And ctr.Enabled = True Then
            If ctr.Tag.ToString <> ctr.Text Then
                isDirty = True
            End If
        End If
        'more if statements for comboboxes and such
      Next
    End Function

I'm hoping to be able to plug this function in somewhere and do a CALL on it something like

 if isDirty() then
    MsgBox "You have made a change to this form"
 End if

Do i have to call this on each control's selection changed?

Upvotes: 0

Views: 206

Answers (1)

Mukul Varshney
Mukul Varshney

Reputation: 3141

There are 2 approaches

  1. When most of the controls do not have change event handler, your approach seems to be correct. By the way, in your above code you missed Exit For, after isDirty = True.
  2. When most of the controls have changed event handler implemented(this will be the case if in your design you have data models/objects), rather than checking changes in a loop, I would prefer to check when the change is made. I mean to say declare a form level variable bool changed. Controls like textbox, combobox, radiobutton, checkbox, etc.. mostly have their change event handlers, if the change is made set changed to True.

Upvotes: 1

Related Questions