0

「ship1、ship2」などと呼ばれる4つのボタンがあります。フォームの右側に(同じ速度で同時に開始して)移動し、1つの「ship」をクリックするたびにボタンを移動します。すべての船が止まるはずです。

タイマーを使う必要があることは知っています(スレッドを使用するコードを書いていますが、船を止めるときに問題が発生します)。タイマーの使い方がわかりません。

MDSNでタイマー情報を読み込もうとしましたが、わかりませんでした。だからあなたは私を助けることができますか?

スレッドを使用したコードはこちらです。 使いたくない。タイマーを使う必要があります!(コードなしで投稿できないため、ここに投稿しました

    private bool flag = false;
    Thread thr;
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        flag = false;
        thr = new Thread(Go);
        thr.Start();

    }

    private delegate void moveBd(Button btn);

    void moveButton(Button btn)
    {
        int x = btn.Location.X;
        int y = btn.Location.Y;
        btn.Location = new Point(x + 1, y);
    }

    private void Go()
    {
        while (((ship1.Location.X + ship1.Size.Width) < this.Size.Width)&&(flag==false))
        {
            Invoke(new moveBd(moveButton), ship1);
            Thread.Sleep(10);
        }

        MessageBox.Show("U LOOSE");

    }

    private void button1_Click(object sender, EventArgs e)
    {
        flag = true;
    }
4

1 に答える 1

1

グーグルしましたWindows.Forms.Timerか?

次の方法でタイマーを開始できます。

Timer timer = new Timer();

timer.Interval = 1000; //one second
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();

Elapsed'ボタン'の移動を処理するコードを配置するイベントを処理するには、イベントハンドラーが必要です。

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)  
{
      MoveButton();       
}
于 2012-08-02T15:33:18.890 に答える