Checking a radiobutton

Asked

Viewed 43 times

0

Good morning ! I created a small system that uses some radio button components, but when I do not mark any the system generates an error, someone knows how can I make a conference call if some radio button has been marked. Below is the code:

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));
  • What exactly does the error generate? Better if it’s the whole stacktrace.

  • You should probably be running and the variable rbnTurno should be null because it has no radio selected, hence when you pass to the method RealizarProcessamento error. You have to treat this, and even display a message if applicable.

  • Hello ! I was able to solve, and was just treating to check if there is a radio button selected, then it worked out. thanks...

1 answer

1

You can use a lambda expression for that.

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);
  • It worked perfectly, thank you very much.

Browser other questions tagged

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