如何覆盖用户控件上的事件

发布于 2024-08-26 04:39:01 字数 282 浏览 5 评论 0原文

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

当 UserControl 的文本框更改时,我想过滤 OwnerForm 的内容。

但我怎样才能做到呢?我不想在用户控件内指定 OwnerForm。

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

提前致谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凹づ凸ル 2024-09-02 04:39:01

如果您的用户控件是由 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

您的用户控件是由 C# 制作的,请执行此操作,只需重定向用户控件的文本框的 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

如果 明智的是,VB.NET的FilterBox和C#的Filterbox是相同的。但C#中的实现更简单,只需将消费者程序员的事件直接插入到用户控件的textbox1的事件中即可。

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

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

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

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

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.

后来的我们 2024-09-02 04:39:01

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

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

牵你手 2024-09-02 04:39:01

覆盖(扩展)控制。看看是否可以将事件处理程序连接到文本框的 Changed 事件。如果您找不到这样的事件,请检查它是否具有可以覆盖的 OnTextChanged 函数(其名称可能有所不同,但大多数控件编写者遵循的约定是将 On* 函数设为虚拟,以便扩展的人控件可以覆盖它)。

如果这一切都失败了,请启动你的调试器,然后开始探索。查找上述事件和/或函数,然后通过一些反射来调用或挂钩它们。

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.

写给空气的情书 2024-09-02 04:39:01

仅当扩展 TextBox 时才可以覆盖。因此,您可能应该向进行过滤的文本框(文本更改)注册一个处理程序。

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.

恬淡成诗 2024-09-02 04:39:01

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

[注意:这是伪代码,请验证 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)
  }
}


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


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文