views:

86

answers:

2

Hi folks,

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 want it to display

abcdefg

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

Any suggestions?

A: 

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

Wael Dalloul
I'm ebarassed it was this simple. :) i wasn't storing the backspace char .. I just never thought of removing the last character from the StringBuilder. That's what programming at midnight does to you *blush* .. time for beddies :P
Pure.Krome
A: 

Try using the TextBox.TextChanged event instead.

Ian Kemp