Take a GroupBox
, put let say Label
inside and then set AutoSizeMode = GrowAndShrink
and AutoSize = true
.
Two problems will arise:
- There is a huge gap between
Label
and bottom ofGroupBox
(almost enough to fit anotherLabel
lol); AutoSize
doesn't respect theGroupBox.Text
property.
Question is how to make GroupBox.AutoSize
working properly? Properly means: minimum Width should be enough to fit GroupBox.Text
, there should be no gaps below for unknown reason (it's not Margin
, nor Padding
and it looks pretty ugly).
I've tried to measure string length in OnPaint
and setting MinimumSize
right there. It works, but I have doubts about this, as if I would want to actually set MinimumSize
later - it will be lost after repaint.
Update, here is screenshot:
It's simple that the location of your
Label
is fixed at some point other than(0,0)
, try this:You may also want to try setting the
Padding
of yourGroupBox
to0
for all (default is 3):It seems as though the
GroupBox
control has a predefined padding of sorts when growing the control ifAutoSize = true
. That is, once a control (inside the GroupBox) gets within 20 pixels or so of the bottom of the GroupBox, the GroupBox starts growing. This causes a 20 pixel or so padding from the bottom of the bottom-most control to the bottom of the GroupBox (as highlighted in yellow by @Sinatr's attached image).Based on my observations, the padding seems to be less when growing the
Width
of the GroupBox.At any rate, you can do something like the following "get around" the issue:
You can get rid of the unwanted yellow space at the bottom by deriving a new class from GroupBox that adjusts the bottom edge a bit. In VB something like ...