选择了哪个pictureBox?C# [英] Which pictureBox was selected? C#

查看:57
本文介绍了选择了哪个pictureBox?C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写钢琴.我遇到了一个小问题.

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.

创建的注释存储在一个名为picBox的PictureBox类型的数组中.我已经构造了以下事件处理程序,但是它不起作用.

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?

这里是将图片框(包含便笺)添加到工作人员(panel3)的方法.

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

推荐答案

如第一条评论中所述,您在错误的位置订阅了该事件.

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

还使用事件处理程序的 sender 参数来了解单击了哪个图片框(它将包含图片框的实例).

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

这篇关于选择了哪个pictureBox?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆