Windows表单应用程序。 VB .NET 4.0在我的应用程序中,我有一个全局布尔变量,它跟踪何时进行更改以及何时保存它们名为changesSaved。在关闭时,它甚至会在关闭窗口之前检查此变量的值。我正在使用textchanged事件在文本更改时自动将changesSaved的值更改为FALSE。问题:组框项目正在动态填充,以便用户可以编辑值或只是查看它们。此动态填充导致textchanged事件触发,因为程序正在更改框的文本值以将值存储在数据库中。 textchanged事件不应该触发,除非用户他或她自己在文本框中输入一个或多个值。有没有办法指定源我想你可以说文本更改事件或其他方式,以便它只会触发当用户输入信息时。???函数如下:第一个由load事件调用,将值放在框中......下一个是由于第一个被调用的那个,也是导致问题的那个..
Private Sub loadProperty(ByVal x As Integer)
Dim _property As property_info = db.property_info.Single(Function(s) s.idProperties = x)
p_settingsCity.Text = _property.city.ToString
p_settingsState.Text = _property.state.ToString
p_settings_PropertyName.Text = _property.property_Name.ToString
p_settingsZipCode.Text = _property.zipcode.ToString
p_settings_Address.Text = _property.address1.ToString
p_settingsCity.Text = _property.city.ToString
p_settingsState.Text = _property.state.ToString
If _property.AllowRentProration = 1 Then
RentProrate.Checked = True
Else
RentProrate.Checked = False
End If
RentProrate.Visible = True
End Sub
Private Sub PropertyTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PropertyDetailsGroup.TextChanged
ChangesSaved = False
End Sub
答案 0 :(得分:2)
有几种不同的方式。
由于您只是在ChangesSaved = False
事件中执行TextChanged
,因此只需在之后将其设置为True
,然后以编程方式对其进行更新。
Private Sub loadProperty(ByVal x As Integer)
'// code
RentProrate.Visible = True
ChangesSaved = True
End Sub
或者只是删除TextChanged
处理程序,以编程方式更新TextBox,然后重新添加处理程序。
RemoveHandler PropertyDetailsGroup.TextChanged, AddressOf PropertyTextChanged
PropertyDetailsGroup.Text = "value from database"
AddHandler PropertyDetailsGroup.TextChanged, AddressOf PropertyTextChanged
实现 INotiftyPropertyChanged 接口并使用Databinding
将是另一种方式,实际上更干净。您不需要表单级别的标志,您可以捕获用户在类级别所做的任何更改,如下所示:
Public Class TestClass
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private _IsDirty As Boolean = False
Private _TextValue As String = String.Empty
Private Sub OnPropertyChanged(ByVal propertyName As String)
_IsDirty = True
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public ReadOnly Property IsDirty() As Boolean
Get
Return _IsDirty
End Get
End Property
Public Property TextValue() As String
Get
Return _TextValue
End Get
Set(ByVal value As String)
If value <> _TextValue Then
_TextValue = value
OnPropertyChanged("TextValue")
End If
End Set
End Property
End Class
答案 1 :(得分:0)
我建议使用Validated()
代替TextChanged
事件。仅当所述元素的焦点被用户输入改变时才调用Validated()
。这样您就可以通过外部来源保存TextBox
。