Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GroupBox autosize

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

like image 833
Sinatr Avatar asked Aug 19 '13 07:08

Sinatr


2 Answers

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
like image 95
Jerry Avatar answered Sep 20 '22 13:09

Jerry


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);
like image 34
King King Avatar answered Sep 16 '22 13:09

King King