-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#?

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
Aras
  • 89
  • 1
  • 1
  • 8
  • 2
    Allow me to demonstrate the website Google, as I search for [`remove last character in string c#`](https://www.google.com/search?q=remove+last+character+in+string+c%C2%B7&oq=remove+last+character+in+string+c%C2%B7&aqs=chrome..69i57j0l3.3035j0&sourceid=chrome&ie=UTF-8#q=remove+last+character+in+string+c%23) and find not 1, not 2, not even 3, but 4 duplicates of this exact question on StackOverflow alone – tnw Sep 12 '13 at 19:53
  • @tnw same question but worth noting that the other accepted answer isn't applicable (other answers are) – McAden Sep 12 '13 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) – Daniel Abou Chleih Sep 15 '13 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. – Adam Lear Sep 28 '13 at 21:20

4 Answers4

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;
    }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 1
    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 Sep 12 '13 at 19:55
  • 1
    @DonBoitnott Even more confusing, OP seems satisfied with Daniel's response anyway. – tnw Sep 12 '13 at 19:57
  • @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 – Daniel Abou Chleih Sep 12 '13 at 19:58
  • @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. – DonBoitnott Sep 12 '13 at 20:01
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" />
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
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

Community
  • 1
  • 1
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
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

Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31