-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.

Clayton
  • 123
  • 1
  • 2
  • 10
  • 1
    Why are you using timers to calculate elapsed time? – Oded Dec 26 '11 at 10:34
  • 1
    Way too much code. You should trim it down to the parts that are related to the problem. – H H Dec 26 '11 at 10:34
  • This is very weird indeed. Did you try putting a breakpoint at mn.PlayMusic(pit, duration); and debugging the thing? Try to find at which point dur becomes 0 – Svarog Dec 26 '11 at 10:35
  • 2
    It seems very unlikely that parameter passing is *really* going wrong here. I suspect there's something else amiss, but it's hard to tell when we've been given so much code, but still not a complete program to try for ourselves. – Jon Skeet Dec 26 '11 at 10:35

1 Answers1

5

I posted this answer earlier, and it received 2 upvotes, but then I changed my mind, deleted it, did some more troubleshooting, so now I am reposting it.

First of all, duration is not a global variable; it is a member of your class.

Now, I really do not know why duration is zero when it is passed to PlayMusic(). I looked into it hard, and there appears to be no reason for such a thing happening. I thought that panel1_MouseDown() sets it to zero, and immediately afterwards panel1_Click() passes it to PlayMusic(), but that is not correct: Click() occurs together with MouseUp(), so duration should not have been zero at that time.

But it does not matter, because your approach is completely wrong, so you are going to have to change it anyway, and the problem will probably fix itself in the process.

You will never be able to invoke PlayMusic() with pitch and duration, because you need to invoke PlayMusic() immediately upon MouseDown(), but at that time you do not know what the duration is going to be yet.

Also, using a timer to figure the duration is entirely unnecessary, and inherently inaccurate; if you really need to know the duration, just record the current time on MouseDown() and subtract it from the current time on MouseUp(). But you do not need to do that, either. All you need to do is just stop playing the sound on MouseUp(). (You will only have to do it in order to be able to replay the sound later, if you so wish.)

Also, I would advise you to seriously reconsider the correctness of adding new MouseDown and MouseUp event handlers to the panel every time you receive an OnClick event.

Also, I would advise you to use meaningful variable names, especially when you are showing your code to others, asking them to figure out what is wrong with it. Your panel1_OnClick handler does not handle click events for panel1, as it name would suggest, but instead it handles click events for all your music key buttons.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142