公开 UserControl 的所有事件处理程序 [英] Expose all event handlers of UserControl

查看:27
本文介绍了公开 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 的所有事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆