Create list of radio button in C#?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Create list of radio button in C#?

  1. #1
    Join Date
    Jun 2004
    Location
    Vietnam
    Posts
    103

    Smile Create list of radio button in C#?

    hi all
    Does anyone know how to create a list of radio button in C#?
    Say, I try to control radio buttons in group with index or address (use pointer), just like in VC++. I'm bored with using "if... else..." command line to check if one radio button is checked or not.
    Thanks in advance

  2. #2
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    5550' N 3739' E
    Posts
    1,503

    Re: Create list of radio button in C#?

    Just put them inside a Group Box and you dont have to worry about that.
    then you can simply assign a single click event and get the clicked RadioButton by simply casting sender to radiobutton/
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  3. #3
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Create list of radio button in C#?

    At a guess:

    Put all related radio buttons in a Panel or GroupBox.
    Attached all button's CheckChanged event to the same handler method.
    In the handler code cast sender to a RadioButton and access its Checked property.

    [Edited: Looks like Andy beat me to it]
    Useful? Then click on (Rate This Post) at the top of this post.

  4. #4
    Join Date
    Jun 2004
    Location
    Vietnam
    Posts
    103

    Question Re: Create list of radio button in C#?

    Thanks for yours replies
    But I'm not so clear about what you guys posted.
    Well, it's better if I say more clearly what I suppose to do.
    In VC++, for example, I could use radio group to get value with RadioButton's variable (int):
    Code:
    CString str[3];
    str[0] = "Me";
    str[1] = "You";
    str[2] = "Him";
    MessageBox(str[m_iRadioButton]); // Show string with the index of radio button which is checked
    That's so simple. But in C#:
    Code:
    // Check 3 radio buttons
     if(radioButton1.Checked)
     {
            MessageBox.Show(str[0]);
     }
     else if(radioButton2.Checked)
     {
            MessageBox.Show(str[1]);
     }
     else if(radioButton3.Checked)
     {
            MessageBox.Show(str[2]);
     }
    In VC++, there is one (and only one) variable of radio group (created by class wizad), which is used to handle, or to determine, which one was checked. But in C#, I guess we have to create this variable ourself. So I think that might be a list or a group of radio button. And with this I could do something just like in VC++:
    Code:
    MessageBox.Show(str[radioGroup.Index]) // For example
    Any idea? Thacks in advance

  5. #5
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Create list of radio button in C#?

    Hi duongpv,

    have you actually tried this in C#?

    A RadioButton is not simply an index.

    In the event handler for a Radio button:
    Code:
    RadioButton button = sender as RadioButon;
    
    if (button.Checked) MessageBox.ShowMsg(button.Text);
    Useful? Then click on (Rate This Post) at the top of this post.

  6. #6
    Join Date
    Jun 2004
    Location
    Vietnam
    Posts
    103

    Re: Create list of radio button in C#?

    Thanks Norfy
    It seems the best way I've known, even that's not exact what I thought. But that's fine.
    Thank you all again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center