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

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

问题描述

我的WinForm UserControl中有一堆TextBox。每个这些文本框都有很少的事件处理程序,如On_Enter - 显示ListBox和建议,On_KeyUP - 如果Keys.Code == Keys.Enter - SelectNextControl()。当我把这个控件放在一张表单中时,这些事件都没有发生。如何将所有这些事件暴露给包含的表单?如何使UserControl的事件触发该UserControl的事件处理程序?

解决方案

所以,如果我理解正确,我认为有2方法可以继续:



方法1



在UserControl中,设置每个文本框的修饰符属性(或您感兴趣的)到public:





然后在使用此UserControl的窗体中,您可以访问所有这些文本框,从而访问它们的事件:

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

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

方法2
(取自此帖



您可以创建您的UserControl的新事件只是传递底层文本框的事件。然后,底层文本框可以保留为UserControl的私有。



在UserControl中添加此事件:

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

或者,您可以创建一个涉及所有文本框的单个事件: p>

 公共事件KeyEventHandler AnyTextBoxKeyDown 
{
添加
{
textBox1.KeyDown +值;
textBox2.KeyDown + = value;
textBox3.KeyDown + = value;
...
}
删除
{
textBox1.KeyDown - = value;
textBox2.KeyDown - = value;
textBox3.KeyDown - = value;
...
}
}

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

  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)
{
/ *我们已经知道TextBox1被按下,但是如果
*我们可以使用发件人
* object:* /
TextBox textBox1 =(TextBox)sender;

/ *在TextBox1(用户控件内)按下
*时,添加代码。 * /
}

private void myUserControl1_AnyTextBoxKeyDown(object sender,KeyEventArgs e)
{
/ *此事件处理程序可能由不同的
*文本框触发。要获取导致
*的实际文本框,此事件使用以下内容:* /
TextBox textBox =(TextBox)sender;

/ *在用户控件中按下
*键时,添加代码来处理。 * /
}
}

请注意,虽然这种方法保持文本框私有在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:

Approach 1

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

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

Approach 2 (Taken from this post)

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.

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;
        ...
    }
}

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. */
    }
}

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天全站免登陆