Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have created a calculator with a very nice GUI, but I want the users of my calculator to delete numbers one by one instead of clearing everything at once from the textbox. How do I write a code for the backspace button? Do I need to create a button really or there is a way to write the code to each btn1, btn2 etc buttons that will enable the user to reduce numbers one by one?

For example, I want to type 123 and by mistake, I typed 132 and I want to remove the 32 instead of clearing all the numbers including one.

your help is needed..
Posted
Updated 31-May-20 23:53pm
Comments
Tomas Takac 31-Aug-14 3:25am    
How do I write a code for the backspace button?
Trim the right most digit. Where is the catch?

Do I need to create a button really or there is a way to write the code to each btn1, btn2 etc buttons that will enable the user to reduce numbers one by one?
It is not clear to me what you mean by this.

Refer - Simple Calculator in C#[^]
Quote:
C#
private void btnBackspace_Click(object sender, EventArgs e)
{
    //backspace
    if (txtResult.Text != string.Empty)
    {
        int txtlength=txtResult.Text.Length;
        if (txtlength != 1)
        {
            txtResult.Text = txtResult.Text.Remove(txtlength - 1);
        }
        else
        {
            txtResult.Text = 0.ToString();
        }        
    }
}
 
Share this answer
 
v2
Comments
Member 11004573 1-Sep-14 11:14am    
Thank you For Answer Me,But this CODE Is Not Running Well.Please Tell Me Another Method because When I Press The Button Then All Number Removed From The Text BOX .
Did you debug and see what is the issue?
Member 11004573 2-Sep-14 9:08am    
YES I run All The Code when i press the button then all data Delete from the Text box.
That is fine. I am telling did you put a break point inside the function and debug to see what is happening inside that?
Hi,

Please try this.

C#
private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Focus();
            Application.DoEvents();
            SendKeys.Send("{BACKSPACE}");
        }
 
Share this answer
 
use below code

C#
string s = "0";
        if (TextBox1.Text.Length > 1)
        {
            s = TextBox1.Text;
            s = s.Substring(0, s.Length - 1);
        }
        TextBox1.Text = s;
 
Share this answer
 
C#
    private void button1_Click(object sender, EventArgs e)
        {
//backspace
             string temp= textBox1.Text;
             if (temp.Length != 0)
             {
                 textBox1.Text = temp.Substring(1);
             }
        }
 
Share this answer
 
private void button10_Click(object sender, EventArgs e)
{
int x = result.Text.Length - 1;
result.Text = result.Text.Substring(0, x);
if (result.Text.Length == 0)
{
result.Text = "0";
}
}
 
Share this answer
 

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