views:

376

answers:

5

Hello!

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: 

You can override only if you extend the TextBox. So you should probably register a handler to the textbox (text change) where you do your filtering.

thelost
Can you give my any example or link with explanation please?
Vadim
A: 
Rakesh Gunijan
+1  A: 

Create a FilterChanged event in UserControl which will be raised on TextChanged event of inner TextBox. Then you will have it nicely encapsulated.

Axarydax
If i created in a UserControl such a sub: Public Overridable Sub FilterParentForm() End Subhow can I override it in a OwnerForm? I want to write something like Public Overrides Sub UserControl.FilterParentForm End Subbut I can't.
Vadim
if your OwnerForm inherits UserControl, you can do overrides sub. override work only in context of inheritance. if you drag your usercontrol to ownerform, you must have an assignable event in your usercontrol and add handler to that event, just like any other controls in your program, e.g. TextChanged, KeyPress, Click, etc
Michael Buen
A: 

Override (extend) the control. See if you can wire up an event handler to the Changed event of the textbox. If you cannot find an event like that, then check to see if it has a OnTextChanged function that you can override (its name may differ, but the convention followed by most control writers is to make the On* functions virtual so that people who extend the control can override it).

Failing all that, fire up your debugger, and go spelunking. Look for the aforementioned events and/or functions, then get nasty with some reflection to invoke or hook them.

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

Michael Buen