• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

How to get the tag from a series of radio buttons

I have 8 radio buttons in a group box.  I want to get the tag of the checked radio button, without doing a series of 'if' statements.  How do I do this using 'switch'?
0
rmmarsh
Asked:
rmmarsh
  • 4
  • 3
  • 2
  • +1
1 Solution
 
HainKurtSr. System AnalystCommented:
"get the tag of the checked radio button" : need more info...

* what tags are you trying to get
* do you want this on client (JS) or server (C#)
0
 
rmmarshAuthor Commented:
I put a tag (a number) on each of the radio buttons... that's what I want to get... and it's not .ASP , it's a desktop C# .NET program...
0
 
lenordisteCommented:
unfortunately you will have to look through your radio buttons and use a IF to find which one is checked. What alternative though would be to use LINQ, which is more elegant but pretty much does the same thing:
var checkedRadioButton = myGroupBox.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
var myTag = checkedRadioButton.Tag;
or shorter:
var myTag = myGroupBox.Controls.OfType<RadioButton>().Where(r=>r.Checked).FirstOrDefault(r => r.Tag);

which should return a refrerence to your checked radio button or a null reference if none of them are checked. This assumes however that all the radio buttons of your groupbox are indeed linked. If not there are a lot of ways you can work around it: for instance have a naming scheme like "groupBox1Rad1","groupBox1Rad2","groupBox1Rad3" and then use this :
var myTag = myGroupBox.Controls.Where(r=>r.Checked&&r.Name.StartsWith("groupBox1").FirstOrDefault(r => r.Tag);

Good luck!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HainKurtSr. System AnalystCommented:
here is a sample

if you select 3rd option
st = 'C'
sv = 'c'
    <asp:RadioButtonList ID=rbList runat=server>
    <asp:ListItem Value=a Text=A />
    <asp:ListItem Value=b Text=B />
    <asp:ListItem Value=c Text=C />
    <asp:ListItem Value=d Text=D />
    </asp:RadioButtonList>

...

            ListItem si = rbList.SelectedItem;
            string st = si.Text;
            string sv = si.Value;

Open in new window

0
 
HainKurtSr. System AnalystCommented:
ignore above post since it is valid for ASP.Net
0
 
rmmarshAuthor Commented:
This is the sort of thing I want to do...  gbReportTime is the groupbox, Controls are the 8 radio buttons in the groupbox.

            //  now determine which radio button was checked
            switch ( (RadioButton) gbReportTime.Controls.    ?????????????


0
 
lenordisteCommented:
You won't be able to use a switch in that kind of scenario. I think there are two options:
1) loop though the controls and evaluate the Checked property of your radio buttons
2) use linq to retrieve the tag of the checked radio button:
var myTag = gbReportTime.Controls.OfType<RadioButton>().Where(r=>r.Checked).FirstOrDefault(r => r.Tag);
0
 
rmmarshAuthor Commented:
I guess I'm gonna learn LINQ now... thanks for the help everybody...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just wire the CheckedChanged() event of ALL the RadioButtons to the SAME method:
        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton2.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton3.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton4.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton5.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton6.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton7.CheckedChanged += new EventHandler(rb_CheckedChanged);
            radioButton8.CheckedChanged += new EventHandler(rb_CheckedChanged);
        }

        void rb_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked)
            {
                Console.WriteLine(rb.Name + " was checked!");
                // grab the tag value...if you assigned this in the IDE it's a STRING
                string tagValue = (string)rb.Tag;
                Console.WriteLine("Tag = " + tagValue);
            }
        }

Open in new window

0
 
rmmarshAuthor Commented:
Idle_Mind:  I'm sorry I already awarded points -- your solution is EXACTLY what I was looking for... thank you so much!

Is there anyway I can split the point when they have already been given?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You'd have to send a request to a moderator to re-open the question so you can assign points differently...

...but don't worry about it as I wouldn't have posted in a CLOSED question if all I wanted was points.

I answer thousands of questions here...points come and go!  =)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now