Jarrett Robertson
Jarrett Robertson

Reputation: 459

Placing a User Control into a groupbox

I have tried to search this question but most of the questions I've seen deal with how to place a user control into a group box in winforms. I have a slightly different question.

I have a form that contains a combo box and group box. I have a series of user controls that I want to swap in and out of the group box depending on the combo box selection. This all works perfectly fine.

The issue I am running into is that it seems to place the user control in the top left corner of the group box. This obscures the border and any text on the group box itself. I have tried changing the Padding in the group box and also the margins on the user control and nothing appears to change how the group box places the control.

I'm aware I can use a panel or manually place the controls after they are added, but I was hoping someone might know how the group box determines where to place controls and why it seems like padding/margins are being ignored.

Does padding only matter when the group box has multiple controls and the user control is being seen as a single control that doesn't need padded?

Upvotes: 1

Views: 1228

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Set the UserControl's Dock property to Fill when inside the GroupBox.

The Dock property uses the DisplayRectangle property of the parent control, which will respect the interior border area the GroupBox uses for the text caption and border lines.

Upvotes: 1

Related Questions