Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# If-Else条件下的运算符_C# - Fatal编程技术网

C# If-Else条件下的运算符

C# If-Else条件下的运算符,c#,C#,我有3列,项目代码,产品名称和数量。每列有10个文本框。我希望在填充行后启用“保存”按钮,如果没有,则禁用该按钮。但我的问题是我怎么做?这就是我迄今为止所尝试的: public void showButtonSave() { if ((!String.IsNullOrEmpty(txtItem.Text) && !String.IsNullOrEmpty(txtProduct.Text) && !String.IsNullOrEmpty(t

我有3列,项目代码,产品名称和数量。每列有10个文本框。我希望在填充行后启用“保存”按钮,如果没有,则禁用该按钮。但我的问题是我怎么做?这就是我迄今为止所尝试的:

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;
        }
补充资料


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

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

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

   btnAdd.Enabled = AllValuesNotNull(txtItem.Text, txtItem.Text2, txtItem.Text4 ... etc)
附言。
如果任何文本框为null或为空,则会导致整行未填充,并且由于您希望填充所有行,因此在此场景中应禁用按钮。

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

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

   btnAdd.Enabled = AllValuesNotNull(txtItem.Text, txtItem.Text2, txtItem.Text4 ... etc)
附言。 若任何文本框为null或为空,则会导致整行未填充,并且由于您希望填充所有行,所以在这种情况下应禁用该按钮

我想在每行填充[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)
  ... ;
我可以更进一步,提出一种合适的模型/视图方法,但我认为这更适合于

我想在每行填充[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)
  ... ;

我可以进一步提出一种适当的模型/视图方法,但我认为这更适合于。

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

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;   
然后像这样检查它们:

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;   

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

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;   
然后像这样检查它们:

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;   

首先,当您有三列和10行时,最好使用为此目的制作的组件

因此,第一个升级是使用

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

问题解决了


但是,如果您不想花时间了解DataGridView,可以将| |替换为&&因为竖条是OR运算符,&&是and。因此,您需要检查所有单元格是否已填充…

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

因此,第一个升级是使用

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

问题解决了


但是,如果您不想花时间了解DataGridView,可以将| |替换为&&因为竖条是OR运算符,&&是and。所以你要检查所有的单元格是否都被填满了…

除了if语句的恐怖之外。。。你的问题是什么?你在使用WPF吗?你有什么样的应用程序?网络,windows。您是否使用了某种网格?您似乎已经知道如何启用/禁用按钮,您正在设置btnAdd.Enabled。所以假设
您有一个名为btnSave的保存按钮,为什么不以完全相同的方式启用/禁用呢?你到底需要什么帮助?最好使用DataGridView,而不是离散的文本框,然后你可以在行中迭代寻找一个空白单元格。除了if语句的可怕之处。。。你的问题是什么?你在使用WPF吗?你有什么样的应用程序?网络,windows。您是否使用了某种网格?您似乎已经知道如何启用/禁用按钮,您正在设置btnAdd.Enabled。因此,假设您有一个名为btnSave的保存按钮,为什么不以完全相同的方式启用/禁用它呢?你到底需要什么帮助?最好使用DataGridView,而不是离散的文本框,然后你可以在行中迭代寻找一个空白单元格。谢谢这个sir@CompuChip谢谢这个sir@CompuChip