Windows Develop Bookmark and Share   
 index > Windows Forms General > Validating Event of Radio Button
 

Validating Event of Radio Button

What is the use of validating event of a radio button?

In a form if I have 2 radio buttons how can I check whether one is clicked?

I want to do this using the validating event of the radio button is it possible?

What should be the coding for that?

I have tried it doing in the button_click event. But if Iwant to validate the data entered by the user before the button is clicked how can I do?


SNK08  Wednesday, March 11, 2009 4:40 PM
You need to put the radio buttons inside a container. A GroupBox is the most natural choice. You can then use the container's Validating event to display the warning. For example:

private void groupBox1_Validating(object sender, CancelEventArgs e) {
if (!radioFemale.Checked && !radioMale.Checked) {
e.Cancel = true;
this.BeginInvoke(new MethodInvoker(sexProblem));
}
}
private void sexProblem() {
MessageBox.Show("Please select your preference");
}

You'll need to use the BeginInvoke() hoopla to avoid the nasty focus problems you'll get when MessageBox steals the focus away just as it is changing.

Hans Passant.
nobugz  Sunday, March 15, 2009 3:26 PM
This is what I usually do for my radio buttons:

//Willfirenomatterwhichoneyoucheck,causethereareonlytwosotheybothchange
privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
{
//Togetvalueuse
boolvalue=radioButton1.Checked;
//Checkifvalueisinvalidhoweveryouwant
if(valueIsInvalid)
{
//Disableevent
radioButton1.CheckedChanged-=newEventHandler(radioButton1_CheckedChanged);
//Settoopposite
if(value)
radioButton2.Checked=true;
else
radioButton1.Checked=true;
radioButton1.CheckedChanged+=newEventHandler(radioButton1_CheckedChanged);
}
}

Probably would be better to use Validating event though (if radiobutton has it) so you can justuse e.Cancel instead of having to -= and += the eventhandler.
ScottyDoesKnow  Wednesday, March 11, 2009 4:58 PM
Thanks for your reply.

But when I use the above code,if by mistake I select radio button1 instead of radio button2 it is not possible to check the correct button after clicking on the wrong button.

How the code should be modified to do this?
SNK08  Thursday, March 12, 2009 5:23 AM
Sorry I don't understand what you need, this is how my code works:

Form loads, one of the radio buttons is checked.
User tries to check the other radio button:
if valid it works
if invalid it sets it back to the first radio button

What do you want done differently?
ScottyDoesKnow  Thursday, March 12, 2009 4:15 PM

Hi SNK08,

RadioButton and CheckBox only have two status, checked and unchecked. The Control.Validating event does fire when RadioButton lost focus.

If I put a checkBox1 and a radioButton1 on my form, if I don't want radioButton1 to be checked when checkBox1 is not checked, I can handle the Validating event of radioButton1. Code like this.

publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
radioButton1.Validating+=newCancelEventHandler(radioButton1_Validating);
}
voidradioButton1_Validating(objectsender,CancelEventArgse)
{
if(checkBox1.Checked==false)
{
radioButton1.Checked=false;
}
}
}


Since the Validating event of radioButton1 only occurred when it lost focus, when you change the checked status of checkBox1, Validating won't fire, so the hole remain in your application. To avoid all invalid input, you need to do more value check instead of just handle the Validating event of RadioButton.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, March 13, 2009 6:40 AM
Thanks for your reply.

For my application I have two radio buttons. I want to check whether any one of the radio button is selected or not. If none of the radio button is selected it should display an error message stating " Error !!! Please Select Any One Option"How can I do?

What should I do to validate two radio buttons named: rdbMale and rdbFemale.?

Please Help me.




SNK08  Sunday, March 15, 2009 6:48 AM
You need to put the radio buttons inside a container. A GroupBox is the most natural choice. You can then use the container's Validating event to display the warning. For example:

private void groupBox1_Validating(object sender, CancelEventArgs e) {
if (!radioFemale.Checked && !radioMale.Checked) {
e.Cancel = true;
this.BeginInvoke(new MethodInvoker(sexProblem));
}
}
private void sexProblem() {
MessageBox.Show("Please select your preference");
}

You'll need to use the BeginInvoke() hoopla to avoid the nasty focus problems you'll get when MessageBox steals the focus away just as it is changing.

Hans Passant.
nobugz  Sunday, March 15, 2009 3:26 PM
Hi nobugz,

Thanks for your reply.

When I execute the code given by you it does not display any message box when I dont select any one of the radio button.

Should I make any changes in the properties.
SNK08  Sunday, March 15, 2009 4:05 PM
Debug it. Set a breakpoint on the Validating event handler. If it doesn't break, make sure you actually added the event. Select the group box, click the lightning bolt icon in the Properties Window and double-click Validating.
Hans Passant.
nobugz  Sunday, March 15, 2009 4:13 PM
Hi Nobugz,

Thanks for your reply.

When I m inserting a BreakPoint it is breaking at the Run Time. I have included the event handler.

What should I do?
SNK08  Sunday, March 15, 2009 5:08 PM
Keep debugging, single-step after the breakpoint hit. Verify it sets e.Cancel to true. Also set a breakpoint on the MessageBox.Show() statement.

Have you set the radio buttons' AutoCheck property to false?

Hans Passant.
nobugz  Sunday, March 15, 2009 5:22 PM

You can use google to search for other answers

Custom Search

More Threads

• Checkedlistbox
• Sorting/Arranging Rectangles or Controls in Runtime
• How can I add the last part of the string which is before the comma to the new string?
• How could I acheive this effect?
• Filter/Search records in DataGridView?
• Manipulating background color, size etc. of a textbox that was created in runtime
• Image on MDI Form
• Radio Button as an On/Off Toggle?
• The Color class's FromArgb method
• Recources, images