公开 UserControl 的所有事件处理程序

Expose all event handlers of UserControl(公开 UserControl 的所有事件处理程序)
本文介绍了公开 UserControl 的所有事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WinForm 用户控件中有一堆文本框.每个文本框都有一些事件处理程序,例如 On_Enter - 显示带有建议的 ListBox,On_KeyUP - if Keys.Code == Keys.Enter - SelectNextControl().当我将该控件放在表单中时,这些事件都不会触发.如何将所有这些事件暴露给包含表单?如何使 UserControl 的事件触发该 UserControl 的事件处理程序?

I have a bunch of TextBoxes in my WinForm UserControl. Each of those text boxes has few event handlers such as On_Enter - show ListBox with suggestions, On_KeyUP - if Keys.Code == Keys.Enter - SelectNextControl(). When I place that control in a Form, none of those events fire up. How to expose all those events to the containing form? How to make UserControl's events fire up event handlers of that UserControl?

推荐答案

所以,如果我理解正确的话,我认为有两种方法可以继续:

So, if I understand correct, I think there are 2 ways you can proceed:

方法 1

在 UserControl 中,将每个文本框(或您感兴趣的文本框)的 Modifiers 属性设置为 public:

In the UserControl, set the Modifiers property of each textbox (or the ones you are interested in) to public:

然后在使用此 UserControl 的表单中,您可以访问所有这些文本框以及它们的事件:

Then in the Form that uses this UserControl you can access all these textboxes and hence their events:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        myUserControl1.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
    }
}

方法 2(取自这篇文章)

您可以为您的 UserControl 创建新事件,只需将底层文本框的事件传递出去.然后,底层文本框可以对 UserControl 保持私有.

You can create new events for your UserControl that simply pass forward the event of an underlying textbox. The underlying textboxes can then remain private to the UserControl.

在 UserControl 中添加这个事件:

In the UserControl add this event:

public event KeyEventHandler TextBox1KeyDown
{
    add { textBox1.KeyDown += value; }
    remove { textBox1.KeyDown -= value; }
}

或者您可以创建一个处理所有文本框的事件:

Or you can create a single event that deals with all textboxes:

public event KeyEventHandler AnyTextBoxKeyDown
{
    add
    {
        textBox1.KeyDown += value;
        textBox2.KeyDown += value;
        textBox3.KeyDown += value;
        ...
    }
    remove
    {
        textBox1.KeyDown -= value;
        textBox2.KeyDown -= value;
        textBox3.KeyDown -= value;
        ...
    }
}

现在您的 UserControl 有自己的事件,表单中的代码可以使用:

Now your UserControl has a event of its own that the code in the Form can use:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        myUserControl1.TextBox1KeyDown += new KeyEventHandler(myUserControl1_TextBox1KeyDown);
        myUserControl1.AnyTextBoxKeyDown += new KeyEventHandler(myUserControl1_AnyTextBoxKeyDown );
    }

    private void myUserControl1_TextBox1KeyDown(object sender, KeyEventArgs e)
    {
        /* We already know that TextBox1 was pressed but if
         * we want access to it then we can use the sender
         * object: */
        TextBox textBox1 = (TextBox)sender;

        /* Add code here for handling when a key is pressed
         * in TextBox1 (inside the user control). */
    }

    private void myUserControl1_AnyTextBoxKeyDown(object sender, KeyEventArgs e)
    {
        /* This event handler may be triggered by different
         * textboxes. To get the actual textbox that caused
         * this event use the following: */
        TextBox textBox = (TextBox)sender;

        /* Add code here for handling when a key is pressed
         * in the user control. */
    }
}

请注意,虽然这种方法在 UserControl 中保持文本框私有,但仍然可以通过 sender 参数从事件处理程序访问它们.

Note that while this approach keeps the textboxes private within the UserControl, they can still be accessed from the event handler by the sender argument.

这篇关于公开 UserControl 的所有事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)