Pragmateek
Pragmateek

Reputation: 13374

Find the top left origin of a GroupBox's inner area

I know the GroupBox is almost like a Panel with a header and a border but is not scrollable.

So in the GroupBox there is what I call the "inner area" where we want the inner elements to be displayed.

But it seems that the GroupBox does not place the elements in this area but directly at its top left corner like would a dumb panel.

Here is a simple illustration of the "issue":

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Button button = new Button { Text = "Hello!!!" };

        GroupBox groupBox = new GroupBox { Text = "Some useful stuff", Dock = DockStyle.Fill };
        groupBox.Controls.Add(button);

        this.Controls.Add(groupBox);
    }
}

Which gives this ugly result:

GroupBox with Button inside

I could play with the Button's Location property to add an offset but this is not 100% satisfactory.

What is the cleanest way to place the elements "inside the inner area"?

Is there a way to know the size of the border and header in order to use the right offset?

Upvotes: 1

Views: 111

Answers (1)

LarsTech
LarsTech

Reputation: 81620

Try using the GroupBox's DisplayRectangle property for that:

Button button = new Button { Text = "Hello!!!" };
button.Location = groupBox.DisplayRectangle.Location;
groupBox.Controls.Add(button);

Upvotes: 2

Related Questions