0

When I empty the textbox there is an error show that Input string was not in a correct format. This is the code.

private void txtQty_TextChanged(object sender, EventArgs e)
{

    int cart_qty = 0;
    if ((int.Parse(txtQty.Text) + cart_qty > qty))
    {

        MessageBox.Show("Unable to proceed. Remaining quanity on hand is " + qty, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
}

Now I want to try is to do nothing if the textbox is empty. How this works?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?system-string-isnullorwhitespace(system-string) (This of course still doesn't check if the user entered rubbish into the textbox that's not an integer number nor whether the inputted number is within the value range for the `int` data type, but this helps taking care of the textbox being empty...) –  Dec 14 '22 at 19:19
  • 1
    What do you think the result of `int.Parse(txtQty.Text)` when the text is empty? – Roman Ryzhiy Dec 14 '22 at 19:19
  • 2
    `if (txtQty.Text == "") return;` – Rufus L Dec 14 '22 at 19:24

3 Answers3

0

If txtQty.Text is "" then int.Parse(txtQty.Text) will produce an error, because "" can't be translated into an integer value. The same would be true if the value is " " or "I'm not an integer" or any other non-integer text value.

Use int.TryParse instead, so you can handle the case when no valid integer is present:

int cart_qty = 0;
int parsedQty = 0;
if (int.TryParse(txtQty.Text, out parsedQty))
{
    if (parsedQty + cart_qty > qty)
    {
        MessageBox.Show("Unable to proceed. Remaining quanity on hand is " + qty, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
}
else
{
    // the input was not a valid integer
}

You could structure it in a variety of ways. Maybe if int.TryParse returns false then you just return; from the method immediately. Maybe you show a message to the user. Etc. But overall the point is to use TryParse instead of Parse if the value might not be a valid integer.

David
  • 208,112
  • 36
  • 198
  • 279
0

Use TryParse. It will automatically return false if the TextBox contains an invalid number or is empty

private void txtQty_TextChanged(object sender, EventArgs e)
{
    int cart_qty = 0;
    if (Int32.TryParse(txtQty.Text, out int quantity) && quantity + cart_qty > qty) {
        MessageBox.Show("Unable to proceed. Remaining quantity on hand is " + qty, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}

Note that you can declare the quantity variable directly in the out parameter. You can also combine two if-statements into one by combining the two conditions with &&.

But be aware that the TextChanged event fires after each digit you are typing (effectively after each edit). So, you will have a hard time typing in a number having more than one digit if the message box pops up in between. You should use another event like Validated or Leave.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

It sounds like you're trying to do some numeric validation on the string. In this case, int.TryParse is your friend - it takes an out paramter that's set to the int value of the string if it succeeds, and it returns false if the string is not a valid int.

Here's an example using your code above, although I changed the condition slightly. You were creating a cart_qty variable and assigning it 0, then adding that to the number the user entered, which doesn't do anything to change the number.

private void txtQty_TextChanged(object sender, EventArgs e)
{
    int cart_qty;
    
    // If TryParse returns true, then cart_qty will represent the value
    if (int.TryParse(txtQty.Text, out cart_qty))
    {
        // Don't let them enter a negative number
        if (cart_qty < 0) 
        {
             MessageBox.Show("Unable to proceed. Quantity must be a positive number:", 
                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        // Don't let them enter a number larger than qty
        else if (cart_qty > qty)
        {
            MessageBox.Show("Unable to proceed. Remaining quanity on hand is " + qty, 
                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            // cart_qty is valid, so do something here
        }
    }
    // TryParse returned false, so the value was not a valid integer
    else
    {
        MessageBox.Show("Unable to proceed. Must enter a valid integer.", 
            "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43