Michael Haddad
Michael Haddad

Reputation: 4435

Why is the "Size" property of a control set by the designer when the control is docked to its parent in C# Winforms?

Consider a TabelLayoutPanel. I add it to Form1 using the Visual Studio designer and set the Dock property to to Fill. Even though I have set the size using the Dock property, the Size property is also set in Form1.Designer.cs

this.tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel.Size = new System.Drawing.Size(1000, 800);

Why is that? Why is the designer adding this line? Isn't the Dock property enough? Based on that, if I dynamically create this TableLayoutPanel, should I set both the Dock and the Size?

Upvotes: 1

Views: 682

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156978

Why is the designer adding this line?

That is a result of the inner working of the designer of the control. The Visual Studio designer takes every property that is not set to its default value. The default value for Size eventually boils down to the default value of Control.Size, which isn't specified. Hence, it is always generated by the designer. See as an example TabStop where a default value is specified: only setting it to false in the designer will generate code.

Does it have any effect on the visual end result of your TableLayoutPanel? No.

Upvotes: 1

Reza Aghaei
Reza Aghaei

Reputation: 125197

You don't need to set the Size. Setting Dock is enough.

The designer serializes all properties which have a value different than their default value. Since setting Dock property changes Size, the designer serializes it too.

The serialized size just will be used in future, if you set the Dock to None. Also setting size of a docked control at run-time doesn't have any effect.

Upvotes: 1

Related Questions