Answered by:
c# mouseclick show which radiobuttons are checked

Question
-
I'm sure this is something simple.
I have a form with about a dozen plus Radio Buttons, all in the same group. I am trying to capture the name of the radio button that is selected if you click anywhere on the form.
private void MouseClick(object sender, MouseEventArgs e) { RadioButton btn = sender as RadioButton; Messbox.Show(//Selected button); }
I've tried using a switch statement but I am getting an error.
Any suggestions would be greatly appreciated
Thursday, October 18, 2012 5:41 PM
Answers
-
Loop through all the radioButtons and check if Checked property is true:
StringBuilder sb = new StringBuilder(); foreach(RadioButton rb in groupBox1.Controls.OfType<RadioButton>()) { if(rb.Checked) { //if selected, add the name of radioButton tostringbuilder sb.AppendLine(rb.Name); } } MessageBox.Show("Selected RadioButtons are:\r\n"+ sb.ToString());
Mitja
- Proposed as answer by Enrique Ferreyra Thursday, October 18, 2012 6:47 PM
- Marked as answer by Munkee Works Tuesday, October 23, 2012 5:59 AM
Thursday, October 18, 2012 5:53 PM -
Got it to work.
changed it to work if I click anywhere in the group box. had to add
this.groupBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button1_Click);
Thanks for you help.- Marked as answer by Munkee Works Wednesday, October 31, 2012 5:03 AM
Tuesday, October 23, 2012 6:00 AM
All replies
-
Loop through all the radioButtons and check if Checked property is true:
StringBuilder sb = new StringBuilder(); foreach(RadioButton rb in groupBox1.Controls.OfType<RadioButton>()) { if(rb.Checked) { //if selected, add the name of radioButton tostringbuilder sb.AppendLine(rb.Name); } } MessageBox.Show("Selected RadioButtons are:\r\n"+ sb.ToString());
Mitja
- Proposed as answer by Enrique Ferreyra Thursday, October 18, 2012 6:47 PM
- Marked as answer by Munkee Works Tuesday, October 23, 2012 5:59 AM
Thursday, October 18, 2012 5:53 PM -
Thanks for the reply
I've tried this, I just get a message box with "Selected RadioButtons are:" Not quite sure what's going on.
Tuesday, October 23, 2012 3:56 AM -
Got it to work.
changed it to work if I click anywhere in the group box. had to add
this.groupBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button1_Click);
Thanks for you help.- Marked as answer by Munkee Works Wednesday, October 31, 2012 5:03 AM
Tuesday, October 23, 2012 6:00 AM