Force FlowLayoutPanel to re-layout all controls after they have been added and layed out prior

StackOverflow https://stackoverflow.com/questions/23608179

سؤال

I have a FlowLayoutPanel which I add controls to when my form is first instantiated. After this I show the form to the user. I would like to allow the user to check a checkbox that will change the WrapContents attribute of the FlowLayoutPanel and have it re-layout all of the Controls I added to it initially.

I have read many posts on SuspendLayout, ResumeLayout, and PerformLayout but they all seem to only pertain to when you are initially adding controls and not after controls have been added.

So I think the solution is to remove all of the controls from the panel, change the WrapContents attribute, then re-add all of the Controls when the user checks the checkbox? This just seems like too much work, is there an easier way to do this?

هل كانت مفيدة؟

المحلول 2

SuspendLayout/ResumeLayout are typically used when you are adding controls to FlowLayoutPanel and don't want layouting to occurs after each addition. It is suspended until you say resume (possibly even forcing it).

In your case, you already have collection of controls ready. Changing WrapContents will cause layouting (obviously), but only once. There will be no gain from using SuspendLayout/ResumeLayout.

If you have layout not updated immediately after changing WrapContents, then you forgot somewhere to call ResumeLayout, it should be called same number of times as suspending.

نصائح أخرى

flowLayoutPanel.FlowDirection = flowLayoutPanel.FlowDirection; will force a re-rendering.

As for its elegance... let he who has not hacked cast the first stone.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top