471,075 Members | 715 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Creating GroupBox with Radio Buttons from enum using reflection

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
Jun 27 '08 #1
4 6349
On Fri, 11 Apr 2008 17:55:06 GMT, "Gugale at Lincoln"
<gu**************@gmail.comwrote:
>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.
Jun 27 '08 #2
On Fri, 11 Apr 2008 10:55:06 -0700, Gugale at Lincoln
<gu**************@gmail.comwrote:
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.
You need to set the AutoSize property of all of your relevant containers
-- 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
Jun 27 '08 #3
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" <jj******@cinnovations.netwrote in message
news:bp********************************@4ax.com...
On Fri, 11 Apr 2008 17:55:06 GMT, "Gugale at Lincoln"
<gu**************@gmail.comwrote:
>>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.

Jun 27 '08 #4
It worked. Thanks!

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 11 Apr 2008 10:55:06 -0700, Gugale at Lincoln
<gu**************@gmail.comwrote:
>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.

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

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mr.Clean | last post: by
1 post views Thread by Agnes | last post: by
8 posts views Thread by johnmmcparland | last post: by
2 posts views Thread by K Forester | last post: by
3 posts views Thread by Sin Jeong-hun | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.