简体   繁体   English

如何比较两个文本框?

[英]How can I compare two text boxes?

I am trying to compare two text boxes to see if they are empty but I get an exception error: 我正在尝试比较两个文本框,以查看它们是否为空,但出现异常错误:

Input string was not in a correct format 输入的字符串格式不正确

Code: 码:

private void btncalc_Click(object sender, EventArgs e)
{
    try
    {
        int ina= int.Parse(txttea.Text);
        int inb= int.Parse(txtcoffee.Text);
        int inc = 0, ind = 0;
        if (this.txttea.Text == "" && this.txtcoffee.Text == "")  // this not working
        {
            MessageBox.Show("select a item");
            txttea.Focus();
        }
        if (cbxwithoutsugar.Checked)
        {
            inc = (ina * 20);
        }
        else
        {
            inc = (ina * 8);
        }
        if (cbxcoldcoffee.Checked)
        {
            ind = (inb * 10);
        }
        else
        {
            ind = (inb * 5);
        }
        txtamount.Text = (Convert.ToInt32(inc) + Convert.ToInt32(ind)).ToString();
    }
    catch (Exception a)
    {
        MessageBox.Show(a.Message);
    }
}

You should first check that the textboxes are empty and only then try to get the values. 您应该首先检查文本框是否为空, 然后才尝试获取值。
Also, use String.IsNullOrEmpty like this: 另外,像这样使用String.IsNullOrEmpty

    if (String.IsNullOrEmpty(txttea.Text) || String.IsNullOrEmpty(txtcoffee.Text)) 
    {
        MessageBox.Show("select a item");
        txttea.Focus();
    }
    int ina= int.Parse(txttea.Text); // See comment below about TryParse
    int inb= int.Parse(txtcoffee.Text);
    int inc = 0, ind = 0;

Also, use TryParse instead of Parse (and Trimming is always a good idea to avoid WhiteSpace): 另外,使用TryParse而不是Parse(并且Trimming总是避免空白的好主意):

int ina;
if (!Int32.TryParse(txttea.Text.Trim(), out ina))
{
   MessageBox.Show("Value is not a number");
}

try to use the TryParse method because if theres a whitespace your code fails, with TryParse u don't need to try-catch and just compare the two ints to zero, eg: 尝试使用TryParse方法,因为如果存在空格,则您的代码将失败,使用TryParse无需尝试捕获,只需将两个int进行比较即可为零,例如:

int ina =0 , inb =0;

int.TryParse(txttea.Text, out ina);
int.TryParse(txtcoffee.Text, out inb);

if (ina == 0 && this.inb == 0)  // this not working
{

}

I recommend that you use NumericUpDown rather than TextBox when you expect a number. 建议您使用数字时,建议您使用NumericUpDown而不是TextBox。 This way you can use Value to get number of coffee and tea. 这样,您可以使用“价值”获取咖啡和茶的数量。

private void btncalc_Click(object sender, EventArgs e)
{
    try
    {
        int ina= numtea.Value;
        int inb= numcoffee.Value;
        int inc = 0, ind = 0;
        if (ina == 0 && inb == 0)  // this not working
        {
            MessageBox.Show("select a item");
            numtea.Focus();
        }
        if (cbxwithoutsugar.Checked)
        {
            inc = (ina * 20);
        }
        else
        {
            inc = (ina * 8);
        }
        if (cbxcoldcoffee.Checked)
        {
            ind = (inb * 10);
        }
        else
        {
            ind = (inb * 5);
        }
        txtamount.Text = (inc + ind).ToString();
    }    
}

This is a good userfriendly solution. 这是一个很好的用户友好解决方案。

I have a feeling that the line you are indicating as 'not working' is not the problem, but the integer parsing is. 我感觉到您指示为“不工作”的行不是问题,但整数分析是问题。 The exception isn't one that can be thrown from the logical test, but it is one that can be thrown from a malformed integer parse. 异常不是可以从逻辑测试中抛出的异常,而是可以从格式错误的整数解析中抛出的异常。 Try commenting out the parsing lines and see if the error still ocurrs. 尝试注释掉分析行,看看是否仍然出现错误。

At first you are parsing the text of the text boxes. 首先,您正在解析文本框的文本。 and then you are again checking their text values against null. 然后再次检查它们的文本值是否为null。 Is it not redundant ? 这不是多余的吗?

If the text in the text areas are not number then there should be a parsing exception. 如果文本区域中的文本不是数字,则应该存在解析异常。 and it will shown in the Messagebox. 它将显示在消息框中。 I think you can try to put some logging statement priting the values of the parsed integers. 我认为您可以尝试放置一些日志记录语句来对已解析的整数的值进行排序。

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

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