简体   繁体   中英

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:

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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