简体   繁体   English

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

[英]How to delete the last character from a TextBox?

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 Backspace key, but I have no idea about how to delete the last character on TxtResult.Text (Text Box). 我正在尝试编写MS Windows计算器的副本 - 只是为了练习我在我正在做的课程中获得的知识 - 而且我在编写Backspace键时遇到问题,但我不知道如何删除TxtResult.Text (文本框)上的最后一个字符。 So, can some one teach me how to do that? 那么,有人可以教我怎么做吗?

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

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 Substring method I'm using here extracts a portion of the string and assigns it to the Text property of the textbox. 编辑:根据要求,我在这里使用的Substring方法提取字符串的一部分,并将其分配给Text框的Text属性。 See: http://msdn.microsoft.com/en-us/library/aka44szs.aspx 请参阅: http//msdn.microsoft.com/en-us/library/aka44szs.aspx

尝试

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

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


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

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM