简体   繁体   中英

Message box if no radio button selected in c#?

Hi I was just wondering how to prompt an error if no radio button is selected? I am adding a lot of entries into a listview and I have it so you can select either Fixed or Zero (working with bonds) and I want that if neither is selected, a messagebox shows up. I tried:

if (rdbtn_Fixed == null && rdbtn_Zero == null)
        {
            MessageBox.Show("Please select either Fixed or Zero coupon Type");
            return;
        }

But when I click the button to add to the listview, no error prompts up for some reason!!

Any help would be greatly appreciated, thanks!

Damian

Check if radio is checked or not instead of checking if it is null. The radio button would not be null as they are being instantiated and wont be null. You can put condition on RadioButton.Checked property to check if radio button is selected.

if (!rdbtn_Fixed.Checked &&  !rdbtn_Fixed.Checked)
{
      MessageBox.Show("Please select either Fixed or Zero coupon Type");
      return;
}

If the controls rdbtn_Fixed and rdbtn_Zero are on your form/page, then, all else being equal, they won't be null ; in order to determine if a RadioButton is selected you should use the apporiate property : RadioButton.Checked .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM