Q:

How to override event on a UserControl

I have a WinForms application (OwnerForm) with some UserControl.
When textbox of UserControl is changed, I want to filter content of a OwnerForm.
But how can I make it? I don't want to specify OwnerForm inside the user control.
I know a solution to add manually handlers for MyUserControl.tb.TextChanged to some functions on a owner form, but I think it's bad way. I'll prefer to have overridable functions, but I can't imagine how to do it. Any suggestions?
Thanks in advance,

A:

do this if your usercontrol is made from VB.NET, have to handle the event and re-raise it to consumer of your control:
Public Class FilterBox

    <Browsable(True)> _
    Public Shadows Event TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        RaiseEvent TextChanged(sender, e)

    End Sub

End Class

do this if your usercontrol is made from C#, just redirect the TextChanged event of your usercontrol's textbox:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Craft
{

    public partial class FilterBox : UserControl
    {
        public FilterBox()
        {
            InitializeComponent();            
        }

        [Browsable(true)]
        public new event EventHandler TextChanged
        {
            add
            {
                textBox1.TextChanged += value;
            }
            remove
            {
                textBox1.TextChanged -= value;
            }
        }//TextChanged "surfacer" :-)

    }//FilterBox
}//Craft

consuming-wise, VB.NET's FilterBox and C#'s Filterbox are the same.  but the implementation in C# is more straightforward, it just plug the event of consumer programmer directly to usercontrol's textbox1's event. 
i think the title of the article Defining Add and Remove Accessor Methods for Events in VB.NET should be: Want to be the envy of all your VB friends? 
as you can see from the implementation code above, C# has less runtime overhead.
the C# code above is not possible in VB.NET: One might ask "why should I care?" Well, C# permits programmers to define their own add and remove subscription events. As a result a C# developer can extend the behavior of the add and remove subscription methods. One useful application of the add and remove handler is to surface an event on a constituent control
note: to consume your usercontrol's textbox's changed event.  in VS designer, click the Properties toolbox, click the event(lightning) icon, double-click the TextChanged, add the necessary logic.

