我们正在使用 vb.net/dev express 工具。我们有几个控件文本框、组合等...而不是检查每个更改的值,我们想要遍历所有控件并检查是否已编辑任何内容,然后在表单关闭时保存。下面是我尝试完成的一些代码。问题是,虽然它在技术上有效......它使用递归 AddDirtyEvent(c) ,所以当我关闭表单并单击是保存时......由于多个控件它多次调用该消息框......如果我接受out ,它将无法工作并检测脏更改。我只是想知道我怎样才能让它以我想要的方式工作,或者是否有更简单的方法......
Dim is_Dirty As Boolean = False
Private Sub AddDirtyEvent(ByVal ctrl As Control)
For Each c As Control In ctrl.Controls
If TypeOf c Is TextEdit Then
Dim tb As TextEdit = CType(c, TextEdit)
AddHandler tb.EditValueChanged, AddressOf SetIsDirty
End If
'If TypeOf c Is ComboBoxEdit Then
' Dim cb As ComboBoxEdit = CType(c, ComboBoxEdit)
' AddHandler cb.SelectedIndexChanged, AddressOf SetIsDirty
'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 Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If is_Dirty = True Then
Dim dr As DialogResult = MessageBox.Show("Do you want save changes before leaving?", "Closing Well Info", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
If dr = Windows.Forms.DialogResult.Yes Then
SimpleButtonSave.PerformClick()
Me.Close()
End If
End If
End Sub