0

So I am trying to add several images (Music Notes) in the form of PictureBox to a form, but despite all my efforts, only the first music note is being displayed, and all preceding music notes are not.

Key_MouseUp Event (occurs when the mouse is lifted up after a piano key is pressed):

private void Key_MouseUp(object sender, MouseEventArgs e)
    {
        foreach (MusKey mk in panel1.Controls)
        {
            if (sender == mk) //true for the specific key pressed on the Music Keyboard
            {
                if (e.Button == MouseButtons.Left)
                {
                    timer1.Enabled = false;
                    sp.Stop();
                    string bNoteShape = null;

                    // ticks -> milliseconds
                    if (count > 15)
                    {
                        bNoteShape = "SemiBreve";
                        duration = 2000;
                    }
                    else if (count > 10 && count <= 15)
                    {
                        bNoteShape = "DotMinim";
                        duration = 1000;
                    }
                    else if (count > 5 && count <= 10)
                    {
                        bNoteShape = "Minim";
                        duration = 500;
                    }
                    else if (count > 2 && count <= 5)
                    {
                        bNoteShape = "Crotchet";
                        duration = 250;
                    }
                    else if (count >= 1.25 && count <= 2)
                    {
                        bNoteShape = "Quaver";
                        duration = 125;
                    }
                    else
                    {
                        bNoteShape = "Semi-Quaver";
                        duration = 63; 
                    }

                    MusicNote mn = new MusicNote(mk.getMusicNote(), duration, bNoteShape, mk.getNote());
                    Notes.Add(mn);

                    panel2.Controls.Add(mn); //adding MusicNote component to MusicStaff (panel2) collection
                }
            }
        }
    }

Music Note Class and Constructor:

class MusicNote : PictureBox
{
    public int notepitch;
    public int noteduration;
    public String noteshape;
    public String note;
    enum Accid { sharp, flat, sole };

    String NoteImage_path = "C:\\Users\\Luke Xuereb\\Documents\\University\\Year 2\\Semester 1\\Object Oriented Programming\\Piano Assignment\\Notes-Images\\";
    static int xLoc = 50;
    static int yLoc = 30;


    bool dragging = false;
    System.Timers.Timer timer1 = new System.Timers.Timer();

    public MusicNote(int iNotepitch, int iDuration, String iBnoteShape, String iNote)
    {
        notepitch = iNotepitch;
        noteduration = iDuration;
        noteshape = iBnoteShape;
        note = iNote;

        ImageLocation = NoteImage_path + noteshape + ".bmp";
        BackColor = Color.Beige;

        Location = new Point(xLoc, yLoc);
        xLoc += 15;
    }

I have been several days stuck on this problem, and am quite convinced that the logic of the code is correct (which is in fact why the first Music Note is always displayed).

Is there a problem when it comes to displaying several pictureboxes? A new picturebox is created everytime a new music note is created, since class MusicNote inherits from class PictureBox.

Alternatively, is there a better option for my current problem, instead of using PictureBox?

Any help would be massively appreciated. Thank you!

Luke Xuereb
  • 73
  • 12
  • They are probaly all sitting at the same spot. Change the location before or after adding them! _ Consider drawing them all in the Paint event of one PictureBox. It really depends on what you want to do: How many and how often will they change etc.. – TaW Jan 04 '19 at 12:14
  • @TaW they are not because as can be seen in the MusicNote constructor, xLoc is continuously being incremented by 15, therefore the point is always different. – Luke Xuereb Jan 04 '19 at 12:15
  • Right; I missed that and also the `static`. Hm. It seems to work here. Do use the Debugger to see the Locations etc..! (Put it on the `panel2.Controls.Add(mn);` and look into the controls collection to see the values for all previos boxes!) – TaW Jan 04 '19 at 12:26
  • @TaW put what on the panel2.Controls.Add(mn); ? – Luke Xuereb Jan 04 '19 at 12:30
  • A debugger breakpoint. – TaW Jan 04 '19 at 12:31
  • @TaW I gave it a go, but of course no music note is shown because no music note has been added to the panel yet. – Luke Xuereb Jan 04 '19 at 12:36
  • Keep running/stepping til the 2nd or 3rd time! – TaW Jan 04 '19 at 12:39
  • 1
    @TaW I used the debugging and it was very helpful. Turns out my mistake was that the images were being constructed very close to each other, and the transparent background of the previous image was overlapping the preceding one. Therefore apart from the first note, it was just a series of transparent backgrounds overlapping each other. Thank you for your kind help! – Luke Xuereb Jan 04 '19 at 13:16
  • No problem. Note that the [debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx) is your very best friend in the world of coding; always try to use it before even coming here..! – TaW Jan 04 '19 at 13:55
  • @TaW Noted!! :) – Luke Xuereb Jan 04 '19 at 13:55

0 Answers0