c# 中的鼠标按下计时器
Timer on Mouse Down in c#
所以我想知道用户按下按钮的时间。我正在使用 button1_MouseDown 方法,如下所示。但是计数变量保持为 0。
有人可以帮我解决这个问题吗?
提前致谢!
private void button1_MouseDown(object sender, MouseEventArgs e)
{
foreach(MusKey mk in this.Controls)
{
if(sender == mk)
{
if(e.Button == MouseButtons.Left)
{
timer1.Enabled = true;
count = 0;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
sp.SoundLocation = ( ---directory---- + mk.musicNote + ".wav");
sp.Play();
}
}
}
}
private void timer1_Tick (object sender, EventArgs e)
{
count = count++;
}
您的问题是由于赋值使用了 post 增量。
count = count++;
事件的顺序是评估右侧,包括赋值前的副作用 - 所以计数的当前值被存储(=0)计数然后增加并且现在存储的值被分配 - 原始值零的值被写回增量值。
你只需要使用count++;
private void timer1_Tick (object sender, EventArgs e)
{
count++;
}
所以我想知道用户按下按钮的时间。我正在使用 button1_MouseDown 方法,如下所示。但是计数变量保持为 0。
有人可以帮我解决这个问题吗?
提前致谢!
private void button1_MouseDown(object sender, MouseEventArgs e)
{
foreach(MusKey mk in this.Controls)
{
if(sender == mk)
{
if(e.Button == MouseButtons.Left)
{
timer1.Enabled = true;
count = 0;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
sp.SoundLocation = ( ---directory---- + mk.musicNote + ".wav");
sp.Play();
}
}
}
}
private void timer1_Tick (object sender, EventArgs e)
{
count = count++;
}
您的问题是由于赋值使用了 post 增量。
count = count++;
事件的顺序是评估右侧,包括赋值前的副作用 - 所以计数的当前值被存储(=0)计数然后增加并且现在存储的值被分配 - 原始值零的值被写回增量值。
你只需要使用count++;
private void timer1_Tick (object sender, EventArgs e)
{
count++;
}