开发者

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 ho开发者_Go百科w 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,


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.


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


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.


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.


You can provide some controller to do such operations.

[Note: this is pseudo code, verify eventhandler for textchanged]


public interface IOperationController
{
  bool DataChanged(string newData,string oldData);
}


public class YourControl:UserControl
{
  private IOperationController_controller;

  public void SetController(IOperationControllercontroller)
  {
    _operationController = controller;
  }

  private void OnTextChanged(object sender, TextChangedEventArgs e)
  {
   if(_operationController != null && _operationController.DataChanged(e.Value,textBox.Text))
   {
    //perform your operation
   }
  }
}

public class OwnerForm:Form,IOperationController
{
  public OwnerForm()
  {
    yourControlInstance.SetController(this)
  }
}


0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜