为PictureBox鼠标按下添加事件 [英] Add event for PictureBox mouse down

查看:394
本文介绍了为PictureBox鼠标按下添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使此活动正常进行:

I want to make this event to work:

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    //code
}

我知道我必须为此添加一个事件,但是我无法在任何地方找到语法.如何添加此事件?

I know I have to add an event for this to work but I wasn't able to find the syntax anywhere. How can I add this event?

推荐答案

您通常必须在表单的构造函数中为事件分配事件处理程序:

You have to assign event handler to the event, usually in the form's constructor:

class MyForm 
{ 
    PictureBox pictureBox1;

    public MyForm()
    {
        ...
        InitializeComponent(); 
        ...
        pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
        ... 
    }
}

如果通过Visual Studio中的窗体设计器添加了控件,它将自动生成InitializeComponent()方法,该方法创建控件(调用其构造函数),因此请确保在调用InitializeComponent()之后访问控件.

If you added your control through Form Designer in Visual Studio, it will automatically generate InitializeComponent() method which creates controls (calls their constructors) so make sure you're accessing controls after the call to InitializeComponent().

您还可以通过Form Designer将事件处理程序分配给事件:选择控件,右键单击它,选择属性,单击Flash图标( Events ),找到所需的事件(MouseDown)并双击它-事件处理程序方法将分配给该事件(您可以检查InitializeComponent()中的代码).现在,您只需要在事件处理程序的主体中编写代码.

You can also assign event handler to the event through Form Designer: select control, right click it, select Properties, click on flash icon (Events), find desired event (MouseDown) and double click it - event handler method will be assigned to that event (you can check the code in InitializeComponent()). Now you just have to write the code in the body of the event handler.

这篇关于为PictureBox鼠标按下添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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