Using GroupBox in C# - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using GroupBox in C#

PicoPico Posts: 14Member
This may sound like a silly question, but I can't seem to find the answer anywhere. I've added a bunch of radio buttons to a GroupBox and I'm trying to find out which of the buttons is selected (when a button is pressed). I don't see any property or method in the GroupBox class to obtain the RadioButton's tab index. A way to solve this problem would be to do this:
[code]
for(int i=0;i<myGroupBox.Controls.Count;i++)
{
RadioButton rb= (RadioButton)myGroupBox.Controls[i];
if(rb.checked==true)
{
//do whatever
}
}
[/code]

But the above method does not seem very elegant, so there must be a better way?
Any help would be appreciated.
Thanks.

Comments

  • BaldusariusBaldusarius Posts: 162Member
    : This may sound like a silly question, but I can't seem to find the answer anywhere. I've added a bunch of radio buttons to a GroupBox and I'm trying to find out which of the buttons is selected (when a button is pressed). I don't see any property or method in the GroupBox class to obtain the RadioButton's tab index. A way to solve this problem would be to do this:
    : [code]
    : for(int i=0;i<myGroupBox.Controls.Count;i++)
    : {
    : RadioButton rb= (RadioButton)myGroupBox.Controls[i];
    : if(rb.checked==true)
    : {
    : //do whatever
    : }
    : }
    : [/code]
    :
    : But the above method does not seem very elegant, so there must be a better way?
    : Any help would be appreciated.
    : Thanks.
    :

    There's no built in way to do this. GroupBoxes are convenient containers for RadioButtons, but they can contain any kind of control, so GroupBox properties are not specific to any type of control.

    It would be very easy to derive from it to do what you want, though.
    [code]
    public class RadioGroupBox : GroupBox
    {

    private RadioButton selected;

    public RadioGroupBox(RadioButton[] buttons)
    {
    foreach(RadioButton rb in buttons)
    {
    rb.CheckChanged += new EventHandler(rb_CheckChanged);
    }
    }

    private void rb_CheckChanged(object sender, EventArgs e)
    {
    if(((RadioButton)sender).Checked)
    selected = sender;
    }

    public RadioButton SelectedButton
    {
    get{ return selected; }
    set{ selected = value; }
    }
    }
    [/code]


  • PicoPico Posts: 14Member
    There's no built in way to do this. GroupBoxes are convenient containers for RadioButtons, but they can contain any kind of control, so GroupBox properties are not specific to any type of control.
    :
    : It would be very easy to derive from it to do what you want, though.
    : [code]
    : public class RadioGroupBox : GroupBox
    : {
    :
    : private RadioButton selected;
    :
    : public RadioGroupBox(RadioButton[] buttons)
    : {
    : foreach(RadioButton rb in buttons)
    : {
    : rb.CheckChanged += new EventHandler(rb_CheckChanged);
    : }
    : }
    :
    : private void rb_CheckChanged(object sender, EventArgs e)
    : {
    : if(((RadioButton)sender).Checked)
    : selected = sender;
    : }
    :
    : public RadioButton SelectedButton
    : {
    : get{ return selected; }
    : set{ selected = value; }
    : }
    : }
    : [/code]
    :

    Thanks Baldusarius, nice solution. 2 small problems though. I wanted an index of the selected item. This is not really a big problem as I can just set the tag property of each RadioButton that I add to RadioGroupBox and make a method to obtain this tag. The other problem is that I was working in the form designer and your solution requires me to add everything to code, and this makes positioning the RadioGroupBox and its RadioButtons rather a hassile. So the next thing I would have to do is make a custom component that can be added to my form visually. Right?

    Thanks again.
  • BaldusariusBaldusarius Posts: 162Member

    >>"2 small problems though. I wanted an index of the selected item."

    This was just an example of how to do what you're asking. You can return anything you please.

    >>"The other problem is that I was working in the form designer and >>your solution requires me to add everything to code, and this makes >>positioning the RadioGroupBox and its RadioButtons rather a hassile. >>So the next thing I would have to do is make a custom component that >>can be added to my form visually. Right?"

    There's practically no limit to what you can do if you're willing to step outside the visual tools and actually write some code. Besides, when you derive from a control, you can write it once and use it many times.

    But yes, if you'd like to manipulate the contained controls using the visual designer, you'll have to make a custom component.
Sign In or Register to comment.