vb.net 将 textchanged 事件设置为仅在用户而非程序实际输入文本时触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9931971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:01:33  来源:igfitidea点击:

Setting a textchanged event to only fire when text is actually entered by user not program

vb.netwinforms

提问by Skindeep2366

Windows forms Application. VB .NET 4.0 In my application I have a global boolean variable that keeps track of when changes are made and when they are saved called changesSaved. On closing even it checks the value of this variable before closing the window.. I am using textchanged event to automatically change the value of changesSaved to FALSE when text is changed.. Problem: The group box items are being populated dynamically so the user can edit the values or simply just view them. This dynamic population is causing the textchanged event to fire because the program is changing the text value of the box to put the values in that are stored in the database. The textchanged event should not fire unless the user him or herself enters a value or values in the text boxes.. Is there a way to specify the source i guess you could say for the text changed event or another way so that it will only fire when the user inputs information.??? Functions are as follows: The first is called by the load event to place the values in the box... The next one is the one that is being called as a result of the first one and is also the one that is causing the problems..

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   

采纳答案by LarsTech

A couple different ways.

几种不同的方式。

Since you are just doing ChangesSaved = Falsein the TextChangedevent, just set it to Trueafteryou programmatically update it.

由于您只是ChangesSaved = FalseTextChanged事件中进行,因此只需True您以编程方式更新它之后将其设置为。

Private Sub loadProperty(ByVal x As Integer)
  '// code
  RentProrate.Visible = True
  ChangesSaved = True
End Sub


Or just remove the TextChangedhandler, update the TextBox programmatically, then add the handler back in.

或者只是删除TextChanged处理程序,以编程方式更新 TextBox,然后重新添加处理程序。

RemoveHandler PropertyDetailsGroup.TextChanged, AddressOf PropertyTextChanged
PropertyDetailsGroup.Text = "value from database"
AddHandler PropertyDetailsGroup.TextChanged, AddressOf PropertyTextChanged


Implementing an INotiftyPropertyChangedinterface and using Databindingwould be another way, cleaner actually. You wouldn't need the flag at the form level, you could catch any changes made by the user at the Class level, something like this:

实现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

回答by Alex

I would suggest using the Validated()instead of the TextChangedEvent. Validated()is only called when the focus of said element gets changed by user input. That way you're save to populate your TextBox'es by an external source.

我建议使用Validated()而不是TextChanged事件。Validated()仅在用户输入更改所述元素的焦点时才调用。这样,您就可以TextBox通过外部来源保存以填充您的'es。

Control.Validated Event @ MSDN

Control.Validated 事件@ MSDN