Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docking multiple controls - one fills remaining space

Tags:

I'm trying to dock N number of controls in a container. I want them all to fill the entire width, but stack. I want one particular control (currently the last one) to fill the remaining space, while all others have fixed sizes.

This:

Button b1 = new Button() { Text = "Button 1", Dock = DockStyle.Top };
Button b2 = new Button() { Text = "Button 2", Dock = DockStyle.Top };
Button b3 = new Button() { Text = "Button 3", Dock = DockStyle.Fill };

Controls.Add(b1);
Controls.Add(b2);
Controls.Add(b3);

Produces this:

Button 3 filling entire parent

As you can see, Button 3 is doing exactly what I told it to: Fill the parent. But that's not what I want it to do. Aside from manually placing, and handling resize events, etc. how can I make Button 3 fill the remaining space?

Note: I am not using the designer.

like image 676
Jonathon Reinhart Avatar asked Oct 13 '11 04:10

Jonathon Reinhart


People also ask

How is anchoring different from docking?

Anchor refers to the position a control has relative to the edges of the form. A textbox, for example, that is anchored to the left edge of a form will stay in the same position as the form is resized. Docking refers to how much space you want the control to take up on the form.

Which setting of the dock property causes the control to fill its form or container control?

In the Properties window, select the arrow to the right of the Dock property. Select the button that represents the edge of the container where you want to dock the control. To fill the contents of the control's form or container control, press the center box.

Which control you would select to place the child controls in top left right and bottom using dock property?

The TableLayoutPanel control supports the Anchor and Dock properties in its child controls.

What is anchor property in C#?

Use the Anchor property to define how a control is automatically resized as its parent control is resized. Anchoring a control to its parent control ensures that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.


2 Answers

While adding b3.BringToFront() (after it has been added to Controls) works, the simplest solution here, is to simply change the order in which the buttons are added to Controls. The following code works perfectly:

Button b1 = new Button() { Text = "Button 1", Dock = DockStyle.Top };
Button b2 = new Button() { Text = "Button 2", Dock = DockStyle.Top };
Button b3 = new Button() { Text = "Button 3", Dock = DockStyle.Fill };

Controls.Add(b3);    // this guy first!
Controls.Add(b1);
Controls.Add(b2);

The result:

enter image description here

If you take a close look at the borders in this little example, this actually seems to work better than BringToFront().

like image 65
Jonathon Reinhart Avatar answered Nov 06 '22 22:11

Jonathon Reinhart


Basically the DockStyle.Fill control should be added first in the Controls collection.

like image 39
vezenkov Avatar answered Nov 06 '22 22:11

vezenkov