Creating Piano with C# part 2 : <p>In relation to my previous question I posted, I am creating a c# piano which will display a keyboard which when clicking on a music key will display a music note shape and output a music sound. However, I encountered another problem. When I click on a music key it outputs the required image, but when I click again on another again with a different duration the same image stays and does not change. Can anyone help me with this please ?</p>

<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace NewPiano
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SoundPlayer sp = new SoundPlayer();
        long count;
        private Control mn;
        Stopwatch watch = new Stopwatch();

        private void Form1_Load(object sender, System.EventArgs e)
        {

            MusKey mk;
            BlackMusKey bmk;

            int[] whitePitch = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24 };
            int[] blackPitch = new int[] { 2, 4, 7, 9, 11, 14, 16, 19, 21, 23 };
            int[] xPos = new int[] { 10, 30, 70, 90, 110, 150, 170, 210, 230, 250 };

            for (int k = 0; k &lt; 7; k++)
            {
                int iNote = whitePitch[k];
                int xpos = k * 40;
                mk = new MusKey(iNote, xpos + 150, 120);
                mk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
                mk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
                this.panel1.Controls.Add(mk);
            }

            int xOffs = 20;

            for (int k = 0; k &lt; 5; k++)
            {
                int iNote = blackPitch[k];
                int xpos = xPos[k] * 2;
                bmk = new BlackMusKey(iNote, xpos + 150, 120);
                bmk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
                bmk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
                this.panel1.Controls.Add(bmk);
                this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront();
            }

        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            watch.Reset();

            foreach (MusKey mk in panel1.Controls.OfType&lt;MusKey&gt;())
            {
                if (sender == mk)
                {
                    if (e.Button == MouseButtons.Left)
                    {

                        watch.Start();
                        count = 0;
                        sp.SoundLocation = @"C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + mk.musicNote + ".wav"; //change this to the location of your "mapped" folder

                        sp.Load();
                        sp.Play();
                    }
                }
            }

        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            foreach (MusKey mk in panel1.Controls.OfType&lt;MusKey&gt;())
            {
                if (sender == mk)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        watch.Stop();
                        count = watch.ElapsedMilliseconds;
                    }

                    sp.Stop();

                    int duration = 0;
                    string bNoteShape = null;
                    int pitch = 0;

                    if (count &gt;=2024)
                    {
                        bNoteShape = "SemiBreve";

                        duration = (16 + 20) / 2;

                        pitch = 1;
                    }

                    else if ((count &gt;= 1024) &amp;&amp; (count &lt; 2024))
                    {
                        bNoteShape = "DotMinim";

                        duration = (11 + 15) / 2; //average

                        pitch = 2;

                    }
                    else if ((count &gt;= 768) &amp;&amp; (count &lt; 1024))
                    {
                        bNoteShape = "minim";

                        duration = (6 + 10) / 2; //average

                        pitch = 3;
                    }
                    else if ((count &gt;= 512) &amp;&amp; (count &lt; 768))
                    {

                        bNoteShape = "Crotchet";
                        duration = (3 + 5) / 2; //average
                        pitch = 4;
                    }
                    else if ((count &gt;= 256 &amp;&amp; count &lt; 512))
                    {
                        bNoteShape = "Quaver";

                        duration = 2; //average
                        pitch = 5;
                    }
                    else if ((count == 0 || count &lt; 256))
                    {
                        bNoteShape = "SemiQuaver";
                        duration = 1;
                        pitch = 6;
                    }


                    MusicNote mn = new MusicNote(count, pitch, bNoteShape, duration);
                    panel1.Controls.Add(mn.drawNote());
                    watch.Restart();



                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics graphicsObj;

            graphicsObj = this.panel1.CreateGraphics();

            Pen myPen = new Pen(System.Drawing.Color.Black, 1);

            for (int k = 0; k &lt; 5; k++)
            {
                int ypos = (k * 15) + 20;
                graphicsObj.DrawLine(myPen, 20, ypos, 550, ypos);
            }
        }

}
}
</code></pre>

<p>Music Note Class:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewPiano
{
    class MusicNote
    {

        public long noteID;
        public int pitch = 0;
        public string shape = "";
        public int duration = 0;

        public MusicNote() { }

        public MusicNote(long count, int mpitch, string shape, int duration)
        {
            this.noteID = count;
            this.shape = shape;
            this.duration = duration;
            this.pitch = mpitch;
        }

        public PictureBox drawNote()
        {
            PictureBox noteDrawing = new PictureBox();
            noteDrawing.Size = new System.Drawing.Size(40, 40);
            noteDrawing.Location = new System.Drawing.Point(10, 10);

            noteDrawing.ImageLocation = @"C:/Users/Kim/Desktop/Notes-Images/" + shape + ".bmp";

            return noteDrawing;
        }

        /*public void PlayAllNotesOnClick()
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = "C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + this.pitch + ".wav";

            player.Play();
            System.Threading.Thread.Sleep(duration + 100);
            player.Stop();
        }
        */

        public void onDrag(int pitch)
        {
            if (pitch &gt; 150 &amp;&amp; pitch &lt; 155)
            {
                this.pitch = 1;
            }
            if (pitch &gt; 100 &amp;&amp; pitch &lt; 154)
            {
                this.pitch = 2;
            }
            if (pitch &gt; 75 &amp;&amp; pitch &lt; 99)
            {
                this.pitch = 5;
            }
            if (pitch &gt; 50 &amp;&amp; pitch &lt; 74)
            {
                this.pitch = 6;
            }
            //this.pitch = pitch;
        }

    }
}
</code></pre>
