GroupBox.AutoSize and GroupBox.Text wrapping

我怕爱的太早我们不能终老 提交于 2019-12-23 03:21:53

问题


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 and Label 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!