1

用c#制作一个简单的电梯系统,当你通过输入楼层号选择一个楼层时,电梯就会到这个楼层。

下一阶段是使用按钮选择楼层。问题是当电梯到达用户要求的楼层时,程序不接受按钮的输入。

这是我必须调用电梯运动并打开计时器的代码:

private void liftOneMove()
{
    if ((liftOne.Direction == 1) && (lift1.Value <= 40)) // while lifts going up and floor less/equal to 40
    {
        timer1.Enabled = true;
    }
}

这是计时器内的代码:

private void timer1_Tick(object sender, EventArgs e)
{
    lift1.Value++;
    liftOne.CurrentFloor = lift1.Value;
    lblLift1Flr.Text = "" + lift1.Value;
    if (liftOne.FloorRequests.Exists(element => element == lift1.Value))
    {
        RbLift1.Checked = true;
        lblLift1Current.Text = "Doors opening";
        //Need to pause the timer here to allow user to select a floor
        liftOne.FloorRequests.Remove(lift1.Value);

        if(liftOne.FloorRequests.Count == 0)
        {
            liftOne.Direction = 2;
            timer1.Enabled = false;
            MessageBox.Show("Floor Requests  " + liftOne.FloorRequests.Count);
        }
    }
}
4

1 回答 1

0

至于原型,您可能想要做这样的事情来允许用户选择另一个楼层:

RbLift1.Checked = true;
lblLift1Current.Text = "Doors opening";

timer1.Enabled = false; // stop the first timer after reaching the floor
timer2.Enabled = true; // start a second timer, which will restart the first timer in 5 seconds

liftOne.FloorRequests.Remove(lift1.Value);

并使用另一个计时器在 5 秒内启用第一个计时器:

private void timer2_Tick(object sender, EventArgs e)
{
    timer1.Enabled = true; // restart the first timer
    timer2.Enabled = false; // stop the second timer
}

在这 5 秒的时间里,用户可以选择另一个楼层。

于 2012-02-07T16:44:15.700 回答