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!