how to check the radio button is checked or not in windows form

A.Goutam

I have 3 pair of radiobutton at my form I want to check at every group of radiobutton one radiobutton is checked when user press save button. I used the below code to check every radiobutton, but I think this one a better approach to check.

if (radioBttnAddrssCoYes.Checked || rdoBttnAddCorrectNo.Checked)
{
    if (rdoGroundFloorTrue.Checked || radioGroundFFalse.Checked)
    {
        if (rdoHomeTrue.Checked || radioMeet.Checked)
        {
            // Here I do something
        }
        else
        {
            MessageBox.Show("Please check Customer All day At Home");
        }
    }
    else
    {
        MessageBox.Show("Please Check Ground Floor Delivery");
    }
}
else
{
    MessageBox.Show("Check Addresss Correct"); 
}

Below is my form for better understanding

Thanks For your support and help

Zohar Peled

I would probably do it a little different, but it doesn't mean your way is wrong.

if(VaildateRadioGroupIsChecked(radioBttnAddrssCoYes, rdoBttnAddCorrectNo, "Check Addresss Correct") && 
   VaildateRadioGroupIsChecked(rdoGroundFloorTrue, radioGroundFFalse, "Please Check Ground Floor Delivery") && 
   VaildateRadioGroupIsChecked(rdoHomeTrue, radioMeet, "Please check Customer All day At Home")) 
{
    // do your thing...
}

private bool VaildateRadioGroupIsChecked(RadioButton a, RadioButton b, string MessageToUser) 
{
    if(!a.Checked && !b.Checked) {
        Messagebox.Show(MessageToUser);
        return false;
    }
return true;
}

Of course, you can develop it further and not send the 2 radio buttons to the function, but it's containing element. this way, if you have 3, 4, or n radio buttons you can check all of them in one simple call.

Another well known technique to validate that a radio box is checked is to simply hold a boolean variable on the form level that is false by default, and changed to true when any of the radio buttons is checked. However, this technique will not serve you well in this case, since you will need 3 boolean variables for that, and the code will be unnecessary complicated comparing to the other solutions.

To answer your question in the comment, I would probably do something like that:

string MessageToUser = VaildateRadioGroupIsChecked(radioBttnAddrssCoYes, rdoBttnAddCorrectNo, "Check Addresss Correct") + 
                       VaildateRadioGroupIsChecked(rdoGroundFloorTrue, radioGroundFFalse, "Please Check Ground Floor Delivery") +  
                       VaildateRadioGroupIsChecked(rdoHomeTrue, radioMeet, "Please check Customer All day At Home");

if(MessageToUser.Length == 0) {
    // do your stuff
} else {
    Messagebox.Show(MessageToUser);
}

private string VaildateRadioGroupIsChecked(RadioButton a, RadioButton b, string MessageToUser) 
{
    if(!a.Checked && !b.Checked) {
        return MessageToUser + "\n";
    }
return string.Empty;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to check which radio button is checked in android?

From Dev

How to check if radio is checked?

From Dev

Jquery check if radio button is checked

From Dev

Check that a specific radio button is checked

From Dev

How to check if all the radio button gropus in a division are checked or not?

From Dev

How to check if any of the Radio buttons are checked or not when clicked on submit button

From Dev

How do I check if a radio button is checked using JavaScript?

From Dev

How to set checked="checked" in first radio button?

From Dev

Using jQuery to check if radio button is not checked

From Dev

Using jQuery to check if radio button is not checked

From Dev

jQuery: Check and listen if radio button is checked

From Dev

Ionic 2 : Radio button in form, checked by default

From Dev

How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

From Dev

Submit form on radio button check

From Dev

Android - How to enable a button if a radio button is checked?

From Dev

How to render input form based on radio button checked in rails simple form

From Dev

How to check the Radio button validation using javascript in HTML form

From Dev

How to check radio button with radio button value

From Dev

check if user is logged in and then check if user was logged in with a radio button checked

From Dev

how to set checked radio button by checking another radio button

From Dev

How can I get the text of radio button if radio button checked?

From Dev

How to check if radio is checked in blade Laravel 5

From Dev

How to render checked radio button in React

From Dev

How to display checked value for radio button collection

From Dev

How to set a certain radio button to checked "true"

From Dev

How to add checked class to a radio button in AngularJS

From Dev

how to find checked radio button in a gridview?

From Dev

How to mark a radio button as checked with no action performed?

From Dev

How to set radio button checked initially in AngularJs?

Related Related

  1. 1

    how to check which radio button is checked in android?

  2. 2

    How to check if radio is checked?

  3. 3

    Jquery check if radio button is checked

  4. 4

    Check that a specific radio button is checked

  5. 5

    How to check if all the radio button gropus in a division are checked or not?

  6. 6

    How to check if any of the Radio buttons are checked or not when clicked on submit button

  7. 7

    How do I check if a radio button is checked using JavaScript?

  8. 8

    How to set checked="checked" in first radio button?

  9. 9

    Using jQuery to check if radio button is not checked

  10. 10

    Using jQuery to check if radio button is not checked

  11. 11

    jQuery: Check and listen if radio button is checked

  12. 12

    Ionic 2 : Radio button in form, checked by default

  13. 13

    How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

  14. 14

    Submit form on radio button check

  15. 15

    Android - How to enable a button if a radio button is checked?

  16. 16

    How to render input form based on radio button checked in rails simple form

  17. 17

    How to check the Radio button validation using javascript in HTML form

  18. 18

    How to check radio button with radio button value

  19. 19

    check if user is logged in and then check if user was logged in with a radio button checked

  20. 20

    how to set checked radio button by checking another radio button

  21. 21

    How can I get the text of radio button if radio button checked?

  22. 22

    How to check if radio is checked in blade Laravel 5

  23. 23

    How to render checked radio button in React

  24. 24

    How to display checked value for radio button collection

  25. 25

    How to set a certain radio button to checked "true"

  26. 26

    How to add checked class to a radio button in AngularJS

  27. 27

    how to find checked radio button in a gridview?

  28. 28

    How to mark a radio button as checked with no action performed?

  29. 29

    How to set radio button checked initially in AngularJs?

HotTag

Archive