首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.net writing已更改,请等待用户完成写入

.net writing已更改,请等待用户完成写入
EN

Stack Overflow用户
提问于 2012-05-08 20:58:34
回答 4查看 2.1K关注 0票数 1

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

如何才能使其仅在用户完成编写或每次用户稍作休息时才执行查询?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-05-08 21:07:44

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

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

票数 1
EN

Stack Overflow用户

发布于 2014-03-15 01:08:09

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

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2012-05-08 21:03:19

例如,使用500ms (半秒)的间隔连接一个Timer对象,并在触发.onTextChanged事件时启动它。然后,当计时器“滴答作响”时,使用该事件来触发对数据库的查询?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10499090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档