Reputation:
I am looking for information on datagrid using textbox assign textchanged_event
.
The problem I encountered was that when I typed the characters with a accented
then datagrid would blink (it seems every change would be recorded in the textbox not just the final result).
Example: when typing "khang"
If I continue to type the letter "a"
, in english it will result in "khanga"
and everything goes well, but in some languages it should be "khâng"
and I see in textchanged it will take many steps as:
"khang" -> "khang•" -> "khang" -> "khan" -> "kha" -> "khâ" ->"khân"->"khâng"
in this moment, the datagrid will blink continuously.
what should I do to textchanged only handle the final result?
Thanks for advices!
Upvotes: 0
Views: 59
Reputation: 54417
You can't guarantee that you only handle the final value unless you use a Button.Click
instead of a TextBox.TextChanged
, or maybe force the user to hit Enter and handle the TextBox.KeyDown
or the like. What you can do instead is ensure that you only act on the TextChanged
if no other TextChanged
has occurred for a specific length of time by using a Timer
. That way, the delay is not so great that the user really feels it but it is long enough to allow multiple keystrokes without acting on them in between.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/restart the Timer every time the Text changes.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Perform filter when time expires.
Timer1.Stop()
BindingSource1.Filter = $"SomeColumn LIKE '%{TextBox1.Text}%'"
End Sub
It's up to you want you set the Interval
of the Timer
to but I'd be looking at around 300. You can do a bit of experimentation to find what gives you the best balance.
Upvotes: 1