2016-03-24 23 views
1

ユーザー入力に基づいてタイマーカウントを下げたいと思います。タイマーカウントダウンのユーザー入力

これは私のフォームは次のようになります。

this is my timercountdown

と、これは私のコードです:

private int counter=80; 
DateTime dt = new DateTime(); 
private void button1_Click(object sender, EventArgs e) 
{ 
    progressBar1.Maximum = counter * 1000; 
    progressBar1.Step = 1000; 
    timer1 = new System.Windows.Forms.Timer(); 
    timer1.Tick += new EventHandler(timer1_Tick); 
    timer1.Interval = 1000; 
    timer1.Start(); 
    textBox1.Text = counter.ToString(); 
} 


private void timer1_Tick(object sender, EventArgs e) 
{ 
    counter--; 
    progressBar1.PerformStep(); 
    if (counter == 0) 
    { 
     timer1.Stop(); 
    } 
    textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss"); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    timer1.Enabled = false; 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    timer1.Stop(); 
    textBox1.Clear(); 
    progressBar1.Value = 0; 
} 

ユーザーがテキストボックスに新しい値を入力した場合どのようにタイマーを更新することができます?

答えて

1

TextBox1にOnTextChangedイベントハンドラをアタッチします。 TextInputコントロールが

protect void textinput1_OnTextChange(object sender, EventArg e) { 
    button2_Click(sender, e); 

} 

を変更したり、タイマーが停止したら、タイマーが起動し、それを再有効化するときに、ユーザーの入力を無効にすることができたときにタイマーを停止します。

0

開始ボタンをクリックするだけで、テキストボックス対応の値をfalseに設定するだけです。また、タイマーの停止時に再度有効にするか、キャンセルボタンをオンにします。コードは次のとおりです。 textBox1.Enabled = false;

textBox1.Enabled = true;