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

南楼画角 提交于 2019-11-27 03:45:47
Matt Hamilton

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.
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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!