LTR
LTR

Reputation: 1352

Get really usable area of a Groupbox

In Windows Forms, when I position a label control at Y=0 inside a groupbox, then the label intersects with the title text on top of the groupbox.

How can I get the usable area within the Groupbox, i.e. the are that's not obstructed by the title text of the box?

Setting control.Y = groupBox.Padding.Top doesn't work. And Groupbox.ClientRectangle doesn't take the text into account either.

Explanation

Edit: There is an easy hack to get that inner rectangle: Simply position one Labelin the GroupBox, and set it's Dock property to Fill. Then you can get the relevant information (Top/Bottom/Left/Right) from the Panel, or simply use the panel directly to add your child controls. However, I'd still like to know how to get those coordinates without such hacks.

Upvotes: 6

Views: 2699

Answers (2)

Mihai-Daniel Virna
Mihai-Daniel Virna

Reputation: 994

Quite old thread, but here is what I use for my controls :

label1.Location = new Point(0,(int)(groupBox1.Font.Size)*2);

HeaderWithBigText

HeaderWithSmallText

And this is how it looks with different text sizes.

Upvotes: 1

LarsTech
LarsTech

Reputation: 81620

Try using the DisplayRectangle property:

The DisplayRectangle property returns the client rectangle of the display area of the control. For the base control class, this is equal to the client rectangle. However, inheriting controls might want to change this if their client area differs from their display area. The display rectangle is the smallest Rectangle that encloses a control and is used to lay out controls.

Example:

label1.Location = groupBox1.DisplayRectangle.Location;

Upvotes: 3

Related Questions