Force user to select Radio Button inside the Groupbox

Asked

Viewed 378 times

1

How do I force user to select a Radio Button inside the Groupbox.

Example of the image below

Imagem1

  • Couldn’t put a preselected one?

  • It would be interesting too, but if there was the possibility of playing an alert if the user forgot.

  • @Danillovictor Did my answer solve your problem? If yes, consider marking it as accepted.

  • Ready, marked!

1 answer

4


Using LINQ can stay like this:

using System.Linq;

// gbRadioButtons - Nome do seu groupBox que contém os radio buttons.        
var anySelected = gbRadioButtons.Controls.OfType<RadioButton>().Any(x => x.Checked);

if(!anySelected) // Se nenhum foi selecionado
{
   MessageBox.Show("Por favor, selecione um item.");
}

In the code above, I’m using LINQ to iterate over all type items RadioButton within your GroupBox and look if at least some RadioButton was selected and write this value to the variable anySelected.

  • Could put right if(!gbRadioButtons.Controls.OfType<RadioButton>().Any(x => x.Checked)), could not?.

  • 1

    I considered this, but decided to put in a separate variable to better organize the example and keep it more readable.

  • 1

    It’s much more readable the way you did it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.