在C#中检查flowLayoutPanel是否为空

3

我希望在我的flowLayoutPanel为空时显示一个错误标签,但我不知道如何检查flowLayoutPanel是否为空。这是我的当前代码:

private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (flowLayoutPanel1.Controls == null)
            {
                customtoolwarning.Visible = true;
            }
            else
            {
                customtoolwarning.Visible = false;
            }
        }

Please Help,

Thanks

3个回答

4
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (flowLayoutPanel1.Controls.Count > 0)
            {
                customtoolwarning.Visible = true;
            }
            else
            {
                customtoolwarning.Visible = false;
            }
        }

2
你遇到的问题是你在检查 Controls 是否为空时使用了 null。然而,当 Controls 为空时,它的值不会为 null,而是非空且长度为0。例如:
if (flowLayoutPanel1.Controls.Count == 0) {
  // It's empty
}

0
lblNoContacts.Visible = (flowLayoutPanel.Controls.Count == 0) ? true : false;

基本上没有什么区别,它只是另一种写法而已 :) - Chriz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接