简体   繁体   English

在c#中的usercontrol中公开并引发子控件的事件

[英]expose and raise event of a child control in a usercontrol in c#

Hi. 你好。 I have a UserControl which contains a textbox. 我有一个包含文本框的UserControl。 I wanted to access the textchanged event of the textbox but in the event properties of the usercontrol I don't see the events for the textbox. 我想访问文本框的textchanged事件,但在usercontrol的事件属性中,我没有看到文本框的事件。 How can I expose and handle particular events of the child controls from the publicly exposed UserControl in Winforms with C#. 如何使用C#从Winforms中公开公开的UserControl公开和处理子控件的特定事件。

You can surface a new event and pass any subscriptions straight through to the control, if you like: 如果您愿意,可以展示新事件并将所有订阅直接传递给控件:

public class UserControl1 : UserControl 
{
    // private Button saveButton;

    public event EventHandler SaveButtonClick
    {
        add { saveButton.Click += value; }
        remove { saveButton.Click -= value; }
    }
}

Expose the entire TextBox as a public property in user control and subscribe to it's events the way you desire. 将整个TextBox公开为用户控件中的公共属性,并以您希望的方式订阅它的事件。
Example: 例:

class myUserControl: UserControl { 
private TextBox _myText;
public TextBox MyText { get {return _myText; } }

} }

After doing this you can subscribe on any of its events like so: 完成此操作后,您可以订阅任何事件,如下所示:

theUserControl.MyText.WhatEverEvent += ... 

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM