仅在启用保存按钮时更改控件值 [英] Enable save button Only When the controls values are changed

查看:64
本文介绍了仅在启用保存按钮时更改控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有一个应用程序,其中A网格用于绑定公司详细信息和复选框以及相同形式的文本框,用于绑定网格选定行的这些值。
当从一行导航到网格中的另一行时
。如果对文本框或复选框进行任何更改,我们应该启用保存按钮并显示保存更改之类的消息。

i写道foreach循环中的事件


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

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




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



这里我的问题是如果选中复选框然后按钮启用。但是当取消选中复选框时它应该禁用(即没有做出任何更改)

有没有办法得到这个...

i我没有使用EDMX。


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.

推荐答案

很明显,添加这些事件处理程序是不够的。如果您考虑一下,您应该看到系统应该记住所有控件的初始状态,并在某些内容发生变化时启用该按钮,但是如果更改进入控制状态,该状态恰好变为完全相同的状态。一开始,应该再次禁用该按钮。



因此,您需要为此创建某种机制,以便记住状态并将其与当前状态进行比较。我建议双重使用你需要的机制,即使没有这个功能:从UI获取数据到某些数据结构。



换句话说,即使是如果您不打算创建此功能,则需要使用一些描述纯数据并从UI中抽象出来的数据模型。您可以使用此数据填充某些控件集,然后允许用户操作控件来修改数据,然后读取控件状态以获取更新的数据。首先,即使您没有初始数据,也应该存在人口步骤;在这种情况下,它仍然是一些默认数据集。



因此,要解决您的问题,请在所有事件处理程序中使用此数据更新步骤,即修改控制状态的每个事件。编辑前请记住数据。在每个控制步骤修改中,从控件获取数据并与初始数据进行比较。这样,您可以将控件状态的比较替换为数据结构的比较。这个抽象将帮助您重用代码并更好地将UI与代码的其他方面分开。



我建议将所有与此操作相关的数据封装在一个单个数据类并为此数据类型定义相等运算符。它会让你的代码变得更优雅。



-SA
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


这篇关于仅在启用保存按钮时更改控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆