Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get really usable area of a Groupbox

Tags:

c#

winforms

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.

like image 622
LTR Avatar asked Jan 15 '13 20:01

LTR


People also ask

Can a GroupBox display a caption?

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.

What is GroupBox used for?

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.

How do I add controls to GroupBox?

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.

What is a GroupBox in C#?

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.


2 Answers

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;
like image 112
LarsTech Avatar answered Oct 21 '22 06:10

LarsTech


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.

like image 1
Mihai-Daniel Virna Avatar answered Oct 21 '22 06:10

Mihai-Daniel Virna