1

I have two problems with the GroupBox, they appears after setting GroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink and GroupBox.AutoSize = true.

GrouBox bug

  • 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!

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • the `GroupBox` is designed to autosize accordingly to its children(child controls), not its `Text`. – King King Aug 20 '13 at 14:21
  • .. and there is no way to deal with it? Currently I have to make own control (based on `GroupBox`), draw everything, but I have difficulty to deal with build-in `AutoSize` and none of my solutions satisfy me: calculating size in `OnPaint` and setting `MinimumSize`, using parent container/form `OnVisibleChanged` event to set `MinimumSize` again, using not-autosized GroupBox, but size it manually. I want to have nice automatically working control, yet can not understand how to deal with that *gap-bug*. – Sinatr Aug 20 '13 at 14:31
  • @Sinatr we all want that, but that's not going to happen. try registrating the resize event maybe and undo it or stop the resize – No Idea For Name Aug 20 '13 at 14:50
  • Maybe you can use a LayoutPanel in your groupbox? – CristisS Aug 20 '13 at 14:52
  • @NoIdeaForName, so I shouldn't use `AutoSize` and rather manually set size based on content? Wierd... @CristisS, it doesn't matter what is inside, the `GroupBox` will keep *gap* between *that* and bottom line for unknown reasons. – Sinatr Aug 20 '13 at 15:05
  • You can't escape the wpf trolls here. You might have better luck at the MSDN forums, they have a dedicated forum for winforms questions. – Hans Passant Aug 20 '13 at 18:24
  • @HighCore, WPF is out of the question yet. I do like stackoverflow more than MSDN and most of answers to my problems I find/get here. Seems to be uncommon issue, then.. I'll just wait for someone with the similar issue.. – Sinatr Aug 21 '13 at 06:50

1 Answers1

0
/* 
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();
Acrdiaz
  • 1
  • 1