radio buttons (array)

J

juli

Hello,
I need a control which will contain radio buttons that will be added
in a loop.
I am using this control a source of some other control.
I tried to use group box windows control and to add the radio buttons
this way(in a loop):
this.groupBox1.Controls.Add(radio);

but I can see there only one radio button . How can I see all of them
or maybe I should use some kind of radio buttons array first?
Thanks a lot!
 
T

Tom Clement

Hi Juli,

The first question is: Where do you want them to appear?

What I'd do is this:

1. Create a user control with three ratio buttons on it, spaced about how
you would like them.
2.. Open up the "#region Designer generated code" region
3.. Take a look at the exact code related to the radio buttons. This
includes a. the initialization of each ratio button. b. the assignment of
properties to the radio button and c. the addition of the ratio buttons to
it's parents controls collection.
4. Write a function with a loop that does the same thing.

Off the top of my head, you might end up with something like this:

private void AddButtons(Control parent, int count)
{
for( int i = 0; i < count; i++ )
{
RadioButton rb = CreateRadioButton(int i,
string.Format("DoWhatever{0}", i) );
parent.Controls.Add(rb);
}
}
private RadioButton CreateRadioButton(int i, string text)
{
RadioButton rb = new RadioButton();
this.rb.BackColor = System.Drawing.Color.White;
this.rb.Checked = (i == 0)? true: false;
this.rb.Location = new System.Drawing.Point(10, 10 + (20 * i));
this.rb.Name = "rb" + i.ToString();
this.rb.Size = new System.Drawing.Size(100, 20);
this.rb.TabIndex = i;
this.rb.TabStop = true;
this.rb.Text = text;
this.rb.CheckedChanged += new
System.EventHandler(this.rb_CheckedChanged);
return rb;
}
private void rb_CheckedChanged(object sender, System.EventArgs e)
{
if( sender.Name == "rb1" ) // Etc.
{
// Do whatever you want to do
}
}
 
B

Bruce Wood

Frank is most likely correct. Did you change the .Location property of
each radio button so that it was at different coordinates within the
group box?
 
J

juli jul

Hei,
Thank you all .About the location I did tried to change it but I think I
am not doing it corectly. How exactly I suppose to do it?

I need to add those radio buttons to not a windows control but I need
them to be in a windows control.
 
B

Bruce Wood

myRadioButton.Location = new Point(5, y);
Then increment y for some value (like 20) between each radio button.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top