如何覆盖UserControl上的事件

时间:2021-07-02 15:49:19

I have a WinForms application (OwnerForm) with some UserControl.

我有一个WinForms应用程序(OwnerForm)与一些UserControl。

When textbox of UserControl is changed, I want to filter content of a OwnerForm.

当UserControl的文本框发生变化时,我想过滤OwnerForm的内容。

But how can I make it? I don't want to specify OwnerForm inside the user control.

但我该怎么做呢?我不想在用户控件中指定OwnerForm。

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?

我知道一个解决方案,将MyUserControl.tb.TextChanged的手动处理程序添加到所有者表单上的某些函数,但我认为这是不好的方法。我更喜欢有可覆盖的功能,但我无法想象如何做到这一点。有什么建议么?

Thanks in advance,

提前致谢,

5 个解决方案

#1


3  

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

如果您的usercontrol是从VB.NET制作的,那么必须处理该事件并将其重新提升为您的控件的使用者:

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:

如果你的用户控件是用C#编写的,只需重定向usercontrol文本框的TextChanged事件:

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.

消费方面,VB.NET的FilterBox和C#的Filterbox是相同的。但是C#中的实现更直接,它只是将消费者程序员的事件直接插入到usercontrol的textbox1的事件中。

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?

我认为在VB.NET中为事件定义添加和删除访问器方法的文章的标题应该是:想成为所有VB朋友的羡慕?

as you can see from the implementation code above, C# has less runtime overhead.

从上面的实现代码中可以看出,C#具有较少的运行时开销。

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

上面的C#代码在VB.NET中是不可能的:有人可能会问“我为什么要关心?”好吧,C#允许程序员定义他们自己的添加和删除订阅事件。因此,C#开发人员可以扩展添加和删除订阅方法的行为。添加和删​​除处理程序的一个有用的应用是在组成控件上显示事件

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.

注意:使用usercontrol的文本框已更改的事件。在VS设计器中,单击Properties工具箱,单击事件(闪电)图标,双击TextChanged,添加必要的逻辑。

#2


1  

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

在UserControl中创建一个FilterChanged事件,该事件将在内部TextBox的TextChanged事件上引发。然后你会很好地封装它。

#3


1  

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

覆盖(扩展)控件。看看是否可以将事件处理程序连接到文本框的Changed事件。如果你找不到那样的事件,那么检查它是否有你可以覆盖的OnTextChanged函数(它的名称可能不同,但大多数控件编写者遵循的惯例是使On *函数成为虚拟的,以便扩展的人控件可以覆盖它)。

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.

如果做不到这一点,请启动调试器,然后进行探险。寻找上述事件和/或函数,然后通过一些反思来调用或挂钩它们。

#4


0  

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.

只有在扩展TextBox时才能覆盖。因此,您可能应该在文本框(文本更改)中注册处理程序,您可以在其中进行过滤。

#5


0  

You can provide some controller to do such operations.

您可以提供一些控制器来执行此类操作。

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

[注意:这是伪代码,验证textchanged的eventhandler]


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)
  }
}


#1


3  

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

如果您的usercontrol是从VB.NET制作的,那么必须处理该事件并将其重新提升为您的控件的使用者:

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:

如果你的用户控件是用C#编写的,只需重定向usercontrol文本框的TextChanged事件:

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.

消费方面,VB.NET的FilterBox和C#的Filterbox是相同的。但是C#中的实现更直接,它只是将消费者程序员的事件直接插入到usercontrol的textbox1的事件中。

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?

我认为在VB.NET中为事件定义添加和删除访问器方法的文章的标题应该是:想成为所有VB朋友的羡慕?

as you can see from the implementation code above, C# has less runtime overhead.

从上面的实现代码中可以看出,C#具有较少的运行时开销。

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

上面的C#代码在VB.NET中是不可能的:有人可能会问“我为什么要关心?”好吧,C#允许程序员定义他们自己的添加和删除订阅事件。因此,C#开发人员可以扩展添加和删除订阅方法的行为。添加和删​​除处理程序的一个有用的应用是在组成控件上显示事件

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.

注意:使用usercontrol的文本框已更改的事件。在VS设计器中,单击Properties工具箱,单击事件(闪电)图标,双击TextChanged,添加必要的逻辑。

#2


1  

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

在UserControl中创建一个FilterChanged事件,该事件将在内部TextBox的TextChanged事件上引发。然后你会很好地封装它。

#3


1  

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

覆盖(扩展)控件。看看是否可以将事件处理程序连接到文本框的Changed事件。如果你找不到那样的事件,那么检查它是否有你可以覆盖的OnTextChanged函数(它的名称可能不同,但大多数控件编写者遵循的惯例是使On *函数成为虚拟的,以便扩展的人控件可以覆盖它)。

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.

如果做不到这一点,请启动调试器,然后进行探险。寻找上述事件和/或函数,然后通过一些反思来调用或挂钩它们。

#4


0  

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.

只有在扩展TextBox时才能覆盖。因此,您可能应该在文本框(文本更改)中注册处理程序,您可以在其中进行过滤。

#5


0  

You can provide some controller to do such operations.

您可以提供一些控制器来执行此类操作。

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

[注意:这是伪代码,验证textchanged的eventhandler]


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)
  }
}