【发布时间】:2013-12-14 08:28:43
【问题描述】:
我需要使用 Ticks 存储钢琴持续时间,然后根据该持续时间显示音符(音乐播放器会知道)。
我使用的是 100 的间隔,但对于某些测试,我使用了 1000。
问题是这样的。当我调用该方法时(我正在使用 1000 毫秒间隔一个),计时器开始.. 如果我没有设法获得 1000 毫秒,它会显示 Duration 0: 但如果我这样做了例如 2 秒,它显示 3 秒,如果我尝试再按一秒(不同的键)它会显示 4 秒而不是 1。
就像它不断重复一样。 100 间隔 1 也是如此。它发疯了。有时 40 有时 23 等等。知道如何修复(重置计时器)
注意,我使用 System.Windows.Forms.Timer 作为库
调用下面的方法的方法的一部分
for (int i = 0; i < 15; i++)
{
WhiteKey wk = new WhiteKey(wKeys[i], wPos[i]-35,0); //create a new white Key with [i] Pitch, at that x position and at y =0 position
wk.MouseDown += onRightClick; //holds the Duration on Right Click
wk.MouseUp += onMouseUp;
wk.Click += new EventHandler(KeyClick); //Go to KeyClick Method whenever a key is pressed
this.panel1.Controls.Add(wk); //Give it control (to play and edit)
}
控制时间的方法
private void onRightClick(object sender, MouseEventArgs e)
{
wk = sender as WhiteKey;
duration = 0;
t1.Enabled = true;
t1.Tick += timeTick;
t1.Interval = 100;
}
private void timeTick(object sender, EventArgs e)
{
duration++;
}
private void onMouseUp (object sender, MouseEventArgs e)
{
t1.Enabled = false;
String time = "Key: " + pitch + "\nDuration: " +duration ; //Test purposes to see if timer works
MessageBox.Show(time);
}
【问题讨论】: