2012-05-08 13 views
1

私はデータベースにクエリを実行し、結果を下のボックスに表示する検索ボックスを持っています。これは、ontextchangedイベントを使用するとちょっと遅く、新しい文字が書き込まれるたびにデータベースが照会されるためです。.net ontextchanged、ユーザーの書き込みが完了するまで待つ

ユーザーが書き込みを終えたとき、またはユーザーが少し休憩するたびにクエリを実行するにはどうすればよいですか?

+0

VB.NETには、私が知っている限り、searchboxコントロールがありません。あなたはsearchboxをどういう意味ですか?どのようなコントロールを使用していますか? – Leandro

+0

私の悪いですが、それは "TextChanged"のカスタム検索機能を起動する通常のTextBoxフィールドです。 – Sjark

答えて

1

最も簡単な方法は、最後のOnTextChangedが発生した時刻を記録することです。現在の時刻がNより大きい場合は、Webサービスを呼び出します。

もう1つの方法は、Nミリ秒ごとに繰り返し時間を開始し、lastTextでテキストをチェックし、そうでない場合はWebサービスを呼び出します。これを行う場合は、System.Windows.Form.Timerを使用して、検索ボックスから現在のテキストを取得したときにUIでコールバックが実行されるようにします。

+0

ありがとう、私はそれを試みます。 – Sjark

+0

あなたの最初のパラグラフの解決策は動作しません。最後のキー押下は '現在の時刻

+0

@ダン、それは私がそれを含めた理由です。私はOPに何か簡単なことを試して欲しかった。 –

1

おそらくタイマーオブジェクトを500ms(1/2秒)間隔で配線し、.onTextChangedイベントが発生したときにタイマーオブジェクトを開始させることもできます。その後、タイマーが 'ticks'したら、そのイベントを使用してDBにクエリを発行しますか?

0

はこれを試してみてください。このため

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 
1

私のソリューションは、タイマーにキーが変更されるたびにオフに解雇することです。私は、テキストが変更された回数を追跡し、タイマーの期限切れ回数と比較します。 2つの数値が等しい場合は、メソッドを呼び出します。注: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 
関連する問題