Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 407
Chris Bacon Avatar asked Nov 30 '25 17:11

Chris Bacon


2 Answers

private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (flowLayoutPanel1.Controls.Count > 0)
            {
                customtoolwarning.Visible = true;
            }
            else
            {
                customtoolwarning.Visible = false;
            }
        }
like image 151
Ali Tarhini Avatar answered Dec 02 '25 06:12

Ali Tarhini


The problem you're running into is you're checking Controls for null to determine if it's empty. The Controls property won't ever be null but instead will be non-null and have 0 length when empty. For example

if (flowLayoutPanel1.Controls.Count == 0) {
  // It's empty
}
like image 24
JaredPar Avatar answered Dec 02 '25 07:12

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!