GroupBox autosize

后端 未结 3 738
深忆病人
深忆病人 2020-12-11 06:14

Take a GroupBox, put let say Label inside and then set AutoSizeMode = GrowAndShrink and AutoSize = true.

Two prob

相关标签:
3条回答
  • 2020-12-11 06:40

    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;
        }
    
    0 讨论(0)
  • 2020-12-11 06:49

    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);
    
    0 讨论(0)
  • 2020-12-11 06:51

    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
    
    0 讨论(0)
提交回复
热议问题