Click here to Skip to main content
15,897,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have an application in which A grid is there for binding companies Details and checkboxes and textboxes in the same form for binding those values of selected row of the grid..
when navigating from one row to other row in grid .if there are any changes made to textboxes or checkboxes we should enable save button and show a message like "Save changes."
i wrote the events for them in foreach loop
VB
For Each gb As GroupBox In {gbBox1, gpbox2, gpBox3, gpBox4, gbbox5}
            For Each ctrl As Control In gb.Controls
                If TypeOf ctrl Is CheckBox Then
                    AddHandler CType(ctrl, CheckBox).CheckedChanged, AddressOf EnableButton
                ElseIf TypeOf ctrl Is TextBox Then
                    AddHandler CType(ctrl, TextBox).TextChanged, AddressOf EnableButton
                End If
            Next
        Next

VB
Private Sub EnableButton()
      BtnSave.Enabled = True
  End Sub

here my problem is if checkbox is checked then button is enabling. but it should disable when checkbox is unchecked (i.e no changes made )
is there any way to get this ...
i am not using EDMX.
Posted
Updated 12-Aug-13 4:47am
v2

1 solution

It is apparent that adding those event handlers is not enough. If you think about, you should see that the system should remember the initial state of all of the controls and enable the button when something is changes, but if the change comes to the state of control which happens to become exact same state as in the very beginning, the button should be disabled again.

So, you need to create certain mechanism for that, for remembering of the state and comparing it with the current state. I would advise to double-use the mechanism you would need anyway, even without this feature: getting data from UI to some data structure.

In other words, even of you did not aim to create this feature, you would need to have some data model which describes pure data and abstracted from the UI. You use this data to populate some control set, then you allow the user to manipulate control to modify the data, and then you "read" control states to get updated data. First, population step should exist even if you don't have the initial data; in this case it still would be some default data set.

So, to solve your problem, use this data update step in all your event handlers, that is, on every event modifying the control states. Remember the data before editing. On each control step modification, get the data from controls and compare with initial data. This way, you can replace comparison of the states of controls with comparison of data structures. This abstraction will help you to reuse the code and better separate UI from other aspects of the code.

I would advice to encapsulate all the data relevant to this operation in a single data class and define equality operators for this data type. It would make your code way more elegant.

—SA
 
Share this answer
 

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