7

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 of GroupBox (almost enough to fit another Label lol);
  • AutoSize doesn't respect the GroupBox.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:

enter image description here

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • It's obviously that the `label1` Location is not `Point(0,0)`? You didn't even try my solution. – King King Aug 20 '13 at 07:51
  • Yes i tried, and no it doesn't work. I told this in the answer itself. Want me to post another screenshot where `GroupBox` is collapsed (because nothing then prevent `AutoSize` from reducing size of parent to zero). Tell me please, what should I do now with this question? I have an issue, obvious, unsolved, coming from Microsoft, but my question is on hold, so no one will ever bother to answer on it. Re-post it again? – Sinatr Aug 20 '13 at 13:56

3 Answers3

5

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 ...

Public Class BetterGroupBox
    Inherits GroupBox

    Public Overrides Function GetPreferredSize(ByVal proposedSize As Size) As Size
        Dim ns = MyBase.GetPreferredSize(proposedSize)
        Return New Size(ns.Width, ns.Height - 15)
    End Function

End Class
Jerry
  • 393
  • 4
  • 8
1

It's simple that the location of your Label is fixed at some point other than (0,0), try this:

label1.Location = Point.Empty;

You may also want to try setting the Padding of your GroupBox to 0 for all (default is 3):

groupBox1.Padding = new Padding(0);
King King
  • 61,710
  • 16
  • 105
  • 130
  • Doesn't work. Setting this and then `GroupBox.AutoSize = true` will collapse group box to have 1 pixel width or something. Interesting enough, the **height** of collapsed group box will same bigger than needed (to fit ~one more `Label` under `label1`). How this answer suppose to fix second issue (with `GroupBox.Text`) ? – Sinatr Aug 19 '13 at 08:06
  • @Sinatr please post your screen shot. I've tried with this and it `shrinks` almost to the size of the inner `Label`. – King King Aug 19 '13 at 08:18
  • @Sinatr the size is resized depending on the text of `GroupBox`, isn't that better? – King King Aug 20 '13 at 07:12
1

It seems as though the GroupBox control has a predefined padding of sorts when growing the control if AutoSize = 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:

    public void MyFunction()
    {
        groupBox1.AutoSize = true;

        // Do stuff (e.g., add controls to GroupBox)...

        // Once all controls have been added to the GroupBox...
        groupBox1.AutoSize = false;

        // Add optional padding here if desired.
        groupBox1.Height = myBottomMostControl.Bottom;
    }
VineAndBranches
  • 468
  • 3
  • 7