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.
Edit: There is an easy hack to get that inner rectangle: Simply position one Label
in 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.
The GroupBox control is similar to the Panel control; however, only the GroupBox control displays a caption, and only the Panel control can have scroll bars.
The GroupBox displays a frame around a group of controls with or without a caption. Use a GroupBox to logically group a collection of controls on a form. The group box is a container control that can be used to define groups of controls.
To create a group of controls If you have existing controls that you want to enclose in a group box, you can select all the controls, cut them to the Clipboard, select the GroupBox control, and then paste them into the group box. You can also drag them into the group box.
A GroupBox control is a container control that is used to place Windows Forms child controls in a group. The purpose of a GroupBox is to define user interfaces where we can categories related controls in a group. A GroupBox control is a container control that is used to place Windows Forms child controls in a group.
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;
Quite old thread, but here is what I use for my controls :
label1.Location = new Point(0,(int)(groupBox1.Font.Size)*2);
And this is how it looks with different text sizes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With