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;
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With