-6

I want to do something like a calculator erase button or to be more specific I want to delete the last character when a textbox is changed and not a number is written. Like in Java I guess you could use CharAt() method but how about C#?

4
  • 2
    Allow me to demonstrate the website Google, as I search for remove last character in string c# and find not 1, not 2, not even 3, but 4 duplicates of this exact question on StackOverflow alone Commented Sep 12, 2013 at 19:53
  • @tnw same question but worth noting that the other accepted answer isn't applicable (other answers are) Commented Sep 12, 2013 at 19:53
  • Why do you edit the whole question to replace it with a completely different one? Create a new one (this on closed) Commented Sep 15, 2013 at 14:05
  • Do not edit your question to make it completely different and then try to get it reopened by saying it's not a duplicate. Commented Sep 28, 2013 at 21:20

4 Answers 4

5

For the text box, just handle the KeyPress event, like this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
    {
        e.Handled = true;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is the only real answer so far...the question is poorly titled and worded. It's really about ensuring only digits get entered.
@DonBoitnott Even more confusing, OP seems satisfied with Daniel's response anyway.
@DonBoitnott How do you know that this is the real answer? Maybe he already got that and did not know how to delete a char. The question was clear to me: How to delete last Char in String. Anyway +1 for Karl's answer because it will complete this whole mess
@tnw I agree, it is, but the wording is there nonetheless. Perhaps Daniel is right, I dunno. Just seemed like too many people jumped before reading.
2

Try substring:

var myTextBox = GetMyTexBoxFromSomewhere();
myTextBox.Text = myTextBox.Text.Substring(0, myTextBox.Text.Length - 1);

In the spirit of doing it "right"... if you're using windows forms:

private void tbCalculatorLikeInput_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) 
        && !char.IsDigit(e.KeyChar) 
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }

    // only allow one decimal point
    if (e.KeyChar == '.' 
        && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}

And if you're using ASP.NET WebForms:

<asp:TextBox id="txtNumbersOnly" Runat="server" />
<asp:RegularExpressionValidator  Runat="server" ID="valNumbersOnly" ControlToValidate="txtNumbersOnly" Display="Dynamic" ErrorMessage="Please enter a numbers only in text box." ValidationExpression="(^([0-9]*|\d*\d{1}?\d*)$)">
</asp:RegularExpressionValidator>

And if you're using HTML5 / MVC:

<input type="number" />

Comments

1

I presume you would rather want to use NumericTextBox like shown here by subclassing TextBox and overriding OnKeyPress.

There are other examples like here

Comments

0

Use following code:

stringName = stringName.Remove(stringName.Length - 1);

MSDN: http://msdn.microsoft.com/en-us/library/system.string.remove.aspx

Put the code into the TextBox.TextChanged Event-Method

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.