开发者

Not sure how to get the 'final' output from a WinForm KeyPress event

开发者 https://www.devze.com 2022-12-08 09:50 出处:网络
I\'ve got a simple winform. In it has a single TextBox control. In that, i\'ve wired up the KeyPress event. Why? I\'m trying to capture everything the user types in that textbox. But, when they hit re

I've got a simple winform. In it has a single TextBox control. In that, i've wired up the KeyPress event. Why? I'm trying to capture everything the user types in that textbox. But, when they hit return or enter, i then grab everything they've typed and send it to a command parser to do stuff.

I then display on the screen (in a RichTextBox) whatever they typed, so they know the command is about to get processed.

Simple stuff.

But the problem is when I hit the 'backspace' key. The Gui displays the incorrect text, a weird character that represents the backspace character and then the correct text.

So if i type this... ( == the backspace key)

abcdxxx<BS><BS><BS>efg

the gui displays that.

I 开发者_StackOverflowwant it to display

abcdefg

(notice the XXX chars and the 3 backspace chars and not displayed).

Any suggestions?


On key press event check if the key is backspace don't store it, instead delete the last character.


Try using the TextBox.TextChanged event instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消