---
title: "Working with Radio Buttons"  
description: "Working with Radio Buttons"  
author: "Royce Roy"  
published: 2014-02-09  
updated: 2014-02-09  
canonical: https://www.mindstick.com/forum/1964/working-with-radio-buttons  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Working with Radio Buttons

I have a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) which executes a code in 2 different ways based on the [radio button](https://www.mindstick.com/articles/12743/radio-button-in-android) selected(user's choice). My [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is am not able to get what user has selected, meaning the code where it is suppose to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) in different ways based on the radio button [selection](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) , that code [segment](https://www.mindstick.com/interview/1910/explain-the-difference-between-a-data-block-an-extent-and-a-segment) does not execute.

Here is what i have done :-

This is the part where i determine what user has selected from the radio buttons

public Airplane_Simulation()

{

InitializeComponent();

if (rbZero.Checked==true) //checks whether it is selected

{

[section](https://yourviews.mindstick.com/view/297/reservation-for-economically-weaker-section) = rbZero.Text; //if true assigns it's text to a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation).

}

else

if (rbOne.Checked == true)

{

section = rbOne.Text;

}

[semaphore](https://www.mindstick.com/interview/1509/difference-between-critical-section-mutex-and-semaphore) = new Semaphore();

buff = new Buffer();

btnPnlT1 = new ButtonPanelThread(new Point(40, 5), 50, pnlArrival, false, Color.Red, semaphore, buff, btnGo);

waitingTakeOff = new waitPanels(new Point(490, 5), 50, pnlTakeOff, false, Color.Green, semaphore, buff,section);

thread1 = new Thread(new ThreadStart(btnPnlT1.start));

thread3 = new Thread(new ThreadStart(waitingTakeOff.start));

thread1.Start();

thread3.Start();

}

This is the code which is supposed to execute based on the user choice

if(section.Equals("0")) //if the variable match 0 it should this code

{

for (int k = 0; k < 15; k++)

{

panel.Invalidate();

this.movePlane(xDelta, yDelta);

[Thread.Sleep](https://www.mindstick.com/forum/161218/what-is-the-role-of-thread-sleep-in-multithreading-and-when-should-it-be-avoided)(delay);

}

}

else

if (section.Equals("1")) //if variable matches 1 it

//should execute this code

{

for (int k = 0; k < 40; k++)

{

panel.Invalidate();

this.movePlane(xDelta, yDelta);

Thread.Sleep(delay);

}

I tried assigning the value directly to variable section without taking from a radio button then it worked. So i am sure it's something to do with assigning when using radio buttons.

Thank you for your time.

## Replies

### Reply by Pravesh Singh

Hi Royce,

Your only checking which one is selected when your form first opens, the user doesn't get chance to choose one, you need to create a selection changed event

By the way, else if on two lines is probably creating a nested if statement instead of an else if or else.

One way you can do this is,

public Airplane_Simulation()

{

InitializeComponent();

CheckedChanged();

rbZero.CheckedChanged += (s,e) => { CheckedChanged(); }

rbOne.CheckedChanged += (s,e) => { CheckedChanged(); }

public void CheckedChanged()

{

section = rbZero.Checked ? rbZero.Text : rbOne.Text;

}

Otherwise double click on a radio button in design mode.


---

Original Source: https://www.mindstick.com/forum/1964/working-with-radio-buttons

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
