-1

Problem

Hi everyone, I am developing a piano in C#. I have succeeded in creating the piano and it plays fine. However, I want to make use of two timers:

One timer to keep check of how much time the left mouse button was kept down (in Form.cs) and another timer to play the music according to how much time the left mouse button was kept down (this timer is found in MusicNote.cs).

The first timer works as it should and it increases the duration with every tick. The problem is that when I try to pass the global variable duration from Form1 to MusicNote.PlayMusic, the duration value becomes 0 and not the value which was displayed correctly in textBox2.Text.

The same problem occurs when trying to pass bNoteShape from Form1 to MusicNote.

Edit

Thank you all. I have solved the problem.

4

1 回答 1

5

我早些时候发布了这个答案,它收到了 2 票,但后来我改变了主意,删除了它,做了更多的故障排除,所以现在我重新发布它。

首先,duration不是全局变量;它是您班级的成员。

现在,我真的不知道为什么duration传递给PlayMusic(). 我仔细研究了一下,似乎没有理由发生这种事情。我认为panel1_MouseDown()将其设置为零,然后立即panel1_Click()将其传递给PlayMusic(),但这是不正确的:Click()与 一起发生MouseUp(),因此duration当时不应该为零。

但这没关系,因为你的方法完全错误,所以无论如何你都必须改变它,问题可能会在这个过程中自行解决。

您将永远无法PlayMusic()使用音调和持续时间进行调用,因为您需要PlayMusic()立即调用MouseDown(),但那时您还不知道持续时间将是多少。

此外,使用计时器来计算持续时间是完全没有必要的,而且本质上是不准确的;如果您真的需要知道持续时间,只需记录当前时间MouseDown()并从当前时间减去它MouseUp()。但是您也不需要这样做。您需要做的只是停止播放声音MouseUp()。(如果您愿意,您只需这样做以便以后能够重放声音。)

另外,我建议您在每次收到 OnClick 事件时认真重新考虑向面板添加新的 MouseDown 和 MouseUp 事件处理程序的正确性。

另外,我建议您使用有意义的变量名,尤其是当您向他人展示您的代码时,请他们找出问题所在。顾名思义,您的 panel1_OnClick 处理程序不会处理 panel1 的单击事件,而是处理所有音乐键按钮的单击事件。

于 2011-12-26T10:49:59.197 回答