Reputation: 1142
I'm using this at the moment to remove the last character of my string if the back key is pressed, however if I hold the key it isn't removing characters from the string, while the rest of the conditionals work fine adding characters to the string. I suppose that the Remove function don't work well on a kind of loop.
if (key == Keys.Back)
{
if (text.Length > 0)
{
text.Remove(text.Length - 1);
}
}
Update: I thought that it was removing characters on key press, but it didn't. Thanks for the answers of this stupid error.
Upvotes: 1
Views: 1238
Reputation: 10242
Maybe i get also an upvote for posting the same answer like everybody else -.-
text = text.Remove(text.Length - 1);
Upvotes: 0
Reputation: 223237
strings are immutable, so the method Remove
would return a new string. It will not modify the existing string. To see the change assign the result back to your text
variable.
text = text.Remove(text.Length - 1);
Upvotes: 2
Reputation: 101681
Remove
method doesn't modify the text
. It returns a new string, you need to assign it back to your textBox
(or the text
, if you want to manipulate text
variable):
textBox1.Text = text.Remove(text.Length - 1);
Upvotes: 3