0

I am working on a piano in C#. I have encountered a small problem.

I have a piano keyboard which, when pressed, displays the relevant note on the staff.

The notes created are stored in an array of type PictureBox, called picBox. I have constructed the following event handler, however it is not working.

private void pictureBox_Click(object sender, MouseEventArgs e)
        {
            picBox[0].MouseDown += new MouseEventHandler(pic_Click); //testing for first location
        }

    private void pic_Click(object sender, MouseEventArgs e)
    {
        ClickedTextBox.Text = "I was clicked";
    }

I am just testing to see if the first note was clicked. Why is this not working?

Here is the method which adds the picturebox (containing the note) to the staff (panel3).

public void addPictureBox(int x, int y, Image image)
        {
        picBox[cnt] = new PictureBox();

        picBox[cnt].Image = image;
        picBox[cnt].Location = new Point(x, y);
        picBox[cnt].BackColor = Color.Transparent;

        panel3.Controls.Add(picBox[cnt]);
        picBox[cnt].BringToFront();
        cnt++;
    }

What is wrong with my event handler please? Also, what can I do to identify the location in the array of the picturebox clicked? Thank you

Clayton
  • 123
  • 1
  • 2
  • 10

1 Answers1

1

As said in the first comment, you subscribe to the event in a wrong location.

Also use the sender parameter of your event handler to know which picturebox is clicked (it'll contain an instance of the picturebox).

ken2k
  • 48,145
  • 10
  • 116
  • 176