When a floor button is pressed by a person inside the lift, it calls the method ChangeFloor to inc/decrease the chosen lift's level. Two parameters are passed to the ChangeFloor function; the lift's label(floor indicator) and the button pressed to gain the floor they're on through btn.Name. Tried implementing a timer but can't seem to get it to start? 'timer1' created through the toolbar.
private void FloorButtonClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (Convert.ToInt16(btn.Name) > 40)
if (Convert.ToInt16(btn.Name) > 80)
if (Convert.ToInt16(btn.Name) > 120)
ChangeFloor(lblLiftFloor4, btn);
else
ChangeFloor(lblLiftFloor3, btn);
else
ChangeFloor(lblLiftFloor2, btn);
else
ChangeFloor(lblLiftFloor1, btn);
}
private void ChangeFloor(Label lift, Button btn)
{
int level = Convert.ToInt16(lift.Text);
int desired = Convert.ToInt16(btn.Tag);
while( level!=desired )
{
if (level > desired)
{
timer1.Start();
lift.Text = (--level).ToString();
}
else
{
timer1.Start();
lift.Text = (++level).ToString();
}
}
}
I am not sure what you are trying to do exactly... if you just want to simulate the changing of floors and introduce a delay, you could just throw in a Sleep
statement which will cause your app to pause for a given amount of time.
private void ChangeFloor(Label lift, Button btn)
{
int level = Convert.ToInt16(lift.Text);
int desired = Convert.ToInt16(btn.Tag);
while( level!=desired )
{
Thread.Sleep(3000); // <--- Pause for 3 seconds...
if (level > desired)
{
lift.Text = (--level).ToString();
}
else
{
lift.Text = (++level).ToString();
}
}
}
EDIT: Read up of threading because if you want a bunch of independent elevators. Threads are what you could implement to achieve the behavior you're looking for:
http://msdn.microsoft.com/en-us/library/System.Threading(v=vs.110).aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.