简体   繁体   English

为什么我的退格返回一个新文本而不是更新当前文本?

[英]Why does my backspace return a new text instead of updating the current text?

I am trying my hand at a self teaching project by creating a calculator.我正在通过创建一个计算器来尝试一个自学项目。 I am having trouble getting my backspace to update the text in my textbox.我无法让退格键更新文本框中的文本。 My goal is to be able to Backspace using my backspace button and add a number afterward and having my result being exactly whats expected with any backspace ie我的目标是能够使用我的退格按钮退格,然后添加一个数字,让我的结果完全符合任何退格的预期,即

(1) string of 701 (2) Backspace used (3) Adding 4 to the string (4) return 704 (1) 701 的字符串 (2)使用退格(3)将 4 添加到字符串(4) 返回 704

What I am thinking is that when I click the button it is returning a new string and when I enter a new number, it is updating the old string.我在想的是,当我单击按钮时,它会返回一个新字符串,而当我输入一个新数字时,它会更新旧字符串。 I have tried this code in the btnBck_Click function as well as what I am showing in my current code(the BackSpaceFunction(); is my attempt to tell the program to return the updated string).我已经在 btnBck_Click function 中尝试了此代码,以及我在当前代码中显示的内容(BackSpaceFunction(); 是我试图告诉程序返回更新的字符串)。

    private void btnBck_Click(object sender, EventArgs e)
    {
        BackSpaceFunction();
    }

    private string BackSpaceFunction()
    {
        string textBoxText = textBox1.Text;
        textBoxText = textBox1.Text.Remove(textBoxText.Length - 1);
        if (textBox1.Text.Length >= 1)
        {
            textBox1.Text = textBox1.Text.Replace(textBox1.Text, textBoxText);
            return textBox1.Text;
        }

        else if (textBox1.Text.Length == 0)
        {
            textBox1.Text = "0";
            input = string.Empty;
            operand1 = string.Empty;
            operand2 = string.Empty;
        }
        return textBox1.Text;
    }

(Please understand my excessive explaining and use of examples is to make sure everyone can understand in case I myself missed the point. Thank you in advance) (请理解我过度解释和使用示例是为了确保每个人都能理解,以防我自己错过了重点。提前谢谢你)

I am having trouble getting my backspace to update the text in my textbox我无法让退格键更新文本框中的文本

The TextBox control supports use of the backspace key directly. TextBox控件支持直接使用退格键。 As long as the TextBox has the input focus, pressing the backspace key will delete the character just before the current insertion point.只要TextBox有输入焦点,按下退格键就会删除当前插入点之前的字符。

Your code example is incomplete, but it looks like you have a separate Button object that is intended to provide similar functionality.您的代码示例不完整,但看起来您有一个单独的Button object,旨在提供类似的功能。 You aren't taking into account the insertion point for the TextBox , so presumably you just always want to remove the last character.你没有考虑到TextBox的插入点,所以大概你只是想删除最后一个字符。 If that's the case, your method is far more complicated than it needs to be.如果是这种情况,您的方法远比它需要的复杂。 You can just use string.Substring() to remove the last character:您可以只使用string.Substring()删除最后一个字符:

private string BackSpaceFunction()
{
    if (textBox1.Length > 0)
    {
        textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
    }

    if (textBox1.Text.Length == 0)
    {
        textBox1.Text = "0";
        input = string.Empty;
        operand1 = string.Empty;
        operand2 = string.Empty;
    }

    return textBox1.Text;
}

(You never actually use the return value, so it's not clear why the method has a return value. But I left that in because it's not inherently a problem.) (你从来没有真正使用过返回值,所以不清楚为什么该方法有返回值。但我把它留在了,因为它本质上不是问题。)

So to answer my own question, I took the time to try and figure it out for myself.因此,为了回答我自己的问题,我花时间尝试自己弄清楚。 I ended up succeeding and found that this code worked (look at the "if" portion of my if statement).我最终成功了,发现这段代码有效(看看我的 if 语句的“if”部分)。

    private void btnBck_Click(object sender, EventArgs e)
    {

        BackSpaceFunction();

    }

    private string BackSpaceFunction()
    {
        //textBoxText = textBox1.Text.Remove(textBoxText.Length - 1);
        if (textBox1.Text.Length >= 1)
        {
            string textBoxText = textBox1.Text.Remove(textBox1.Text.Length - 1);
            string updatedText = textBox1.Text = textBox1.Text.Replace(textBox1.Text, textBoxText);
            textBox1.Text = updatedText;
            input = string.Format(textBox1.Text, updatedText);
            operand1 = string.Format(textBox1.Text, updatedText);
            operand1 = string.Format(textBox1.Text, updatedText);
        }

        else if (textBox1.Text.Length == 0)
        {
            textBox1.Text = "0";
            input = string.Empty;
            operand1 = string.Empty;
            operand2 = string.Empty;
        }
        return textBox1.Text;

    }'''

I'm not exactly sure why I had to use Format for input , operand1 , and operand2 but it ended up doing exactly what I needed.我不完全确定为什么我必须对inputoperand1operand2使用 Format ,但它最终完全符合我的需要。 If there are any explanations as to why this worked I will gladly accept the learning opportunity.如果有任何解释为什么这有效,我将很乐意接受学习机会。 :) :)

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

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