我如何知道在文本框的 TextChanged 事件中是否按下了退格键或删除键
我使用文本框的 TextChanged 事件。
例如我的文本框中有文本: "a b"- a,b 之间有 2 个空格 我走到两个空格的中间,然后按退格键。我无法通过将新文本与旧文本进行比较来检查按下了哪个键(如果按下了退格键或删除键,则新文本是 字母之间有相同的“a b”-1 空格。
如何检查按下的是哪个键?
I use the TextChanged event of a textbox.
for instance I have the text in the textbox:
"a b"- 2 spaces between a,b
I go in the middle of the 2 spaces, and hit backspace. I can not check by comparing the new text with the old which key was pressed (if backspace or delete was pressed, the newtext is the
same "a b"-1 space between letters.
How can I check which key was pressed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你为什么关心?如果您正在处理
TextChanged
事件,则不必关心文本发生了哪些变化,而只关心文本发生了变化这一事实。如果您需要关心具体更改的内容,则需要处理较低级别的事件,例如
KeyDown
:Why do you care? If you're handling the
TextChanged
event, you're not supposed to care what about the text was changed, just the fact that it was.If you need to care about what specifically was changed, you need to handle a lower-level event, like
KeyDown
:我知道这是一个老问题,但也许有人会发现这很有帮助。
我使用了一个整数来计算当前字符的长度。它随事件更新。
每次触发事件时,我都会检查计数器的长度是否比文本的当前长度长。如果是的话,我们知道,有一些东西被删除了。
I know this is an old question, but maybe someone will find this helpful.
I used an integer, that counts length of current characters. It updates with the event.
Every time, the event is triggered, I check, if the length of the counter is longer than the current length of the text. If it is, we know, there was something deleted.