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 に答える