简体   繁体   中英

picturebox Control is not working C# .NET (Visual Studio)

I'm doing a Piano Keyboard which also has a Music Staff with it so that when you press a Key on the Keyboard, according to the duration a music note (crotchet, semi-breve etc) pops up on the Music Staff.

I have a Form (called Form1) which in it it has a Panel (Panel1) which contains the Keyboard Keys made of Buttons. I also have a Music Staff made of a PictureBox which holds the staff (or stave don't know what it's called) and a Test Text Box which holds the pitches (basically the note played as they are mapped)

The problem is this. I need to make the music notes show on the staff (pictureBox1) so in Form1 which is acting as the main class but whenever I write

mn = new MusicNote (nPos,nPitch, duration,nShape);
pictureBox1.Controls.Add(this.mn);
pictureBox1.Controls[pictureBox1.Controls.Count - 1].BringToFront(); //Bring the notes OVER the Stave

It basically doesn't work BUT when I replace every pictureBox1 with just this OR panel1 (eg)

this.Controls.Add(this.mn); 

it shows the Music Note either on the FORM1 (the grey space) or on the Keyboard itself (see further down to see the Keyboard). The problem is that with this, it doesn't actually adds to the PictureBox but to the Form1/Panel

Do you have any idea how to fix this and make the music Notes actually PART of the PictureBox1? because I also need some methods to work on that pictureBox like when I click one of the Notes they actually play that sound with that duration (in which I still need to figure out how to pass the Controls From Form1.cs to MusicNote.cs)

Part of the coding for MusicNote.cs which has to do with the "adding" of the image is this below:

public class MusicNote : PictureBox
{
    public int pitch;
    public int duration;
    public string noteShape;

    string nShape;

    bool isDragging = false;
    public string rootFolder = @"..\..\\bin\\Debug\\images\\";

    ArrayList al = new ArrayList();
    SoundPlayer sp = new SoundPlayer();
    Timer t1 = new Timer();

    public MusicNote(int x, int mPitch, int mDuration,string nShape)
        : base()
    {
        pitch = mPitch;
        duration = mDuration;
        noteShape = nShape;

        Location = new Point(x, 100);
        this.Size = new Size(35, 40);

        //--------- Get the Image of Note ----------

        Bitmap bmp = new Bitmap(@rootFolder + nShape + ".bmp");
        this.Image = bmp;
        this.BackColor = Color.Transparent;

        //-------Mouse Events-------

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

    }


    etc
    ...
    ...
    ...
    }

Form1.cs Coding (posting all as all are connected from one method to another) The Problem lies in private void onMouseUp (object sender, MouseEventArgs e) which is the last method

using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.Collections;


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



        // Objects of Music Note, Piano Keys, and all variables surronding the Keyboard and Notes
        WhiteKey wk;
        BlackKey bk;
        public int pitch;
        public int duration = 0;
        string nShape = "";

        public const int xOff = 35;
        int count = 0;
        int nPos = 0;
        public string rootFolder = @"..\..\\bin\\Debug\\images\\";

        MusicNote mn;
        MusicStaff ms;
        SoundPlayer sp = new SoundPlayer();
        Stopwatch sw = new Stopwatch();

        //--------------- White and Black Keys Creation both Buttons and Position------------------
        public int[] wKeys = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24, 25 }; //White Keys notes numbers (pitch)
        public int[] bKeys = { 0, 2, 0, 4, 0, 0, 7, 0, 9, 0, 11, 0, 0, 14, 0, 16, 0, 0, 19, 0, 21, 0, 23, 0, 0 }; //Black Keys notes numbers (pitch)
        int[] wPos = { 35, 70, 105, 140, 175, 210, 245, 280, 315, 350, 385, 420, 455, 490, 525 }; // Position of White Keys on the Panel
        int[] bPos = { 0, 57, 0, 92, 0, 0, 162, 0, 197, 0, 232, 0, 0, 302, 0, 337, 0, 0, 407, 0, 442, 0, 477, 0, 1 }; //Position of the Black Keys in the Panel




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

            for (int i = 0; i < 15; i++)
            {                
                WhiteKey wk = new WhiteKey(wKeys[i], wPos[i]-35,0); //create a new white Key with [i] Pitch, at that x position and at y =0 position
                wk.MouseDown += onMouseDown; //Plays the Key and starts Timer
                wk.MouseUp += onMouseUp; // Holds the data like Time and shape and so 
                this.panel1.Controls.Add(wk); //Give it control (to play and edit)
            }




            for (int i = 0; i < 25; i++) //same for the Black Keys but instead we use 25 keys and those denoted at 0 are where the WHITE KEYS should be placed
            {
                if (bKeys[i] != 0)
                {
                    //Same as above but for Black Key which is inherits from WhiteKey
                    bk = new BlackKey(bKeys[i], bPos[i]-35, 0);
                    bk.MouseDown += onMouseDown;
                    bk.MouseUp += onMouseUp;    
                    this.panel1.Controls.Add(bk);
                    this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront(); //Make the Black Keys show OVER the white  
                }

            }
        }


       //Method showing what happens when you do a MouseDown Event
        private void onMouseDown(object sender, MouseEventArgs e)
        {
            wk = sender as WhiteKey; //gets the WhiteKey Controls


            pitch = wk.pitch; //assign pitch


            sp.SoundLocation = @"..\\..\\bin\\Debug\\sound\\mapped\\" + pitch + ".wav"; //find that pressed note
            sp.Play(); //play it

            sw.Reset(); //Reset Stop Watch
            sw.Start(); //Start Time

        }

        private void timeTick(object sender, EventArgs e)
        {
            duration++; 
        }

        private void onMouseUp (object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                sw.Stop(); //Stop time
                duration = (int)sw.ElapsedMilliseconds / 100;

                textBox.Text += (string)(pitch + " , ");  //add the Pitch used to the textbox

                // Will determine the Music Note Image
               if (duration >= 0 && duration < 2)
                {
                    nShape = "Quaver";
                 }
                else if (duration >= 2 && duration < 5)
                {
                    nShape = "Crotchet";
                }
                else if (duration >= 5 && duration < 8)
                {
                    nShape = "minim";
                }
                else if (duration >= 8 && duration < 10)
                {
                    nShape = "DotMin";
                }
                else if (duration >= 10)
                {
                    nShape = "SemiBreve";
                }

                count++; //will help Determine the 'x' coordiante of the Music Note
                nPos = xOff * count; //moves the x-coordinate by 35 pixels each note

                mn = new MusicNote(nPos,pitch,duration,nShape); //Creation of a new MusicNote
                pictureBox1.Controls.Add(this.mn); //PROBLEM --- Doesn't add to the PictureBox (Does Nothing)
                pictureBox1.Controls[pictureBox1.Controls.Count - 1].BringToFront(); //Brought to front of stave to make sure it doesn't get hidden in background

            }

        }

    }
}

Any Idea how I can add CONTROL to the PictureBox1 and make the Music Note Show? Because I managed to make it Show on the Form1 and the Panel1 but failed on a pictureBox

How the Piano Looks with music notes on FORM1

钢琴

According to the Visual Studio DEBUGGER [Piano.Form1]

http://postimg.org/image/vdu0x1gv1/

NB Sorry for the long post but I don't know exactly how to explain the problem.

I think the Program was just messing with me.. I tried changing something to png and back to bmp and Now it's working perfectly.. I do not know why but It's the same Code there is up there

Thanks to those who have seen the question.

To play sound in picture box do this

 [DllImport("winmm.dll")]
        private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);

and PlaySound("your sound loc ", 1, 0x0011);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM