Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does hiding controls in WinForm take longer than making them visible?

I have a FlowLayoutPanel that contains 56 checkboxes. The checkboxes are used in three state mode. Now here is the fun part. If the check boxes are uncheked that means that they are not used and can be hidden for ease or reading. To hide them I use another checkbox. When to user clicks the checkbox all unused checkboxs in the FlowPanel are hidden using a foreach iteration.

The problem is that to hide them, that foreach call take (checkBox.Visible=false) about 2-3 seconds and to show them (checkBox.Visible=true) takes 0.5 seconds.

Any suggestions as to why this is happening?

private void hideUnusedPinsCheckBoxClick(objest sender, EventArgs e)
{
   bool state = !hideUnusedPinsCheckBox.Checked;
   foreach(object obj in flowLayoutPanel.Controls)
   {
      CheckBox cB = (CheckBox)obj;
      if(cB.CheckState == CheckState.Unchecked)
         cB.Visible=state;
   }
}
like image 264
Robert Iagar Avatar asked Sep 02 '25 15:09

Robert Iagar


2 Answers

You could try calling SuspendLayout before hiding all your checkboxes, and then calling ResumeLayout afterwards. See this link for more.

The answer your question as to why this is happening. Each time you hide (or show) a control on the FlowLayoutPanel, the panels layouter algorithm is executed in order to rearrange everything on screen. If you hide for example 50 check boxes in a row, the layouter algorithm will execute at least 1,275 times. For example:

- Hide checkbox
- Perform layout for remaining 49 check boxes
- Hide checkbox
- Perform layout for remaining 48 check boxes
- Hide checkbox
- Perform layout for remaining 47 check boxes
- etc...

By calling SuspendLayout, the layouter algorithm does not run at all until you call ResumeLayout, reducing the number from 1,275 to 1.

like image 133
MattDavey Avatar answered Sep 05 '25 05:09

MattDavey


If you hide something, the system must find out what is below that obect, and force that thing to redraw itself.

On the other hand, when you make a control visible, only that control must be drawn.

like image 35
Erich Kitzmueller Avatar answered Sep 05 '25 05:09

Erich Kitzmueller