If- Else条件下的运算符

时间:2016-08-23 08:04:56

标签: c#

我有3列,:after。每列有10个文本框。我想在一行填充后启用保存按钮,否则禁用。但我的问题是我该怎么做?这是我到目前为止所尝试的:

for Rec_T use record
   a at 0 range 0 .. 11; -- bits 0..7 of the 1st byte and bits 0..3 of the 2nd byte
   b at 0 range 12 .. 15; -- bits 4..7 of the second byte
end record;
for Rec_T'Size use 16;

其他信息

行必须具有每列的值才能启用Button。例如,用户完全填充其每一列的第一行然后按钮启用但是一旦用户仅输入第二行的一列,则该按钮开始禁用。要使按钮启用,用户必须完成该行的每一列。

4 个答案:

答案 0 :(得分:3)

  

我想在每行填充[ed]

后启用保存按钮

现在你所拥有的是,一旦任何行被填充就会启用(即第一行填充第二行,第三行等)。

如果要填充所有行,则必须编写"和"无处不在"或":

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

如果至少填充了第一行,并且之后的行完全填满或完全为空,则更有可能要启用它:

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

但是,正如几位人士所评论的那样,您的代码开始变得难以理解。最基本的改进是创建一些检查单行的函数:

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

然后写

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

我可以更进一步,建议一个正确的模型/视图方法,但我认为这更适合Code Review

答案 1 :(得分:2)

好吧,如果要检查要填充的所有行并仅启用按钮,则可以创建检查所有值的扩展方法:

public static bool AllValuesNotNull(params string[] @strings)
{
   return !@strings.Any(string.IsNullOrEmpty);
}

并使用它:

   btnAdd.Enabled = AllValuesNotNull(txtItem.Text, txtItem.Text2, txtItem.Text4 ... etc)

P.S。 如果任何的textBoxes为null或为空,则表示未填充整行,并且由于您希望所有行都已填充,因此在此方案中应禁用按钮。

答案 2 :(得分:1)

我可能在这里有点超出范围,但我认为如果你引入一个类将一行的输入分组在一起,你的代码真的会有利可图。这使您的代码更具可读性和可维护性,例如如果您需要为每行添加额外的输入或想要更改行数。

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

像这样创建行:

const int numberOfRows = 10;
IList<RowModel> rows = new List<RowModel>();
for (var i = 0; i < numberOfRows; i++) {
     rows.Add(new RowModel { Index = i });
}

然后像这样检查:

using System.Linq;
IList<RowModel> rows;
var allValid = rows.All(r => r.IsValid());
btnSave.Enabled = allValid;   

答案 3 :(得分:0)

首先,当你有三列和十行时,最好使用为此目的而制作的组件。

首次升级是使用DataGridView

然后,您可以使用foreach循环遍历每个单元格并检查它们是否为空,或者甚至使用LINQ立即执行此操作。

这就解决了问题。

但是,如果您不想花时间了解DataGridView,您只需将||替换为&&,因为竖线是OR运算符而{{1是和。所以你要检查所有单元格是否已填满...