Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing ~280 controls from visible=true to visible=false takes about 8-9 seconds (too long)

Tags:

c#

.net

winforms

I am wondering if there is something I can do to speed this up, I have narrowed the time lag to this section of code below. I am wondering if there is something else I can do to make this at least 2-3 times faster.

form1.SuspendLayout();
foreach (KeyValuePair<Control, bool> i in ItemControlUpdates)
{
    i.Key.Visible = i.Value;
}
form1.ResumeLayout();

Edit: Thank you guys for the comments, I corrected my code and hope it indicates my specific problem. (I do not need to write to file)

Edit2: The 280 controls are user controls and it is a requirement for my application to view all of them or some of them when the sorting selections are made.

Edit3: Answer is: SuspendLayout() must be called on the parent to be effective, I incorrectly used it on the form when I needed to use it on the panel that the controls were within.

like image 294
FlashTech Avatar asked Oct 24 '25 02:10

FlashTech


2 Answers

The problem was that the controls were embedded in a panel and exchanging form1.SuspendLayout() with panel.SuspendLayout() made this action take no time at all. A couple people hinted at this and I recommend people check out stakx answer as it includes other/similar possible solutions to a problem similar to this one.

Here is the fixed code that works great:

                cPanel.SuspendLayout();
                foreach (KeyValuePair<Control, bool> i in ItemControlUpdates)
                {
                    i.Key.Visible = i.Value;
                }
                cPanel.ResumeLayout();
like image 162
FlashTech Avatar answered Oct 25 '25 17:10

FlashTech


The fact is (as was stated in this comment above) that manipulating so many (280) controls is bound to be slow, simply because Windows Forms is not super-fast. Since you haven't said more about the structure and layout of your form(s) I can only give you a couple generic recommendations:

  • If some of the controls that you are manipulating are not actually embedded in form1, then form1.SuspendLayout() will have no effect for these controls. Suspend the layouting of all relevant forms.

  • You could try grouping some controls that logically belong together into a single user control, or at least into a Panel; you'd then only make that container control visible or invisible. Especially when you are using layout features such as AutoSize, that might mean that Windows Forms has to do less layout computations.

  • Reduce the total number of controls on your form drastically. Chances are that with so many controls on it, your form is too complex from a user experience (UX) point of view: You are asking too much from the user at one time, and your form might just end up being confusing or even unusable. Consider redesigning the form. If the workflow really is that complicated, you might at least split it up into a series of simpler steps – think wizard / multi-step dialog.

like image 24
stakx - no longer contributing Avatar answered Oct 25 '25 16:10

stakx - no longer contributing



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!