2

我正在尝试编写 MS Windows Calculator 的副本 - 只是为了锻炼我在我正在做的课程中获得的知识 - 我在编写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 
        }
    }
}
4

8 回答 8

13

如果我们在模仿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

于 2012-09-13T00:47:08.913 回答
4

尝试

TxtResult.Text = TxtResult.Text.Substring(0, TxtResult.Text.Length - 1);`
于 2012-09-13T00:43:10.483 回答
3
if (textBox1.TextLength > 0) 
{ 
    textBox1.Text = textBox1.Text.Substring(0, (textBox1.TextLength - 1)); 
} 
else 
{ 
    MessageBox.Show("No Number.");
}
于 2013-07-28T19:48:49.390 回答
2

我使用这种方法:

private void button_Click(object sender, EventArgs e)
{
    if (textbox.Text.Length > 0)
    {
        textbox.Text = textbox.Text.Remove(textbox.Text.Length - 1);
    }
}
于 2016-12-13T18:18:19.860 回答
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";
            }
        }

于 2013-08-31T08:03:12.410 回答
0

您必须创建一个名为“buttonDel”的按钮然后

  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";
                }
            }
        }
于 2018-03-25T20:11:17.233 回答
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;
        }
    }
于 2018-04-03T20:03:33.190 回答
0

更新:

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

旧:您可以使用:

TxtInput.Text = TxtInput.Text.ToString().Remove(TxtInput.Text.ToString().Length-1,1);
于 2018-10-08T13:36:49.953 回答