识别谁调用事件处理程序

编程入门 行业动态 更新时间:2024-10-27 14:29:24
本文介绍了识别谁调用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

识别谁调用事件处理程序

本文发布于:2023-11-09 15:02:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!