Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Hi. I have a UserControl which contains a textbox. 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. How can I expose and handle particular events of the child controls from the publicly exposed UserControl in Winforms with C#.

like image 715
Anirudh Goel Avatar asked Jun 03 '09 05:06

Anirudh Goel


2 Answers

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; }     } } 
like image 154
Matt Hamilton Avatar answered Oct 11 '22 16:10

Matt Hamilton


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!

like image 30
Galilyou Avatar answered Oct 11 '22 16:10

Galilyou