.net ontextchanged,请等待用户完成写入

.net ontextchanged,请等待用户完成写入,.net,vb.net,winforms,.net,Vb.net,Winforms,我有一个查询数据库的搜索框,并在下面的框中显示结果。当我使用ContextChanged事件时,这有点慢,因为每次写一封新信时都会查询数据库 我怎样才能让它只在用户完成编写时或每次用户稍作休息时执行查询?也许可以将计时器对象连接到500毫秒(半秒)的间隔,并在.onTextChanged事件触发时启动它。然后,当计时器“滴答”一声时,使用该事件触发对DB的查询?可能会以500毫秒(半秒)的间隔连接一个计时器对象,并在.onTextChanged事件触发时启动它。然后,当计时器“滴答”一声时,使用

我有一个查询数据库的搜索框,并在下面的框中显示结果。当我使用ContextChanged事件时,这有点慢,因为每次写一封新信时都会查询数据库


我怎样才能让它只在用户完成编写时或每次用户稍作休息时执行查询?

也许可以将计时器对象连接到500毫秒(半秒)的间隔,并在.onTextChanged事件触发时启动它。然后,当计时器“滴答”一声时,使用该事件触发对DB的查询?

可能会以500毫秒(半秒)的间隔连接一个计时器对象,并在.onTextChanged事件触发时启动它。然后,当计时器“滴答”一声时,使用该事件触发对DB的查询?

最简单的方法是记录上次ContextChanged发生的时间。如果当前时间大于N,则调用web服务


另一种方法是每N毫秒开始一次重复时间,然后用lastText检查文本,如果不相等,则调用web服务。如果执行此操作,请使用System.Windows.Form.Timer,以便在从搜索框中获取当前文本时,在UI上执行回调。

最简单的方法是记录上次OnTextChanged发生的时间。如果当前时间大于N,则调用web服务

另一种方法是每N毫秒开始一次重复时间,然后用lastText检查文本,如果不相等,则调用web服务。如果执行此操作,请使用System.Windows.Form.Timer,以便在从搜索框中获取当前文本时在UI上执行回调。

尝试以下操作:

Const WaitInterval As Integer = 5   '5 seconds <-- change this to control speed
Dim WithEvents MyTimer As New Timers.Timer

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    MyTimer.Interval = WaitInterval * 1000
    MyTimer.AutoReset = False
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    MyTimer.Stop()
    MyTimer.Start()
End Sub

Private Sub MyTimer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed

    '' -- call your method to query database here --

End Sub
Const WaitInterval As Integer=5'5秒尝试以下操作:

Const WaitInterval As Integer = 5   '5 seconds <-- change this to control speed
Dim WithEvents MyTimer As New Timers.Timer

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    MyTimer.Interval = WaitInterval * 1000
    MyTimer.AutoReset = False
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    MyTimer.Stop()
    MyTimer.Start()
End Sub

Private Sub MyTimer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed

    '' -- call your method to query database here --

End Sub

Const WaitInterval As Integer=5'5秒我的解决方案是每次更改一个键时触发一个计时器。我跟踪文本更改的次数,并将其与计时器过期的次数进行比较。如果两个数字相等,则调用该方法。注意:MySearchMethod()是在用户停止键入后激发的方法,txtQuickSearch是用户正在键入的文本框的ID

Private mint_LastReceivedTimerID As Integer = 0
Private mint_LastInitializedTimerID As Integer = 0

Private Sub txtQuickSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtQuickSearch.TextChanged
    'Increment the counter for the number of times the textbox has been changed
    mint_LastInitializedTimerID = mint_LastInitializedTimerID + 1

    'Wait longer for shorter strings or strings without spaces
    Dim intMilliseconds As Integer = 500
    If txtQuickSearch.Text.Length >= 6 Then
        intMilliseconds = 250
    End If
    If txtQuickSearch.Text.Contains(" ") = True And txtQuickSearch.Text.EndsWith(" ") = False Then
        intMilliseconds = 175
    End If

    Dim objTimer As New System.Timers.Timer(intMilliseconds)
    AddHandler objTimer.Elapsed, AddressOf txtQuickSearch_TimerElapsed

    objTimer.AutoReset = False
    objTimer.Enabled = True
End Sub

Private Sub txtQuickSearch_TimerElapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    'Increment the counter for the number of times timers have elapsed
    mint_LastReceivedTimerID = mint_LastReceivedTimerID + 1

    'If the total number of textbox changes equals the total number of times timers have elapsed (fire method for only the latest character change)
    If mint_LastReceivedTimerID = mint_LastInitializedTimerID Then
        'Fire method on the Main UI Thread
        Me.Dispatcher.Invoke(Sub() MySearchMethod(), System.Windows.Threading.DispatcherPriority.Normal)
    End If
End Sub

我的解决方案是每次更换钥匙时都会触发计时器。我跟踪文本更改的次数,并将其与计时器过期的次数进行比较。如果两个数字相等,则调用该方法。注意:MySearchMethod()是在用户停止键入后激发的方法,txtQuickSearch是用户正在键入的文本框的ID

Private mint_LastReceivedTimerID As Integer = 0
Private mint_LastInitializedTimerID As Integer = 0

Private Sub txtQuickSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtQuickSearch.TextChanged
    'Increment the counter for the number of times the textbox has been changed
    mint_LastInitializedTimerID = mint_LastInitializedTimerID + 1

    'Wait longer for shorter strings or strings without spaces
    Dim intMilliseconds As Integer = 500
    If txtQuickSearch.Text.Length >= 6 Then
        intMilliseconds = 250
    End If
    If txtQuickSearch.Text.Contains(" ") = True And txtQuickSearch.Text.EndsWith(" ") = False Then
        intMilliseconds = 175
    End If

    Dim objTimer As New System.Timers.Timer(intMilliseconds)
    AddHandler objTimer.Elapsed, AddressOf txtQuickSearch_TimerElapsed

    objTimer.AutoReset = False
    objTimer.Enabled = True
End Sub

Private Sub txtQuickSearch_TimerElapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    'Increment the counter for the number of times timers have elapsed
    mint_LastReceivedTimerID = mint_LastReceivedTimerID + 1

    'If the total number of textbox changes equals the total number of times timers have elapsed (fire method for only the latest character change)
    If mint_LastReceivedTimerID = mint_LastInitializedTimerID Then
        'Fire method on the Main UI Thread
        Me.Dispatcher.Invoke(Sub() MySearchMethod(), System.Windows.Threading.DispatcherPriority.Normal)
    End If
End Sub

据我所知,VB.NET没有searchbox控件,您所说的searchbox是什么意思?你在用什么控件?我的错,它是一个普通的文本框字段,在“TextChanged”上触发一个定制的搜索函数。据我所知,VB.NET没有搜索框控件,你说的搜索框是什么意思?你在使用什么控件?我的错,它是一个普通的文本框字段,在“TextChanged”上触发一个定制的搜索功能。你的第一段解决方案不起作用-如果在
当前时间
时发生,则不会注册最后一次按键。不过,第二个解决方案会起作用。@dan,这就是我将其包括在内的原因。“我想让OP先试试简单的东西。”Sjark,很高兴我能帮上忙。也许你可以发布你的解决方案来帮助下一个人!第一段的解决方案行不通-如果最后一次按键发生在
当前时间
时,则不会注册。不过,第二个解决方案会起作用。@dan,这就是我将其包括在内的原因。“我想让OP先试试简单的东西。”Sjark,很高兴我能帮上忙。也许你可以发布你的解决方案来帮助下一个人!