I am stuck on getting my program working, basically i am building a elevator simulation with multiple elevators which move simultaneously so i decided to use threads to do so. Thats when the problem manifested.
Basically i have a set of buttons indicating which floor the person would like to vist then the elevator travels there. However in the code below i have a problem i am trying to use a switch statement to move the elevator depending on the button press. I cannot however find away of detecting the button press to pass into my switch statement which will then execute the correct code.
public Form1()
{
InitializeComponent();
button12.Click += new System.EventHandler(clickedButton);
button11.Click += new System.EventHandler(clickedButton);
button10.Click += new System.EventHandler(clickedButton);
button9.Click += new System.EventHandler(clickedButton);
button8.Click += new System.EventHandler(clickedButton);
button7.Click += new System.EventHandler(clickedButton);
}
Thread e1;
public void thread()
{
int loc = elevator1.Location.Y;
switch (sender)
{
case (button12_Click):
if (loc < 288)
{
while (loc != 308)
{
elevator1.Location = new Point(3, loc);
loc++;
Thread.Sleep(10);
}
}
else
{
if (loc > 389)
{
while (loc != 308)
{
elevator1.Location = new Point(3, loc);
loc--;
Thread.Sleep(10);
}
}
}
break;
case (button11_Click):
if (loc < 246)
{
while (loc != 246)
{
elevator1.Location = new Point(3, loc);
loc++;
Thread.Sleep(10);
}
}
else
{
if (loc > 246)
{
while (loc != 246)
{
elevator1.Location = new Point(3, loc);
loc--;
Thread.Sleep(10);
}
}
}
break;
case (button10_Click):
if (loc < 185)
{
while (loc != 185)
{
elevator1.Location = new Point(3, loc);
loc++;
Thread.Sleep(10);
}
}
else
{
if (loc > 185)
{
while (loc != 185)
{
elevator1.Location = new Point(3, loc);
loc--;
Thread.Sleep(10);
}
}
}
break;
case (button9_Click):
if (loc < 125)
{
while (loc != 125)
{
elevator1.Location = new Point(3, loc);
loc++;
Thread.Sleep(10);
}
}
else
{
if (loc > 125)
{
while (loc != 125)
{
elevator1.Location = new Point(3, loc);
loc--;
Thread.Sleep(10);
}
}
}
break;
case (button8_Click):
if (loc < 63)
{
while (loc != 63)
{
elevator1.Location = new Point(3, loc);
loc++;
Thread.Sleep(10);
}
}
else
{
if (loc > 63)
{
while (loc != 63)
{
elevator1.Location = new Point(3, loc);
loc--;
Thread.Sleep(10);
}
}
}
break;
case (button7_Click):
if (loc < 2)
{
while (loc != 2)
{
elevator1.Location = new Point(3, loc);
loc++;
Thread.Sleep(10);
}
}
else
{
if (loc > 2)
{
while (loc != 2)
{
elevator1.Location = new Point(3, loc);
loc--;
Thread.Sleep(10);
}
}
}
break;
}
}
public void clickedButton(object sender, EventArgs e)
{
e1 = new Thread(thread);
e1.Start();
}
The code worked before without using a switch statement but when i try to us it it fails.
How would i solve the the problem of passing which button was pressed into the switch statement?.