Click here to Skip to main content
15,307,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a need to set Enabled=false for all controls on several tabpages and later to set Enabled=true.
I have a method which sets them to false:-
public Control SetControlsDisabled(Control ParentControl)
  {
      try
      {
          foreach (Control c in ParentControl.Controls)
          {
              c.Enabled = false;
          }
      }
      catch (Exception e1)
      {
          ExceptionHandler(e1.ToString(), "Cant disable controls on form", e1);
      }
      return null;
  }
This works just fine, passing the tabpage name as ParentControl, I can single step through the method and see them change.
The method for setting Enabled to true is nearly identical:
<pre>      public Control SetControlsEnabled(Control ParentControl)
        {
            try
            {
                foreach (Control c in ParentControl.Controls)
                {
                    c.Enabled = true;
                }
            }
            catch (Exception e1)
            {
                ExceptionHandler(e1.ToString(), "Cant disable controls on form", e1);
            }
            return null;
        }

However when I single step through this the Enabled property simply doesn't change.
How can a method change a property to false but not change it back to true?

What I have tried:

I had this as one method and passed the boolean true/false to it but changed to two methods for debugging.
I've checked that all controls do have an Enabled property.
Posted
Updated 19-Jan-22 14:06pm
Comments
j snooze 19-Jan-22 17:56pm
   
That is strange, out of curiosity, does adding a ParentControl.Refresh after the foreach do anything? Wouldn't think you would need that on a winform, but I've seen weirder things.
ormonds 19-Jan-22 18:07pm
   
Nope, added that and same result.

You should have just made it one method since they doing the exact same thing. Pass a true/false value to the method depending on if you want the controls enabled or disabled.

Next, if the controls your enabling/disabling are inside another container on the TabControl, your code, as written, will not change them. Your code will have to modify your code to call itself with a new parent control.
C#
public void SetControlsState(Control parentControl, bool enable)
{
    try
    {
        foreach (Control c in parentControl.Controls)
        {
            c.Enabled = enable;
            if (c.HasChildren)
            {
                SetControlsState(c, enable);
            }
        }
    }
    catch (Exception e1)
    {
        ExceptionHandler(e1.ToString(), "Unable to change the state of controls.", e1);
    }
}

Lastly, the visual state of your controls will not change until your code call tree exits and your app returns to an idle state. That's when your form will process the WM_PAINT messages it's been getting from Windows and will actually redraw the controls on-screen with the new state.
   
Comments
ormonds 19-Jan-22 21:07pm
   
Yes, I did have it as one method, but when it failed split in (for now) into two methods.
There are no container controls on these tabpages so the method needn't be recursive.
I understand that the visual state won't change each step, but when I debug and watch the variable c.Enabled it changes when being set to false in the first method, but not when being set to true in the second method.
Dave Kreskowiak 19-Jan-22 22:08pm
   
It has nothing to do with the code you posted, so the problem is somewhere else.
ormonds 19-Jan-22 22:55pm
   
Thank you, I'm sure you are right. I'll keep looking.
to set the'Enabled property of all Controls on a TabPage:
// 1:
public void SetTabPageEnabled(TabPage tpg, bool isenabled)
{
    tpg.Enabled = isenabled;
}

// 2:

public void SetTabPageControlsEnabled(TabPage tpg, bool isenabled)
{
    foreach (Control cntrl in tpg.Controls)
    {
        cntrl.Enabled = isenabled;
    }
}
Note: in example #2, recursion is not needed because any ContainerControl on the TabPage that is disabled will disable all its contained controls.
   
v4
Comments
ormonds 19-Jan-22 21:02pm
   
Thanks, yes, that was where I started but if I do that then the user can't select each tabpage and see the (disabled) controls. So I changed to simply disabling the controls on each tabpage so that at least they can be seen.
If the user clicks an "Edit" button they are enabled, or are meant to be.
BillWoodruff 19-Jan-22 21:24pm
   
While a TabPage doesn't expose an 'Enabled property at design-time, you can set it at runtime in code, and, the TabPage can still be selected.

see revised solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900