Reputation: 1138
I have a program which executes a code in 2 different ways based on the radio button selected(user's choice). My problem is am not able to get what user has selected, meaning the code where it is suppose to execute in different ways based on the radio button selection , that code 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 = rbZero.Text; //if true assigns it's text to a variable.
}
else if (rbOne.Checked == true)
{
section = rbOne.Text;
}
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(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.
Upvotes: 1
Views: 1554
Reputation: 43320
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.
Upvotes: 4