简体   繁体   English

检查c#中flowLayoutPanel是否为空

[英]Check if flowLayoutPanel is empty in c#

I want to make an error label come up when my flowLayoutPanel is empty, but i don't know how to check that the flowLayoutPanel is empty. 我想在我的flowLayoutPanel为空时出现错误标签,但我不知道如何检查flowLayoutPanel是否为空。 This is my current code: 这是我目前的代码:

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

Please Help, 请帮忙,

Thanks 谢谢

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

The problem you're running into is you're checking Controls for null to determine if it's empty. 您遇到的问题是您正在检查Controls for null以确定它是否为空。 The Controls property won't ever be null but instead will be non-null and have 0 length when empty. Controls属性永远不会为null ,而是为非null,空时为0。 For example 例如

if (flowLayoutPanel1.Controls.Count == 0) {
  // It's empty
}
lblNoContacts.Visible = (flowLayoutPanel.Controls.Count == 0) ? true : false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM