多个控制单击一个事件处理的事件 [英] Multiple Control Click Events handled by one event

查看:141
本文介绍了多个控制单击一个事件处理的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基本的winform应用程序,它具有许多带有点击事件的图片框。点击事件需要使用被点击的图片框的名称才能执行其余的代码。我不想为所有的图片框创建唯一的点击事件。我希望有一个简单的方式来获取这些信息,比如使用sender参数或事件参数。

I am creating a basic winform application that has many pictureboxes with click events. The click events need to use the name of the picturebox that was clicked in order to execute the rest of the code. I do not want to create unique click events for all of the pictureboxes. I was hoping there would be a simple way to get this information, like using the "sender" parameter or the event arguments.

推荐答案

为所有PictureBoxes添加一个点击事件处理程序:

You add one click event handler for all of the PictureBoxes:

pic1.Click += PictureBoxClick;
pic2.Click += PictureBoxClick;

然后将发件人转发到PictureBox,以获取点击哪一个,一个粗略的例子:


Then cast the sender to a PictureBox to get which one was clicked, a rough example:

private void PictureBoxClick(object sender, EventArgs e)
{
    var picBoxName = ((PictureBox)sender).Name;
}

不要忘记在卸载事件中取消挂起事件订阅:

Dont forget to unhook the event subscriptions in the form unload event:

pic1.Click -= PictureBoxClick;
pic2.Click -= PictureBoxClick;

这篇关于多个控制单击一个事件处理的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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