如何从TextBox中删除最后一个字符?

时间:2012-09-13 00:39:07

标签: c#

我正在尝试编写MS Windows计算器的副本 - 只是为了练习我在我正在做的课程中获得的知识 - 而且我在编写 Backspace 键时遇到了问题,但我不知道如何删除TxtResult.Text(文本框)上的最后一个字符。那么,有人可以教我如何做到这一点吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZigndSuperCalc
{
    public partial class FrmZigndSC : Form
    {
        Int64 aux, result;
        Int16 cont = 0;
        bool sucess;
        public FrmZigndSC()
        {
            InitializeComponent();
        }

        private void BtnSoma_Click(object sender, EventArgs e)
        {
            sucess = Int64.TryParse(TxtInput.Text, out aux);
            result += aux;
            TxtInput.Text = Convert.ToString(result);
            TxtInput.Focus();
        }

        private void BtnCE_Click(object sender, EventArgs e)
        {
            TxtInput.Text = "0";
        }

        private void BtnC_Click(object sender, EventArgs e)
        {
            result = 0;
            TxtInput.Text = "0";
        }

        private void BtnBackspace_Click(object sender, EventArgs e)
        {
            // write here a method to delete the last character from 
        }
    }
}

8 个答案:

答案 0 :(得分:11)

如果我们模仿calc.exe,那么它可能就像:

string s = TxtResult.Text;

if (s.Length > 1) {
    s = s.Substring(0, s.Length - 1);
} else {
    s = "0";
}

TxtResult.Text = s;

编辑:根据要求,我在这里使用的Substring方法提取字符串的一部分并将其分配给文本框的Text属性。请参阅:http://msdn.microsoft.com/en-us/library/aka44szs.aspx

答案 1 :(得分:4)

尝试

TxtResult.Text = TxtResult.Text.Substring(0, TxtResult.Text.Length - 1);`

答案 2 :(得分:3)

if (textBox1.TextLength > 0) 
{ 
    textBox1.Text = textBox1.Text.Substring(0, (textBox1.TextLength - 1)); 
} 
else 
{ 
    MessageBox.Show("No Number.");
}

答案 3 :(得分:2)

我使用这种方法:

private void button_Click(object sender, EventArgs e)
{
    if (textbox.Text.Length > 0)
    {
        textbox.Text = textbox.Text.Remove(textbox.Text.Length - 1);
    }
}

答案 4 :(得分:0)



//CLICK ON BUTTON1

 private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 1)
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }![enter image description here][1]
            else
            {
                textBox1.Text = "0";
            }
        }

答案 5 :(得分:0)

You have to create a Button named "buttonDel" then

  private void buttonDel_Click(object sender, EventArgs e)
        {
               if (sender == buttonDel)
            {
                s = textBox1.Text;

                if (s.Length > 1)
                {
                    s = s.Substring(0,s.Length - 1);
                    textBox1.Text = s;
                }
                else
                {
                    textBox1.Text = "0";
                }
            }
        }

答案 6 :(得分:0)

这对我有用!!

    private void txtNumCL_TextChanged(object sender, EventArgs e)
    {
        bool ctrl = Int32.TryParse(txtNumCL.Text, out int outParse);
        if (!ctrl && txtNumCL.Text.Length > 0)
        {
            txtNumCL.Text = txtNumCL.Text.Substring(0, txtNumCL.Text.Length - 1);
            txtNumCL.SelectionStart = txtNumCL.Text.Length;
        }
    }

答案 7 :(得分:0)

已更新:

output.Text=output.Text.Remove(output.Text.Length-1, 1);

旧: 您可以使用:

TxtInput.Text = TxtInput.Text.ToString().Remove(TxtInput.Text.ToString().Length-1,1);