Creating GroupBox with Radio Buttons from enum using reflection

G

Gugale at Lincoln

Hi,

I am using reflection to read names from enum and creating radio buttons
inside a groupbox. Everything is working fine. However, I am not able to
adjust the size of the group box to fit the content. Is there a simple
solution to this problem? Parent of group box is FlowLayoutPanel which fills
the parent form.

string[] names = Enum.GetNames(pi.PropertyType); //pi is a PropertyInfo
object which represents a property of type enum.

GroupBox gbx = new GroupBox();
//gbx.AutoSize = true;
gbx.Name = "gbx" + pi.Name;
gbx.Text = pi.Name;
gbx.Padding = new Padding(2, 5, 2, 2);
panel.Controls.Add(gbx);

TableLayoutPanel tlp = new TableLayoutPanel();
tlp.ColumnCount = (int)Math.Floor(Math.Sqrt(names.Length));
tlp.Dock = DockStyle.Fill;
tlp.Padding = new Padding(1);

//int maxRBLength = 0;
foreach (string name in names)
{
RadioButton rb = new RadioButton();
rb.Name = "rb" + name;
rb.Text = name;
rb.AutoSize = true;
rb.Margin = new Padding(0, 0, 0, 0);
//if (rb.Width > maxRBLength) maxRBLength = rb.Width;
tlp.Controls.Add(rb);
}

gbx.Controls.Add(tlp);


Thanks
SG
 
J

Jack Jackson

Hi,

I am using reflection to read names from enum and creating radio buttons
inside a groupbox. Everything is working fine. However, I am not able to
adjust the size of the group box to fit the content. Is there a simple
solution to this problem? Parent of group box is FlowLayoutPanel which fills
the parent form.

string[] names = Enum.GetNames(pi.PropertyType); //pi is a PropertyInfo
object which represents a property of type enum.

GroupBox gbx = new GroupBox();
//gbx.AutoSize = true;
gbx.Name = "gbx" + pi.Name;
gbx.Text = pi.Name;
gbx.Padding = new Padding(2, 5, 2, 2);
panel.Controls.Add(gbx);

TableLayoutPanel tlp = new TableLayoutPanel();
tlp.ColumnCount = (int)Math.Floor(Math.Sqrt(names.Length));
tlp.Dock = DockStyle.Fill;
tlp.Padding = new Padding(1);

//int maxRBLength = 0;
foreach (string name in names)
{
RadioButton rb = new RadioButton();
rb.Name = "rb" + name;
rb.Text = name;
rb.AutoSize = true;
rb.Margin = new Padding(0, 0, 0, 0);
//if (rb.Width > maxRBLength) maxRBLength = rb.Width;
tlp.Controls.Add(rb);
}

gbx.Controls.Add(tlp);


Thanks
SG

It doesn't work if groubbox.AutoSize = True? If the groupbox is too
large make sure AutoSizeMode is set to GrowAndShrink.
 
G

Gugale at Lincoln

No it doesn't. It just shrinks to a dot. I think it is because I am using
TableLayoutPanel inside the group box. But I have no other options.


Jack Jackson said:
Hi,

I am using reflection to read names from enum and creating radio buttons
inside a groupbox. Everything is working fine. However, I am not able to
adjust the size of the group box to fit the content. Is there a simple
solution to this problem? Parent of group box is FlowLayoutPanel which
fills
the parent form.

string[] names = Enum.GetNames(pi.PropertyType); //pi is a
PropertyInfo
object which represents a property of type enum.

GroupBox gbx = new GroupBox();
//gbx.AutoSize = true;
gbx.Name = "gbx" + pi.Name;
gbx.Text = pi.Name;
gbx.Padding = new Padding(2, 5, 2, 2);
panel.Controls.Add(gbx);

TableLayoutPanel tlp = new TableLayoutPanel();
tlp.ColumnCount = (int)Math.Floor(Math.Sqrt(names.Length));
tlp.Dock = DockStyle.Fill;
tlp.Padding = new Padding(1);

//int maxRBLength = 0;
foreach (string name in names)
{
RadioButton rb = new RadioButton();
rb.Name = "rb" + name;
rb.Text = name;
rb.AutoSize = true;
rb.Margin = new Padding(0, 0, 0, 0);
//if (rb.Width > maxRBLength) maxRBLength = rb.Width;
tlp.Controls.Add(rb);
}

gbx.Controls.Add(tlp);


Thanks
SG

It doesn't work if groubbox.AutoSize = True? If the groupbox is too
large make sure AutoSizeMode is set to GrowAndShrink.
 
G

Gugale at Lincoln

It worked. Thanks!

Peter Duniho said:
You need to set the AutoSize property of all of your relevant
ontainers -- GroupBox, TableLayoutPanel, and possibly the FlowLayoutPanel
and the containing Form as well -- to "true". Once you've done that, it
should work fine.

The GroupBox won't know to resize unless the TableLayoutPanel also
resizes. If your form is large enough always, then the GroupBox's parents
should be fine, but if not, they need to also be set to resize by setting
their AutoSize property to "true".

Pete
 
Top