Consider that you have a scenario like this:
- You have a primary (or Main)
Form in a WinForms Project, 'Form1.'
- In this primary Form, you have a
Panel, 'Panel1,' meant to serve as a container for Controls to be added at run-time (UserControls, WinForms Controls, whatever).
- You wish to set the
Dock property of those to-be-added at run-time Controls in some way so that, as they are added, you take advantage of automatic positioning (and you are willing to sacrifice using the
Margin property, since setting the Dock property will ignore any Margin setting).
To illustrate: let's use the following code example where two different types of Panels are added in response to a Button Click Event in a Form:
private InnerPanel newIP;
private int pCount = 0;
private void button1_Click(object sender, EventArgs e)
newIP = (0 == pCount++) ? new LeftInnerPanel() : new InnerPanel();
newIP.Dock = DockStyle.Left;
If you set the '
property of the control to be added before
adding it to its container Control, the '
, and '
' events of the container Control will not be invoked.
If you set the '
adding the Control to its container Control, those same Events will be invoked twice, as each Control is added.
By this time, I hope you are asking the question: "what's the practical value of this?:"
The way I use it is to watch for the change in the Container control when the number of Controls added suddenly exceed the container Control's boundaries, and Scrollbars are automatically turned on, at which point: in an
for one of those Size-changed type Events:
I do some specific stuff related to visual presentation in the UI to simulate how the added Controls would appear if the 'Margin property wasn't 'wiped out' by the fact the
property is set. Very specifically, I adjust the height of the container
so the 'bottoms' of the inserted
are not 'clipped.'
Other value ?: perhaps just avoiding firing these various size-related Events when you don't need or want to?
Why are the described Events fired twice if you set the
property after adding the Control to its container Control: Ask Microsoft