Search code examples
vb.nettextboxbackspace

Remove last character from textbox on button click


I have a textbox, textbox1, whose ReadOnly property is set to true. It is somewhat like an on-screen keyboard. When the user presses a button, I can add characters by using:

Textbox1.Text = Textbox1.Text & A

But, when the user wants to remove the text, which should work exactly like the Backspace key, the last character should be removed from the textbox.

I am ready to give up normal textbox and use richtextbox if there is any way ahead with the rich one.

How to remove last character of a textbox with a button?


Solution

  • The previously suggested SubString method is what I would have posted too. However, there is an alternative. The Remove method works by removing every character after the given index. For instance:

    TextBox1.Text = Textbox1.Text.Remove(TextBox1.Text.Length - 1)