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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12397924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:07:30  来源:igfitidea点击:

How to delete the last character from a TextBox?

c#

提问by Zignd

I'm trying to write a copy of MS Windows Calculator - just to exercise the knowledge I have acquired in the course I'm doing - and I'm having problems to write the Backspacekey, but I have no idea about how to delete the last character on TxtResult.Text(Text Box). So, can some one teach me how to do that?

我正在尝试编写 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 
        }
    }
}

采纳答案by rein

If we're mimicking calc.exe, exactly then it's probably something like:

如果我们正在模仿calc.exe,那么它可能是这样的:

string s = TxtResult.Text;

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

TxtResult.Text = s;

EDIT: As requested, the Substringmethod I'm using here extracts a portion of the string and assigns it to the Textproperty of the textbox. See: http://msdn.microsoft.com/en-us/library/aka44szs.aspx

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

回答by Lews Therin

Try

尝试

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

回答by Ramzan Ali Qureshi

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

回答by SAYID DASTAAN



//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";
            }
        }

回答by Nerijus Pocevi?ius

I use this method:

我用这个方法:

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

回答by RiFat Chowdhury Ron

You have to create a Button named "buttonDel" then

您必须创建一个名为“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";
                }
            }
        }

回答by Guillermo Daniel Arias

this worked for me!!

这对我有用!!

    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;
        }
    }

回答by Max

Updated:

更新:

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

Old: You can use:

旧:您可以使用:

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