私は窓の形に関する質問があります。私は下のコードと写真に見られるこのカウントダウンを作った。私はそれにも問題があります。スタートとストップで最初のカウントダウンを開始すると、時間、分、秒で実行するように実行されますが、2番目を実行するとすぐに最初のカウントダウンが2番目の値にリセットされます。そして、それは両方の秒をスキップします。したがって、最初の2番目の53から51に、もう1つの52から50に行く..いずれかの人がこの問題を解決し、それらを独立させる方法を知っていますか彼らは明らかに今お互いの?複数のタイマーがWindowsフォームで動作しないようですか?
ありがとうございます!
namespace newtime
{
public partial class Form1 : Form
{
private int h;
private int m;
private int s;
public Form1()
{
InitializeComponent();
}
private void btnStart1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox1.Text = "0";
}
if (textBox2.Text == "")
{
textBox2.Text = "0";
}
if (textBox3.Text == "")
{
textBox3.Text = "0";
}
h = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
s = Convert.ToInt32(textBox3.Text);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
s = s - 1;
if (s == -1)
{
m = m - 1;
s = 59;
}
if (m == -1)
{
h = h - 1;
m = 59;
}
if (h == 0 && m == 0 && s == 0)
{
timer1.Stop();
MessageBox.Show("Times up!", "Timer");
}
string hh = Convert.ToString(h);
string mm = Convert.ToString(m);
string ss = Convert.ToString(s);
textBox1.Text = hh;
textBox2.Text = mm;
textBox3.Text = ss;
}
private void btnStop1_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void btnStart2_Click(object sender, EventArgs e)
{
if (textBox4.Text == "")
{
textBox4.Text = "0";
}
if (textBox5.Text == "")
{
textBox5.Text = "0";
}
if (textBox6.Text == "")
{
textBox6.Text = "0";
}
h = Convert.ToInt32(textBox4.Text);
m = Convert.ToInt32(textBox5.Text);
s = Convert.ToInt32(textBox6.Text);
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
s = s - 1;
if (s == -1)
{
m = m - 1;
s = 59;
}
if (m == -1)
{
h = h - 1;
m = 59;
}
if (h == 0 && m == 0 && s == 0)
{
timer2.Stop();
MessageBox.Show("Times up!", "Timer");
}
string hh = Convert.ToString(h);
string mm = Convert.ToString(m);
string ss = Convert.ToString(s);
textBox4.Text = hh;
textBox5.Text = mm;
textBox6.Text = ss;
}
private void btnStop2_Click(object sender, EventArgs e)
{
timer2.Stop();
}
}
}
あなたは2つのタイマーを持っていますが、あなたは常時上書きする 'h'、' m'、 's'変数の1セットしか持っていません。 – Adrian
これはデバッガを使用し、その部分は少なくとも.. – MethodMan
あなたはそのようなコードを繰り返すのを見るたびに、あなたはすぐに「私はおそらくクラスが必要です」と考えるべきです。 UserControlはここでうまくいくでしょう。 –