tags:

views:

585

answers:

2

Hi,

I have a recursive method on the base form that takes in a control and an enabled flag. It goes through every control on the form and based on what the control type is, it sets the background colour of the control accordingly and sets the enabled property to the parameter.

So generally, the method is called passing (this) as the control, it goes through all controls and their controls and sets things accordingly. This has worked fine but has the forms have had more and more controls added to them, you can actually see the controls disabling one by one and it doesnt look good.

Does anyone have an idea how i can either rewrite this or stop it from showing the disabling process on each control one by one? Something like a SuspendLayout which would work in this case? Its not an option to add a panel to the form and just disable it and reenable it at the end, because I have about 200 + forms that inherit from this base form and cant go through each one and force it to add the controls to the panel. This also wouldnt work because its not only a matter of enabling/disabling the controls, but applying other logic to them.

+6  A: 

Enclose the modification in:

form.SuspendLayout();

and

form.ResumeLayout(false); // read the doc about "false", might be a little unsafe

Also, traversing the control hierarchy might be time consuming. You could do that once and cache them in a list and refer to that list after that.

Mehrdad Afshari
SuspendLayout doesnt work for enable/disable. It only works for paint and layout...
Vixen
I'm not sure. You might be right but anyway, you said in question: "its not only a matter of enabling/disabling the controls, but applying other logic to them." If you're dealing with just 20/30 controls, I guess your problem is in searching for them in the control tree. Couldn't you just cache them in an array once and use the cached list instead?
Mehrdad Afshari
I could try on load of the screen add them to a list and then perform the layout logic on the list... will see if thats any quicker. Thanks
Vixen
If that didn't help, try posting your code.
Mehrdad Afshari
Significant improvement by adding the controls to a list on load and then going through that list to perform the work. Thanks.
Vixen
I moved the comment to the answer so that it'll be more prominent for future readers.
Mehrdad Afshari
A: 

Override the OnPaint method and only call super::OnPaint if you are done disabling them (IsDoneDisabling = true). It's just a workaround not a good design.

OnPaint(...)
{
if (IsDoneDisabling)
    super::OnPaint(...);
}
Cedrik
The question is tagged C#, .NET. I wonder if the syntax is valid in C#... let alone the concept of painting that's irrelevant.
Mehrdad Afshari
It is just a simple pseudo-code. Also, I don't get your point about irrelevance of painting.
Cedrik