Jonald Samilo Jonald Samilo - 3 months ago 12
C# Question

Operators in If- Else Condition

I have 3 Column, the

Item Code, Product Name, and Quantity
. each column has 10 text boxes. I want to enable the save button once a row is filled and disabled if not. But my problem is how can I do that? this is what I tried so far:

public void showButtonSave()
{
if ((!String.IsNullOrEmpty(txtItem.Text) && !String.IsNullOrEmpty(txtProduct.Text) && !String.IsNullOrEmpty(txtQuantity.Text))
|| (!String.IsNullOrEmpty(txtItem2.Text) && !String.IsNullOrEmpty(txtProduct2.Text) && !String.IsNullOrEmpty(txtQuantity2.Text))
|| (!String.IsNullOrEmpty(txtItem3.Text) && !String.IsNullOrEmpty(txtProduct3.Text) && !String.IsNullOrEmpty(txtQuantity3.Text))
|| (!String.IsNullOrEmpty(txtItem4.Text) && !String.IsNullOrEmpty(txtProduct4.Text) && !String.IsNullOrEmpty(txtQuantity4.Text))
|| (!String.IsNullOrEmpty(txtItem5.Text) && !String.IsNullOrEmpty(txtProduct5.Text) && !String.IsNullOrEmpty(txtQuantity5.Text))
|| (!String.IsNullOrEmpty(txtItem6.Text) && !String.IsNullOrEmpty(txtProduct6.Text) && !String.IsNullOrEmpty(txtQuantity6.Text))
|| (!String.IsNullOrEmpty(txtItem7.Text) && !String.IsNullOrEmpty(txtProduct7.Text) && !String.IsNullOrEmpty(txtQuantity7.Text))
|| (!String.IsNullOrEmpty(txtItem8.Text) && !String.IsNullOrEmpty(txtProduct8.Text) && !String.IsNullOrEmpty(txtQuantity8.Text))
|| (!String.IsNullOrEmpty(txtItem9.Text) && !String.IsNullOrEmpty(txtProduct9.Text) && !String.IsNullOrEmpty(txtQuantity9.Text))
|| (!String.IsNullOrEmpty(txtItem10.Text) && !String.IsNullOrEmpty(txtProduct10.Text) && !String.IsNullOrEmpty(txtQuantity10.Text)))
{
btnAdd.Enabled = true;
}
else
{
btnAdd.Enabled = false;
}


additional informaton

Row must have a value of each columns to make the Button enable. Example, the user completely fill the 1st row of its every column then Button enables but once the user input only 1 column of the second row then the button starts to disable. To make the button enable the user must complete every column of the row.

Answer

I want to enable the save button once each row is fill[ed]

Right now what you have is that it enables once any row is filled (i.e. the first row is filled or the second, or the third, etc.).

If you want all of the rows to be filled, you have to write "and" everywhere instead of "or":

if ((!String.IsNullOrEmpty(txtItem.Text) && !String.IsNullOrEmpty(txtProduct.Text) && !String.IsNullOrEmpty(txtQuantity.Text))
   && (!String.IsNullOrEmpty(txtItem2.Text) && !String.IsNullOrEmpty(txtProduct2.Text) && !String.IsNullOrEmpty(txtQuantity2.Text))
   && (!String.IsNullOrEmpty(txtItem3.Text) && !String.IsNullOrEmpty(txtProduct3.Text) && !String.IsNullOrEmpty(txtQuantity3.Text))
        ...

It is more likely that you want to enable it if at least the first row is filled, and the rows after that are either completely filled or completely empty:

if ((!String.IsNullOrEmpty(txtItem.Text) && !String.IsNullOrEmpty(txtProduct.Text) && !String.IsNullOrEmpty(txtQuantity.Text))
   && ((!String.IsNullOrEmpty(txtItem2.Text) && !String.IsNullOrEmpty(txtProduct2.Text) && !String.IsNullOrEmpty(txtQuantity2.Text))
     || (String.IsNullOrEmpty(txtItem2.Text) && String.IsNullOrEmpty(txtProduct2.Text) && String.IsNullOrEmpty(txtQuantity2.Text)))
   && ...

However, as several people have commented, your code is starting to become pretty much unreadable. The most basic improvement would be to create a few functions that check a single line:

private bool IsCompletelyEmpty(TextBox item, TextBox product, TextBox quantity) 
{
  // To do: check that quantity is numeric, positive, item code is valid, etc.
  return String.IsNullOrEmpty(item.Text) 
    && String.IsNullOrEmpty(product.Text)
    && String.IsNullOrEmpty(quantity.Text);
}

private bool IsCompletelyFilled(TextBox item, TextBox product, TextBox quantity) 
{
  return !String.IsNullOrEmpty(item.Text) 
    && !String.IsNullOrEmpty(product.Text) 
    && !String.IsNullOrEmpty(quantity.Text);
}

private bool IsValidFilledOrEmpty(TextBox item, TextBox product, TextBox quantity) 
{
  return IsCompletelyFilled(item, product, quantity) 
    || IsCompletelyEmpty(item, product, quantity)
}

And then write

btnSave.Enabled = IsCompletelyFilled(txtItem, txtProduct, txtQuantity)
  && IsValidFilledOrEmpty(txtItem2, txtProduct2, txtQuantity2)
  && IsValidFilledOrEmpty(txtItem3, txtProduct3, txtQuantity3)
  && IsValidFilledOrEmpty(txtItem4, txtProduct4, txtQuantity4)
  && IsValidFilledOrEmpty(txtItem5, txtProduct5, txtQuantity5)
  ... ;

I could go further and suggest a proper model / view approach, but I think that's more suitable for Code Review.

Comments