2016-09-26 18 views
-1

ボタンをクリックすると、ボタンの色が5秒後に黒に変わりますが、私はそれを動作させることができません。私は既にタイマーの間隔を5000に設定し、プロパティではEnabledをtrueに設定しています。Visual studio C#タイマーが動作しない

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication4 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      timer1.Enabled = true; 
      timer1.Start(); 

      button1.BackColor = Color.Black; 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 

     } 
    } 
} 
+0

timer_tickイベント内の色の変化のコードを書くのか?あなたはwindows.Forms.Timerを使っているはずです。デザイン時に追加したようですが、遅延させたいアクションがTickイベントにあることを確認してください。 –

答えて

2

private void button1_Click(object sender, EventArgs e) 
     {    
      Timer MyTimer = new Timer(); 
      MyTimer.Interval = 4000; 
      MyTimer.Tick += new EventHandler(MyTimer_Tick); 
      MyTimer.Start(); 

     } 

     private void MyTimer_Tick(object sender, EventArgs e) 
     { 
      button1.BackColor = Color.Black; 

     } 
+0

ありがとう!!あなたのソリューションは動作しています! – bubibu

+0

@bubibuあなたはそれを回答としてマークできますか – Sajeetharan

2

あなたは色を黒に変更したい、とそのように滞在する場合、5 seconds後、あなたはtimer1_Tickイベントハンドラでbutton1.BackColorの割り当てを配置する必要があります。また、タイマーを止めるのを忘れないでください。

private void timer1_Tick(object sender, EventArgs e) 
{ 
     button1.BackColor = Color.Black; 
     timer1.stop(); 
} 
1

これを試してみてください:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 

     button1.BackColor = Color.Black; 
     timer1.Stop(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 
     timer1.Interval = 5000; 
     timer1.Start(); 


    } 
} 

あなたは、タイマーのTickイベントに、ボタンの黒い背景色のトリガーを配置する必要があります。ベストsoultionは次のようになり

+0

Tickイベントに入れた 'timer1.Stop();'は素晴らしいでしょう。 – Eric

+0

winformsタイマーでは、それを有効にして、両方を開始するのと同じことをします。 –

+0

私は同意します。これは、タイマーが1回だけチェックされるようにするためです:)私の答えを更新します。 – Jayson

0

は、あなたが何タイマーを使用している

private void timer1_Tick(object sender, EventArgs e) 
    { 
     button1.BackColor = Color.Black; 
     timer1.Enabled = false; 
    } 
関連する問題