[英]Timer on Mouse Down in c#
所以我想知道用戶按下按鈕多長時間。 我正在使用button1_MouseDown方法,如下所示。 但是,count變量保持為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++;
}
您的問題是由於使用后遞增的分配。
count = count++;
事件的順序是在賦值之前評估包括副作用在內的右側-因此存儲了count的當前值(= 0)然后增加了count的數量,現在分配了存儲的值-原始值零被寫入返回增加的值。
您只需要使用count ++即可;
private void timer1_Tick (object sender, EventArgs e)
{
count++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.