2012-05-08 4 views
1

나는 데이터베이스를 쿼리하고 그 결과를 아래 상자에 보여주는 검색 상자가 있습니다. 이것은 ontextchanged 이벤트를 사용할 때 다소 느립니다. 왜냐하면 새 문자가 쓰여질 때마다 데이터베이스가 쿼리되기 때문입니다..net ontextchanged, 사용자가 쓰기를 마칠 때까지 기다려주십시오.

사용자가 글쓰기를 마쳤거나 사용자가 잠시 휴식을 취할 때마다 어떻게 쿼리를 수행 할 수 있습니까?

+0

VB.NET에는 searchbox 컨트롤이 없습니다. 검색 상자는 무엇을 의미합니까? 어떤 컨트롤을 사용하고 있습니까? – Leandro

+0

"TextChanged"에서 맞춤 검색 기능을 실행하는 것은 정상적인 TextBox 필드입니다. – Sjark

답변

1

가장 쉬운 방법은 마지막 OnTextChanged가 발생한 시간을 기록하는 것입니다. 현재 시간이 N보다 크면 웹 서비스를 호출하십시오.

또 다른 방법은 N 밀리 초마다 반복 시간을 시작한 다음 lastText로 텍스트를 확인하고 같지 않은 경우 웹 서비스를 호출하는 것입니다. 이렇게하면 검색 상자에서 현재 텍스트를 가져올 때 UI에서 콜백이 실행되도록 System.Windows.Form.Timer를 사용하십시오.

+0

고마워, 나는 그것을 시도 할 것이다. – Sjark

+0

첫 번째 단락의 해결책이 효과가 없을 것입니다. 마지막 키 누르기는 '현재 시간

+0

@ 단, 그래서 그것을 포함 시켰습니다. 나는 OP가 먼저 간단한 것을 시도하기를 원했습니다. –

1

아마도 timer 객체를 500ms (1/2 초) 간격으로 연결하고 .onTextChanged 이벤트가 발생하면 타이머 객체를 시작해야합니다. 그런 다음 타이머가 '틱'되면 해당 이벤트를 사용하여 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

내 솔루션은 타이머에게 키가 변경 될 때마다 해고하는 것입니다. 나는 텍스트가 변경된 횟수를 추적하여 타이머가 만료 된 횟수와 비교합니다. 두 숫자가 같으면 메서드를 호출하십시오. 참고 : 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 
관련 문제