Checking a radioButton

0

Good morning! I created a small system that uses some radioButton components, but when it does not set the system generates an error, someone knows how can I make a conference if any radioButton has been dialed. The following is the code below:

RadioButton rbnTurno = groupBox2.Controls.OfType<RadioButton>().SingleOrDefault(r => r.Checked);
RadioButton rbnCategoria = groupBox1.Controls.OfType<RadioButton>().SingleOrDefault(r => r.Checked);
RealizarProcessamento(rbnTurno, rbnCategoria, Convert.ToDouble(textBox2.Text), Convert.ToDouble(textBox1.Text));
    
asked by anonymous 19.07.2018 / 15:44

1 answer

1

You can use a lambda expression for this.

See the example below. It is worth mentioning that if you are using the RadioButtons inside a container, you will need to inform as I do below in groupBox1.

 var listaDeRadioButtons = groupBox1.Controls.OfType<RadioButton>().Any(x=>x.Checked);

   if (!listaDeRadioButtons)
       MessageBox.Show("Selecione um Radio Button", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
19.07.2018 / 17:35