0

I am creating a c# piano which when clicking on a music key, an image of the music note is outputted on the form, however no image is being outputted. I am sure that the image path for the bitmap images is correct so I am not sure whether I have done something wrong in the code or I need to add some events to the form itself.Can somebody help me with this please ?

This is the Form class:

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

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

        SoundPlayer sp = new SoundPlayer();
        int count;
        private Control mn;
        int xLoc = 50;
        int yLoc = 30;
        List<MusicNote> notes = new List<MusicNote>();

        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 < 7; k++)
            {
                int iNote = whitePitch[k];
                int xpos = k * 40;
                mk = new MusKey(iNote, xpos + 225, 300);
                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 < 5; k++)
            {
                int iNote = blackPitch[k];
                int xpos = xPos[k] * 2;
                bmk = new BlackMusKey(iNote, xpos + 225, 300);
                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)
        {
            foreach(MusKey mk in this.panel1.Controls.OfType<MusKey>())
            {
                if(sender == mk)
                {
                    if(e.Button == MouseButtons.Left)
                    {
                        timer1.Enabled = true;
                        count = 0;
                        timer1.Start();
                        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 timer1_Tick(object sender, EventArgs e)
        {
            count ++;
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            foreach(MusKey mk in this.panel1.Controls.OfType<MusKey>())
            {
                if (sender == mk)
                {
                    if (e.Button == MouseButtons.Left)
                        timer1.Enabled = false;

                    textBox1.Text = count.ToString();
                    sp.Stop();
                    string bNoteShape = null;
                    int duration = 0;

                    //Insert note lengths 

                    if (count >= 16)
                    {
                        bNoteShape = "SemiBreve";
                        duration = (16 + 20) / 2;
                    }
                    else if ((count >= 11) && (count <= 15))
                    {
                        bNoteShape = "DotMin";
                        duration = (11 + 15) / 2; //average
                    }
                    else if ((count >= 6) && (count <= 10))
                    {
                        bNoteShape = "Minim";
                        duration = (6 + 10) / 2; //average
                    }
                    else if ((count >= 3) && (count <= 5))
                    {
                        bNoteShape = "Crotchet";
                        duration = (3 + 5) / 2; //average
                    }
                    else if ((count == 2))
                    {
                        bNoteShape = "Quaver";
                        duration = 2; //average
                    }
                    else if ((count >= 0) || (count <= 1))
                    {
                        bNoteShape = "SemiQuaver";
                        duration = 1;
                    }

                    //copied from handout

                    MusicNote mn = new MusicNote(mk.musicNote, duration, bNoteShape, xLoc);
                    notes.Add(mn);
                    mn.Location = new Point(xLoc, yLoc);
                    this.panel1.Controls.Add(this.mn);

                    xLoc = xLoc + 40;


                }
            }
        }

        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 < 5; k++)
            {
                int ypos = (k * 15) + 20;
                graphicsObj.DrawLine(myPen, 20, ypos, 550, ypos);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (MusicNote mn in notes)
            {
                sp.SoundLocation = @"C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + mn.pitch + ".wav"; //change this to the location of your "mapped" folder
                sp.Play();
                Thread.Sleep(mn.duration * 150);

            }
        }
    }
}

This is the Music Note class:

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
{
    public class MusicNote : PictureBox
    {


        Boolean isDragging = false;
        public int pitch = 0;
        public string noteshape = "";
        public int duration = 0;
        internal Point Location;
        public int xLocation = 0;


        public MusicNote(int notePitch, int duration, string bNoteShape, int xLoc) : base()
        {

            this.noteshape = bNoteShape;
            this.duration = duration;
            this.pitch = notePitch;
            Location = new Point(xLoc, pitch);
            this.Size = new Size(30, 30);



            Bitmap bmp = new Bitmap(@"C:/Users/Kim/Desktop/Notes-Images/" + noteshape + ".bmp");
            this.Image = bmp;


            this.MouseDown += new MouseEventHandler(StartDrag);
            this.MouseUp += new MouseEventHandler(StopDrag);
            this.MouseMove += new MouseEventHandler(NoteDrag);

        }

        private void StartDrag(object sender, MouseEventArgs e) {

            if(e.Button == MouseButtons.Left) {
                isDragging = true;
                pitch = e.Y;
                this.Location = new Point(this.Location.X, pitch);
            }
        }

        private void StopDrag(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
                pitch = e.Y;
            }
        }


        private void NoteDrag(object sender, MouseEventArgs e)
        {

            if (isDragging)
            {
                this.Top = this.Top + (e.Y - this.pitch);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

    }
 }
  • Just a rough guess, but you are creating a new graphics object in your paint event, but never actually drawing it. For instance, use e.Graphics.DrawImage... – tval Dec 28 '17 at 19:19
  • Sorry for asking but which part of the code should I insert it please ? – Mikhail Galea Dec 28 '17 at 19:30
  • Your form class fails to use the `PaintEventArgs.Graphics` object; creating our own in that event handler isn't correct. And your `MusicNote` class doesn't even attempt to draw anything. In any case, no one here has your bitmaps, and there's lots of code above that isn't relevant to your question. Please improve the question, making sure it includes a good [mcve] that reliably reproduces your problem. – Peter Duniho Dec 29 '17 at 00:01
  • Well put, Peter, you explained it much better than I did. It seems that the entirety of the problems lies within the panel1_Paint in your main form. You need to actually render the graphics object in that class. – tval Dec 29 '17 at 20:47

0 Answers0