简体   繁体   English

防止在文本字段为空时用户按退格按钮时出现错误消息

[英]Preventing error message when user presses backspace button when textfield is empty

Whenever the user presses the backspace button on an empty text field, the following error message appears: 每当用户在空白文本字段上按退格按钮时,就会出现以下错误消息:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll. mscorlib.dll中发生类型为'System.ArgumentOutOfRangeException'的未处理异常。 Additional information: StartIndex cannot be less than zero. 附加信息:StartIndex不能小于零。

How can I get it to ignore backspace button presses if the textfield is empty. 如果文本字段为空,我如何让它忽略按退格按钮。

   private void btnback_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.Text = remainTxt.Text.Remove(remainTxt.Text.Length - 1, 1);
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.Text = totalTxt.Text.Remove(totalTxt.Text.Length - 1, 1);
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.Text = paidTxt.Text.Remove(paidTxt.Text.Length - 1, 1);
        }
    }

You have to check if Text length is not 0. The Text.Remove function's first argument in your handler gets -1 as first parameter. 您必须检查Text length是否不为0。处理程序中Text.Remove函数的第一个参数将-1作为第一个参数。 This causes the exception to be thrown, because this is not a valid index. 这将引发异常,因为这不是有效的索引。 You should wrap the entire method body in a if ( remainTxt.Text.Length > 0 ) block 您应该将整个方法主体包装在if ( remainTxt.Text.Length > 0 )块中

Sounds like you're trying to solve the problem in the wrong way. 听起来您正在尝试以错误的方式解决问题。 The problem you're having is that your code is not checking to see if the text box is empty before you run some logic on it. 您遇到的问题是您的代码运行逻辑之前没有检查文本框是否为空。

Change your code to check if !string.IsNullOrEmpty(remainTxt.Text) (and the others) before attempting to subtract 1 from Length property. 更改代码,以在尝试从Length属性中减去1之前检查!string.IsNullOrEmpty(remainTxt.Text) (及其他)。 Because the textbox is empty, Length - 1 is -1, and is indeed, out of range. 由于文本框为空,因此“ Length -1”为-1,并且实际上超出了范围。

I would abstract out the remove last character operation into a function that guards against an empty text field 我将把移除最后一个字符的操作抽象为一个防止空文本字段的函数

private void RemoveLast(TextBox tb) {
  if (tb.Text.Length > 0) { 
    tb.Text = tb.Text.Remove(tb.Text.Length - 1, 1);
  }
}

Then switch the event handler to use that function 然后切换事件处理程序以使用该功能

private void btnback_Click(object sender, EventArgs e)
{
    if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        RemoveLast(remainTxt);
    }
    else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        RemoveLast(totalTxt);
    }
    else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        RemoveLast(paidTxt);
    }
}

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

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