识别谁调用事件处理程序 [英] Identify who invokes Event Handler

查看:148
本文介绍了识别谁调用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序的文本被修改时,我的标签被写入集中。

I have my app with my labels written to centralize when its text was modified.

为了使我通过EventHandler调用一个方法。我想对每个我的标签使用相同的方法,但是我不知道如何识别在哪个标签中调用它。

To make that I invoke a method through an EventHandler. I want to use same method to each one of my labels, but I don´t know how to identify in the method which label invoke it.

如果我有这样的代码:

       lbl_TextChanged(object sender, EventArgs e)
        {
           ..code..
           label1.Location = new Point("label's location");
           ..more code..
        }

我再次调用该方法从另一个标签,我如何修改该代码,以了解哪个标签调用?

and I invoke again that method from another label, how could I modify that code in order to know which label invoke it?

解决方案感谢您的帮助,这是第一次我试图用事件处理程序做一些事情,不知道我需要投递发件人。这解决了我的问题。

SOLUTION Thanks for the help, This is the first time I tried to make something with the event handler and didn't know that I need to cast the sender. That solves my problem.

感谢您的帮助!

推荐答案

事件处理程序签名中的发件人提起事件。

The sender object in the event handler signature is the control that raised the event.

只需投掷此对象,即可访问所有控件的信息。

Simply cast this object and you have access to all the control's information.

Label label = sender as Label;

你确实需要一点点小心,因为你假设只有类型的控件标签正在提升具有此处理方法的事件。

You do need to be a little bit careful with this, since you are assuming that only controls of type Label are raising events that have this handler method.

这篇关于识别谁调用事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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