Click here to Skip to main content
14,734,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

How I can pragmatically remove last entered character(It may not be necessarily at the end of the string) from a text box.


There is no inbuilt mechanism other than "undo" - which may remove more than just the last character.
You may be able to do something on your own though.
I would create my own control, based on a TextBox and add a Stack<string> to it as a private member.
Handle the TextChanged event and push the new value on the stack.
When you want to undo the last character, pop off the top value, discard it. Pop off the next value, and set the textbox text to that. (This will cause a TextChanged event, which will put it right back on again)

Clearly, you will need to check for empty, and so forth, but that's the way I'd do it.
fjdiewornncalwe 19-Sep-12 9:58am
Almost reposted the same basic answer myself. +5.
OriginalGriff 19-Sep-12 10:01am
It's the obvious solution, really. (And should only take about five minutes to design, code, test and debug ! :laugh:)
[no name] 19-Sep-12 10:00am
Good answer.
Sebastian T Xavier 20-Sep-12 0:53am
my +5
Have you tried this?
textBox1.Text = textBox1.Text.SubString(0, textBox1.Text.Lenght - 1);
fjdiewornncalwe 19-Sep-12 9:55am
My vote of 1: Sorry, on this one you missed this part of the question: "It may not be necessarily at the end of the string"
[no name] 19-Sep-12 9:59am
OK, sorry for my misunderstanding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900