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

Winforms C# VS2008 - radiobuttons loop for checked value

I have 7 radio buttons on a GroupBox labeled radioButton1-7...
How can I quickly retrieve the text value of the selected radiobox?
0
conrad2010
Asked:
conrad2010
  • 3
  • 2
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The Looping method:

            foreach(Control ctl in this.groupBox1.Controls)
            {
                if (ctl is RadioButton)
                {
                    if (((RadioButton)ctl).Checked)
                    {
                        this.label1.Text = ctl.Text;
                        break;
                    }
                }
            }

Another option would be to wire up all of the RadioButtons to a common handler and store the current value in a form level variable:

        string curValue = "";

        private void rb_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked)
            {
                this.curValue = rb.Text;
            }
        }
0
 
Jammer59Commented:
I assigned an CheckedChange event to all my radio buttons and then when the checkbox is selected, I assign the radiobutton Text property to a string variable which I can use within my code.
See attached Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace PolicyXpress1
{
 
    public partial class ProcessDelimited : Form
    {
        private string radioButtonValue = "";
 
        public ProcessDelimited()
        {
            InitializeComponent();
        }
 
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            // Determine if the checkbox is checked.
            Boolean blnChecked = ((RadioButton)sender).Checked;
 
            if (blnChecked)
            {
                radioButtonValue = ((RadioButton) sender).Text;
                MessageBox.Show(radioButtonValue);
            }
        }
 
 
    }
}

Open in new window

0
 
conrad2010Author Commented:
so I would do that for all 7 radio buttons?

radioButton1_CheckedChanged
radioButton2_CheckedChanged

etc?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
conrad2010Author Commented:
ok, sorry, just now  read the 2 lines of text above the solution in which you explained...
0
 
Jammer59Commented:
The easy way to do it is to select all your radiobuttons at once and then go to Properties. Within properties, click the lightning bolt to bring up a list of events.  Within the events, locate the CheckedChanged event and double click.   A CheckedChanged event will be added to your code.  This same event will be used by all 7 of your buttons.  Enter code similar to what Idle_Mind or I have added.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want to wire them up at run-time then you could do:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        string curValue = "";
 
        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in this.groupBox1.Controls)
            {
                if (ctl is RadioButton)
                {
                    RadioButton rb = (RadioButton)ctl;
                    rb.CheckedChanged +=new EventHandler(rb_CheckedChanged);
                }
            }
        }
 
        private void rb_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked)
            {
                this.curValue = rb.Text;
            }
        }
 
    }

Open in new window

0
 
conrad2010Author Commented:
I liked the looping method
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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