C#电梯系统,暂停

发布于 2025-01-02 22:02:17 字数 1052 浏览 0 评论 0原文

用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);
        }
    }
}

Making a simple elevator system in c#, got it working for when yuo select a floor by typing in a floor number, the lift then goes to this floor.

The next stage is to select a floor using a button. The problem is when the lift gets to the floor that the user requested, the program does not accept the input from a button..

This is the code i have to call the lift movement and switch the timer on:

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

This is the code within the timer:

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2025-01-09 22:02:18

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

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 秒期间,用户可以选择另一个楼层。

As for the prototype, you may want to do something like this to allow the user to select another floor:

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);

and use another timer to enable the first timer in 5 seconds:

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

During this 5-second period, user can choose another floor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文