如何创建事件处理程序? [英] How to create an event handler?

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

问题描述

这是场景..



我有两个名为ucAdmin和ucTransactions的UserControl,它们将被特别添加到拆分容器中的Main表单中。首先ucAdmin将显示并且ucTransactions最初是隐藏的。从ucAdmin,有一个工具条按钮,它将显示ucTransactions并隐藏ucAdmin。在ucTransactions中是一个链接,它将显示ucAdmin并隐藏ucTransactions。



如何在不重新初始化主表单的情况下执行此操作?



我可以使用将放置在主窗体中的EventHandler来执行此操作吗?



我是编程新手,我想制作这样的设计。请帮我解决这个问题。



谢谢大家..

Here's the scenario ..

I have two UserControls named ucAdmin and ucTransactions which will be added to the Main form in a split container particularly. First the ucAdmin will show and the ucTransactions is hidden initially. From the ucAdmin, there is a toolstrip button that will show the ucTransactions and hide ucAdmin. And in the ucTransactions is a link that will show the ucAdmin and hide ucTransactions.

How can i do this without re initializing the Main form to new?

Can I do this with an EventHandler which will be placed in the Main form?

I am new in programming in I want to make a design like that. Please help me through this.

Thank you all..

推荐答案

假设你设计了一个表格, 'Form1,和两个UserControls,'TestUC1,'TestUC2。



两个UserControl都有一个按钮控件:TestUC1有'btnShowTestUC2 ... TestUC2有'btnShowTestUC1。



我将首先向您展示在C#中创建事件的两种标准方法,然后,我们将向您展示一种简单明了的技术。



这是一个快速草图:
Assume you designed a Form, 'Form1, and two UserControls, 'TestUC1, 'TestUC2.

Both UserControls have a Button Control: TestUC1 has 'btnShowTestUC2 ... TestUC2 has 'btnShowTestUC1.

I'll start with showing you two variations of the standard ways to create Events in C#, then, we'll show you a technique that is radically simpler.

Here's a quick sketch:
// in the Form that contains design-time placed instances of TestUC1 and TestUC2:
private void Form1_Load(object sender, EventArgs e)
{
    // note 0
    testUC11.MakeUC2Visible += testUC11_OnMakeUC2Visible;

    testUC21.MakeUC1Visible += testUC21_OnMakeUC1Visible;

    testUC21.Visible = false;
}

// EventHandlers defined in the Form:

private void testUC21_OnMakeUC1Visible(object sender, EventArgs e)
{
    testUC11.Visible = true;
    testUC21.Visible = false;
}

private void testUC11_OnMakeUC2Visible(object sender, EventArgs e)
{
    testUC21.Visible = true;
    testUC11.Visible = false;
}

UserControls:

The UserControls:

// user control one: TestUC1
public partial class TestUC1 : UserControl
{
    public TestUC1()
    {
        InitializeComponent();
    }

    // note 1
    public delegate void MakeUC2VisibleEventHandler(object sender, EventArgs e);

    public event MakeUC2VisibleEventHandler MakeUC2Visible;

    protected virtual void OnMakeUC2Visible(EventArgs e)
    {
        if (MakeUC2Visible != null) MakeUC2Visible(this, e);
    }

    private void btnShowTestUC2_Click(object sender, EventArgs e)
    {
        OnMakeUC2Visible(e);
    }
}

// user control two: TestUC2
public partial class TestUC2 : UserControl
{
    public TestUC2()
    {
        InitializeComponent();
    }

    // note 2
    public event EventHandler MakeUC1Visible;

    protected virtual void OnMakeUC1Visible(EventArgs e)
    {
        EventHandler handler = MakeUC1Visible;

        if (handler != null) handler(this, e);
    }

    private void btnShowTestUC1_Click(object sender, EventArgs e)
    {
        OnMakeUC1Visible(e);
    }
}

注0:表格中定义的UserControls的EventHandlers



注1:旧的事件声明的样式需要委托和事件。



注2:更新的样式......可用于.NET 2.0及更高版本...不会需要一个明确的代表



是的,你是对的:我们做了很多转移货物只是为了获得传递给UserControl容器的事件的通知(形成)。有一种更简单的方法:试试这个:

Note 0: EventHandlers for the UserControls defined in the Form

Note 1: The "older" style of Event declaration requires both Delegate and Event.

Note 2: The "newer" style ... available with .NET 2.0 and later ... does not require an explicit Delegate

Yes, you're right: we did a lot of shifting cargo just to get a notification of an event passed out to the UserControl's container (the Form). Is there a simpler way: try this:

// the Form
private void Form1_Load(object sender, EventArgs e)
{
    testUC11.BtnAction = setUCVisibility;
    testUC21.BtnAction = setUCVisibility;
}

private void setUCVisibility(UserControl userControl)
{
    if (userControl is TestUC1)
    {
        testUC11.Visible = false;
        testUC21.Visible = true;
    }
    else
    {
        testUC21.Visible = false;
        testUC11.Visible = true;
    }
}

UserControls:

The UserControls:

// TestUC1
public Action<UserControl> BtnAction;

private void btnShowTestUC2_Click(object sender, EventArgs e)
{
    if (BtnAction != null) BtnAction(this);
}

// TestUC2    // guard against undefined Action
public Action<UserControl> BtnAction;

private void btnShowTestUC1_Click(object sender, EventArgs e)
{
    // guard against undefined Action
    if (BtnAction != null) BtnAction(this);
}

我们声明了Type Action的Public委托(Action总是没有返回值,在这种情况下,接受一个参数)[ ^ ]。在每个UserControl的Button Click EventHandler中,我们调用该动作,将引用传递给Button所在的UserControl实例。



从技术上讲,这个例子的作用是对运行时方法的引用注入UserForms的实例。有人可能会争辩说,这就像提出事件的其他方式一样,会导致UserControls和他们的使用者之间的松散耦合:在Form和UserControls代码之间没有直接链接,没有依赖关系。 UserControls不知道谁/什么插入一个方法,其签名与其代码中声明的Action匹配,或者该方法在执行时将执行的操作。



In我们定义了一个方法,即setUCVisibility,它被分配给两个UserControls中的'Action委托。在这个方法中,我们检测哪个UserControl触发了'Action并做了正确的事情来修改哪个UserControl是可见的,这是隐藏的。

We declared a Public delegate of Type Action (an Action always has no return value, and, in this case, takes one parameter) [^] in each UserControl. In each UserControl's Button Click EventHandler we invoke the action, passing a reference to the UserControl instance the Button is 'in.

Technically, what this example does is inject a reference to a method at run-time into the instances of the UserForms. One could argue that this, like the other ways of raising events shown, results in loose-coupling between the UserControls and their "consumers:" there's no direct-linkage, no dependency, between the Form and the UserControls code. The UserControls "don't know" who/what inserts a method whose signature matches the Action declared in their code, or what that method will do when executed.

In the Form we define one method, setUCVisibility, which is assigned to the 'Action delegate in both UserControls. In this method, we detect which UserControl fired the 'Action and do the right thing to modify which UserControl is visible, which is hidden.


这篇关于如何创建事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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