为什么我的图片框不能响应MouseClick事件? [英] Why wont my picturebox respond to a MouseClick event?
问题描述
您好,我正在制作一个简单的应用程序,但我注意到一些很奇怪的东西,因此为了帮助解释此问题,我创建了一个测试应用程序.
我创建了2个图片框,第一个图片是使用代码创建的,并将其命名为"pic".我使用工具箱创建的第二个图片框,只是将其保留为标准的"picturebox1"名称.
因此,我将两个图片框的背景色都设置为黑色,并为它们创建了一个事件,当我单击它们时,它们会将背景色更改为蓝色.
现在这是一个问题,当我单击"picturebox1"时,它变为蓝色.但是,当我单击图片"时却没有,为什么会发生这种情况?如何使活动在图片"上正常播放?
这是我的示例代码:
Hi, I am making a simple application and I noticed something quite strange so to help explain this problem I created a test application.
I created 2 pictureboxes, the first one I created using code and named it "pic". The second picturebox I created using the toolbox and just left it with the standard "picturebox1" name.
So moving along, I set both of the pictureboxes backcolor to black and I made an event for both of them that will change the backcolor to blue when I click on them.
Now heres the problem, when I click on "picturebox1" it changes to the color blue. But when I click on "pic" it doesn''t, why does this happen? How can I make the event work on "pic"?
here is my sample code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
PictureBox pic = new PictureBox();
private void Form1_Load(object sender, EventArgs e)
{
pic.BackColor = Color.Black;
panel1.Controls.Add(pic);
pic.Location = new Point(200, 100);
}
private void pic_MouseClick(object sender, MouseEventArgs e)
{
pic.BackColor = Color.Blue;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pictureBox1.BackColor = Color.Blue;
}
}
}
我将不胜感激!
谢谢,
MR.AngelMendez
I will appreciate all help given!
thanks,
MR.AngelMendez
推荐答案
Hi在其中,您必须初始化事件,右键单击InitializeComponent()方法->转到定义并复制并粘贴事件处理程序
Hi Inside this you have to initialize your event right click on InitializeComponent() method -> go to definition and copy and paste event handler
public Form1()
{
InitializeComponent();
}
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
this.pic.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pic_MouseClick);
这篇关于为什么我的图片框不能响应MouseClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!