I have a c# app (winform) that generates Groupboxes from an xml file and based on what's in the file, populates it with Radio Buttons or CheckBoxes. Each of these groupboxes have a name, some of them are longer and they get cut off half way through.
This is how they are generated.
int nc = groupNodes.Count;
for (int i = 0; i < nc; i++)
{
node = groupNodes[i];
GroupBox box = new GroupBox();
box.AutoSize = true;
box.AutoSizeMode = AutoSizeMode.GrowAndShrink;
box.Text = node.Attributes["name"].Value;
//......
}
I tried using the following,
Size textSize = TextRenderer.MeasureText(box.Text,box.Font);
box.Width = (int)textSize.Width;
and tried the following
box.width = (int)box.text.length;
but none of this made any difference.
I also came across This thread. But since I don't use the PaintEventArgs
I'm not sure how this applies to me.
Setting the groupbox width is only one of your problems.
It probably should be done like this:
groupBox.AutoSize = true;
int oWidth = groupBox1.Width;
int tWidth = (int)groupBox.CreateGraphics().
MeasureString(groupBox.Text, groupBox.Font).Width;
if (tWidth > oWidth)
{
groupBox.AutoSize = false;
groupBox.Width = tWidth;
}
Note:
AutoSize
property. Which will make the GB wide enough to hold its content ie. the RadioButtons&CheckBoxes with their texts. Therefore these should be set before adjusting the GB sizes.AutoSize
true for some GB and false for others..FlowLayoutPanel
!Add
them to the GBs, right? If some don't show, post the code you create them with!So the order is this: