如何制作 if 语句来检查多个列表框是否为空?
How to make if statement that checks if multple listBoxes are empty or not?
如何创建询问多个列表框是否为空的 if 语句?
这就是我目前所拥有的...是否可以将其组合成一个单独的 if 语句?
if (listBoxEmails.Items.Count < 1)
{
//Perform action
}
if (listBoxWebsites.Items.Count < 1)
{
//Perform action
}
if (listBoxComments.Items.Count < 1)
{
//Perform action
}
这是我能想到的最简单的解决方案,
if (listBoxEmails.Items.Any() && listBoxWebsites.Items.Any() && listBoxComments.Items.Any())
{
// Do something here,
}
你可以在一些集合中有列表框,使用 linq 你可以在一个语句中找到列表是否为空。像这样的东西。当然,列表框集合可以有不同的方法。
private void ValidateListBoxes()
{
List<ListBox> listBoxes = new List<ListBox>();
listBoxes.Add(listBoxEmails);
listBoxes.Add(listBoxWebsites);
listBoxes.Add(listBoxComments);
bool isContainingEmptyList = listBoxes.Any(l => l.Items.Count < 1 || l.Items.Count==0);
}
如果您想从表单上的所有列表框中获取计数,您可以这样做:
if (Controls.OfType<ListBox>().Any(z => z.Items.Count < 1))
{
// Do Something
}
神奇之处在于,如果您在表单上删除或添加更多列表框,则无需更改任何代码。如果你想获取特定的列表框,你可以在所有你想要包含的列表框上设置 Tag
属性 到 CountedListBox
之类的东西,然后这样做:
if (Controls.OfType<ListBox>().Any(z => z.Items.Count < 1 && ((string)z.Tag == "CountedListBox")))
{
// Do something
}
if (listBoxEmails.Items.Count >= 0 && listBoxWebsites.Items.Count >= 0 &&
listBoxComments.Items.Count >= 0)
{
//perform action
}
这是在 WPF 还是 WinForms 中?你可以这样做:
var performAction = (!listBoxEmails.Items.IsEmpty | !listBoxWebsites.Items.IsEmpty | !listBoxComments.Items.IsEmpty);
if (performAction)
{
}
如何创建询问多个列表框是否为空的 if 语句?
这就是我目前所拥有的...是否可以将其组合成一个单独的 if 语句?
if (listBoxEmails.Items.Count < 1)
{
//Perform action
}
if (listBoxWebsites.Items.Count < 1)
{
//Perform action
}
if (listBoxComments.Items.Count < 1)
{
//Perform action
}
这是我能想到的最简单的解决方案,
if (listBoxEmails.Items.Any() && listBoxWebsites.Items.Any() && listBoxComments.Items.Any())
{
// Do something here,
}
你可以在一些集合中有列表框,使用 linq 你可以在一个语句中找到列表是否为空。像这样的东西。当然,列表框集合可以有不同的方法。
private void ValidateListBoxes()
{
List<ListBox> listBoxes = new List<ListBox>();
listBoxes.Add(listBoxEmails);
listBoxes.Add(listBoxWebsites);
listBoxes.Add(listBoxComments);
bool isContainingEmptyList = listBoxes.Any(l => l.Items.Count < 1 || l.Items.Count==0);
}
如果您想从表单上的所有列表框中获取计数,您可以这样做:
if (Controls.OfType<ListBox>().Any(z => z.Items.Count < 1))
{
// Do Something
}
神奇之处在于,如果您在表单上删除或添加更多列表框,则无需更改任何代码。如果你想获取特定的列表框,你可以在所有你想要包含的列表框上设置 Tag
属性 到 CountedListBox
之类的东西,然后这样做:
if (Controls.OfType<ListBox>().Any(z => z.Items.Count < 1 && ((string)z.Tag == "CountedListBox")))
{
// Do something
}
if (listBoxEmails.Items.Count >= 0 && listBoxWebsites.Items.Count >= 0 &&
listBoxComments.Items.Count >= 0)
{
//perform action
}
这是在 WPF 还是 WinForms 中?你可以这样做:
var performAction = (!listBoxEmails.Items.IsEmpty | !listBoxWebsites.Items.IsEmpty | !listBoxComments.Items.IsEmpty);
if (performAction)
{
}