C# 将鼠标单击事件处理程序添加到图片框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13455439/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a mouse click eventhandler to a PictureBox
提问by Arianule
I have a picturebox control and upon clicking it another PictureBox appears at a specific location. I.o.w it wasnt added from the toolbox.
我有一个图片框控件,单击它后,另一个图片框会出现在特定位置。Iow 它不是从工具箱中添加的。
PictureBox picOneFaceUpA = new PictureBox();
        picOneFaceUpA.Location = new Point(42, 202);
        picOneFaceUpA.Width = 90;
        picOneFaceUpA.Height = 120;
        picOneFaceUpA.Image = Image.FromFile("../../Resources/" + picFaceUpToBeMoved[0] + ".png");
        Controls.Add(picOneFaceUpA);
        picOneFaceUpA.BringToFront();
How would I add a MouseClick eventhandler to this control?
我将如何向该控件添加 MouseClick 事件处理程序?
Thanks
谢谢
采纳答案by Jon B
Just add an event handler using the +=operator:
只需使用+=运算符添加一个事件处理程序:
picOneFaceUpA.MouseClick += new MouseEventHandler(your_event_handler);
Or:
或者:
picOneFaceUpA.MouseClick += new MouseEventHandler((o, a) => code here);
回答by Sergey Berezovskiy
Best way in WinForms to subscribe to event - select your control in designer, go to Properties window and select events tab. Then choose any event you want (Click, MouseClick, etc) and double-click on space to the right of event name. Event handler will be generated and subscribed to event.
WinForms 中订阅事件的最佳方式 - 在设计器中选择您的控件,转到“属性”窗口并选择“事件”选项卡。然后选择您想要的任何事件(单击、鼠标单击等)并双击事件名称右侧的空格。将生成事件处理程序并订阅事件。
If you want to subscribe to event manually, then add event handler to event
如果要手动订阅事件,则将事件处理程序添加到事件
picOneFaceUpA.MouseClick += PicOneFaceUpA_MouseClick;
Hitting Tabafter you write +=will generate handler. Or you can write it manually: 
Tab写入后点击+=将生成处理程序。或者您可以手动编写它:
void PicOneFaceUpA_MouseClick(object sender, MouseEventArgs e)
{
   // handle click event
   if (e.Button == MouseButtons.Left)
       MessageBox.Show("Left button clicked");
}
BTWif you don't need additional info about click event (e.g. which button was clicked), then use Clickevent instead. It handles left button clicks by default:
顺便说一句,如果您不需要有关单击事件的其他信息(例如,单击了哪个按钮),请改用Click事件。它默认处理左键点击:
picOneFaceUpA.Click += PicOneFaceUpA_Click;
And handler:
和处理程序:
void PicOneFaceUpA_Click(object sender, EventArgs e)
{
   // show another picture box
}
回答by John Koerner
If you type picOneFaceUpA.Click +=then hit tab, it will autocomplete for you and implement the event handler:
如果您输入picOneFaceUpA.Click +=然后点击选项卡,它将为您自动完成并实现事件处理程序:
    private void button2_Click(object sender, EventArgs e)
    {
        PictureBox picOneFaceUpA= new PictureBox();
        picOneFaceUpA.Click += new EventHandler(picOneFaceUpA_Click);
    }
    void picOneFaceUpA_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
回答by Mr_Green
It seems you know how to add dynamic controls then you should just do
看来您知道如何添加动态控件,那么您应该这样做
this.picOneFaceUpA.MouseClick += new MouseEventHandler(yourMethodName); //hook
and to remove a event
并删除事件
this.picOneFaceUpA.MouseClick -= yourMethodName;     //unhook
the method should be declared something like this:
该方法应声明如下:
private void yourMethodName(object sender, MouseEventArgs e)
{
    //your code here
}

