如何在用户控件中引发事件并在另一个用户控件中捕获它? [英] How Do I Raise An Event In A Usercontrol And Catch It In Another User Control?

查看:58
本文介绍了如何在用户控件中引发事件并在另一个用户控件中捕获它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在usercontrol中引发事件并在另一个用户Control中捕获它?

How do i raise an event in a usercontrol and catch it in another user Control?

推荐答案

对于这种专用事件/消息传递,我更喜欢在UserControl1中使用'Action delegate Type:
For this kind of "dedicated event/message passing" I prefer to use the 'Action delegate Type:
// in UserControl1:
public Action<string> OnTitleChanged;

private void textBox1_TextChanged(object sender, EventArgs e)
{
   if(OnTitleChanged != null) OnTitleChanged(textBox1.Text);
}

// in UserControl2:
public void SetText(string theText)
{
    textBox1.Text = theText;
}

// in the Form that creates instances of the UserControls:
private void Form1_Load(object sender, EventArgs e)
{
    userControl1Instance.OnTitleChanged = userControl2Instance.SetText;
}</string>

注意:



1.行动<> (或Func<>)是一个功能齐全的代表类型;他们能够使用+ =和 - =语法设置或删除多个订阅者。



2.通过直接分配对方法的引用(其Type参数与Action规范相匹配)...而不是使用+ =,你基本上将这个Action专用于一个目的,即将字符串传递给它封装的任何方法。



我相信这种用法是件好事,因为:



a。它简化了:您无需创建自定义事件参数等即可获得Event / EventHandler的好部分。



b。 imho,这种模式的使用传达了程序员的意图,即拥有一个专用的Event / EventHandler,它可以做到一件事。



3。 imho,这个模式不应该用在你发布代码的地方,期望最终用户可以为它创建多个订阅者......尽管可以在没有错误的情况下使用Action或Func。

Notes:

1. An Action<> (or a Func<>) is a full-featured Delegate Type; they are capable of having multiple subscribers which can be set, or removed, using += and -= syntax.

2. By directly assigning a reference to a method (whose Type parameters match the Action specification) ... rather than using +=, you essentially "dedicate" this Action to one purpose, which is passing a string to whatever method it encapsulates.

I believe this usage is a good thing, because:

a. it is simplifying: you get the "good parts" of an Event/EventHandler without the need to create Custom EventArguments, etc.

b. imho, the use of this "pattern" is communicates the intent of the programmer to have a "dedicated Event/EventHandler" that does "one thing."

3. imho, this "pattern" should not be used where you are publishing your code with the expectation that end-users may create multiple subscribers to it ... although that could be used without error with Action, or Func.


基本上,你不应该这样做:一个控件不应该知道另一个控件的存在。如果他们这样做,那么你将两个控件锁定在一起,你不能将它们单独用于任何其他目的。



相反,你的表格应该处理来自Control1的事件,从中获取数据,并通过属性或方法将其传递给Control2。由于Form包含两个控件,因此必须知道它们的存在,所以一切都很好。



这里有一个例子:在两个表格之间转移信息,第3部分:孩子对孩子 [ ^ ] - 它是关于表单的,但控件应该表现为完全相同的方式。
Basically, you shouldn't be doing it like that: one control shouldn't know about the existence of the other. If they do, then you "lock" the two controls together, and you can't use them independently for any other purpose.

Instead, your form should handle the Event from Control1, fetch the data from it, and pass it to Control2 via a Property or Method. Since the Form contains both controls, it has to know about their existence, so that's all fine.

There is an example here: Transferring information between two forms, Part 3: Child to Child[^] - it's about Forms, but Controls should behave in exactly the same way.


这篇关于如何在用户控件中引发事件并在另一个用户控件中捕获它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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