我如何知道鼠标被单击了哪个控件? [英] How do I tell which control the mouse has been clicked over?

查看:103
本文介绍了我如何知道鼠标被单击了哪个控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找了一段时间的代码,这些代码告诉我们鼠标单击了什么控件.我有一个包含50多个控件的窗体,并且我不想单击每个控件来使鼠标单击.我该怎么做?

I have been looking around for a while for some code that tells what control the mouse has clicked. I have a Form with over 50 controls and I don't want to click each one to make a mouse clicked on. How would I do this?

推荐答案

您可以使用每个控件的 Tag 属性.因此,将其设置为有意义的值,并在Click事件上执行以下操作:

You can use the Tag property of each control. So set it to something meaningful and on Click event do something like this:

(sender As Control).Tag

编辑:您也可以这样做:

foreach (Control item in this.Controls)     //this IS YOUR CURRENT FORM
{
    if ((sender as Control).Equals(item))
    {
        //Do what you want
    }
}

这篇关于我如何知道鼠标被单击了哪个控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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