Reputation: 327
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.
Upvotes: 1
Views: 128
Reputation: 4796
First of all, When you have three columns and 10 rows, it is better to use a component that is made for this purpose.
So a first upgrade would be to use a DataGridView
Then you can run through each cell with a foreach
loop and check they are not empty, or even use LINQ to do it at once.
That's it problem solved.
However, if you don't want to take the time to learn about DataGridView, you can just replace the ||
by a &&
because the vertical bar is an OR operator and the &&
is AND. So you want to check that all cells are filled...
Upvotes: 0
Reputation: 9463
I might be a little bit out of scope here, but I think your code would really profit if you introduce a class to group the inputs of a row together. This makes your code more readable and maintainable, e.g. if you need an additional input for each row or want to change the number of rows.
public class RowModel {
public RowModel() {
Item = new TextBox();
Product = new TextBox();
Quantity = new TextBox();
}
public int Index { get; set; } // might be useful to display better error messages
public TextBox Item { get; set; }
public TextBox Product { get; set; }
public TextBox Quantity { get; set; }
// here we only check if this single row is valid
public bool IsValid() {
return !String.IsNullOrWhiteSpace(Item.Text)
&& !String.IsNullOrWhiteSpace(Product.Text)
&& !String.IsNullOrWhiteSpace(Quantity.Text);
// any additional validation here, e.g. Quantity > 0
}
}
Create your rows like this:
const int numberOfRows = 10;
IList<RowModel> rows = new List<RowModel>();
for (var i = 0; i < numberOfRows; i++) {
rows.Add(new RowModel { Index = i });
}
Then check them like this:
using System.Linq;
IList<RowModel> rows;
var allValid = rows.All(r => r.IsValid());
btnSave.Enabled = allValid;
Upvotes: 1
Reputation: 9232
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.
Upvotes: 3
Reputation: 13676
Well, if you want to check ALL rows to be filled and enable button only then, you can create extension method that checks all values :
public static bool AllValuesNotNull(params string[] @strings)
{
return [email protected](string.IsNullOrEmpty);
}
And use it :
btnAdd.Enabled = AllValuesNotNull(txtItem.Text, txtItem.Text2, txtItem.Text4 ... etc)
P.S. If any of textBoxes is null or empty it'd make a whole row not filled and since you want all rows to be filled button should be disabled in this scenario.
Upvotes: 2