Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up the enabling/disabling of controls (C# WinForms)

Tags:

c#

.net

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.

like image 372
Madeleine Avatar asked Nov 27 '25 22:11

Madeleine


1 Answers

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.

like image 178
mmx Avatar answered Nov 29 '25 11:11

mmx



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!