在C#中向表单添加多个图像

时间:2019-01-04 12:08:31

标签: c# forms image winforms picturebox

因此,我试图将PictureBox形式的几张图像(音乐笔记)添加到表单中,但是尽管我付出了所有努力,但仅显示了第一个音符,而没有显示所有先前的音符。

Key_MouseUp事件(在按下钢琴键后抬起鼠标时发生):

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

音符类和构造函数:

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;
    }

我已经在这个问题上待了几天,并且非常确信代码的逻辑是正确的(实际上这就是为什么总是显示第一个音符的原因)。

在显示多个图片框时是否存在问题?每次创建新的音符都会创建一个新的picturebox,因为MusicNote类是从PictureBox继承的。

或者,对于我当前的问题,有没有更好的选择,而不是使用PictureBox?

任何帮助将不胜感激。谢谢!

0 个答案:

没有答案