I am trying to filter a list of items in a ListView
as the user types into a TextBox
and I use KeyDown
, and KeyPress
events but when I read the textbox.Text
, it always returns the text before the last key press. Is there a way to always get whatever is shown in the TextBox
without pressing enter?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
You could use the
TextChanged
event of the TextBox in question. I think theKeyUp
event might work as well.Use the
TextBox.TextChanged
event (inherited fromControl
).My advice is to try not to hack this with the key events (down / press / up) - there are other ways to change a text-box's text, such as by pasting text from the right-click context menu. This doesn't involve pressing a key.
Use the KeyPressEventArgs.KeyChar Property.
The previous answers are incomplete with regards to the actual original question: how to retrieve the contents of the
Text
property when the user has just pressed a key (and including that keypress)?The
KeyUp
event happens to be fired AFTER the contents of theText
property actually change, so using this particular order of events, you can retrieve the latest value of the text contents just using aKeyUp
event handler.The
KeyPress
event doesn't work because that gets fired BEFORE the change of theText
property.You can try with KeyPress event: