问题
I have two problems with the GroupBox
, they appears after setting GroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink
and GroupBox.AutoSize = true
.
GroupBox.Text
width is not taken into account at all. Sizing will occurs to fit content only and then text will get wrapped if it doesn't fit. If it cannot fit - it is simply not displayed.There is unnecessarily big gap between bottom of the
GroupBox
andLabel
inside.
Questions:
How to make GroupBox
respecting its Text
property when autosizing? And how to remove that gap?
For some reasons my previous question gets on hold. Should I delete it or what?
P.S.: if you are putting on hold or something, please comment what is exactly not-clear in what I am asking!
回答1:
/*
Calculate the Text Width in pixels, then set the size for the GroupBox.
*/
groupBoxA.SuspendLayout();
SizeF stringSizeLabel;
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
Font stringFont = new Font("Microsoft Sans Serif", 8.25F);
stringSizeLabel = graphics.MeasureString("SAMPLE TEXT", stringFont);
}
int iWidth = (int)(stringSizeLabel.Width * 1.35f); // Give a little extra width
int iHeight = 78; // This is a sample value
groupBoxA.Size = new System.Drawing.Size(iWidth, iHeight);
groupBoxA.MinimumSize = new System.Drawing.Size(iWidth, iHeight);
groupBoxA.ResumeLayout(false);
groupBoxA.PerformLayout();
来源:https://stackoverflow.com/questions/18337421/groupbox-autosize-and-groupbox-text-wrapping