[SOLVED] User Control - Dock Fill at Runtime

Answered Closed
0
0

Hi,

In dropped two Panels in Window1, one is docked top and the other is docked at the bottom, I added a new User Control and put a Toolbar docked on top. When I dropped the User Control in Window1 using the Designer and set Dock=Fill, it works OK. However, when I removed the User Control from the Designer and created an instance of the User Control at runtime, the Toolbar is covered by Panel. The code is:

UserControl1 uc = new UserControl1();
uc.Dock = DockStyle.Fill;
this.Controls.Add(uc);

Thanks,

Cris

  • You must to post comments
Best Answer
0
0

Hi Cris,

Docking is applied using the z-order of the controls so Fill will fill whatever space is left available by other docked controls. When you use Controls.Add() the new control is rendered first since the z-order is from further away coming toward the viewer. For the Fill to use the remaining space use uc.BringToFront() after adding it to the parent.

You can test the order of docking and how the different sides overlap in the designer using right-click -> SendToBack/BringToFront.

Best,

Luca

  • You must to post comments
0
0

Hi Luca,

I have to add uc.BringToFront() to make it worked. Another option is to put another panel, dock it to Fill and make it as a container of the UserControl.

Thanks

Cris

  • You must to post comments
Showing 2 results