C#用户控制按钮单击事件处理程序从主窗体调用函数

C#用户控制按钮单击事件处理程序从主窗体调用函数

本文介绍了C#用户控制按钮单击事件处理程序从主窗体调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Form和一个UserControl。在我的UserControl中我有一个按钮,我想从Form中调用一个方法。任何人都可以帮助C#?

解决方案

您需要为usercontrol的按钮事件创建一个事件处理程序,并将其从

  class MyControl:UserControl 
{
public delegate void ButtonClickedEventHandler(对象发件人,EventArgs e);
public event ButtonClickedEventHandler OnUserControlButtonClicked;
}

现在您需要听实际按钮的事件:

  class MyControl:UserControl 
{
//见上面

public MyControl()
{
_myButton.Click + = new EventHandler(OnButtonClicked);
}

private void OnButtonClicked(object sender,EventArgs e)
{
//将事件委托给调用者
if(OnUserControlButtonClicked!= null )
OnUserControlButtonClicked(this,e);
}
}

从主窗体(所有者),您现在可以听对于事件:

  public FancyForm:Form 
{
public FancyForm()
{
_myUserControl.OnUserControlButtonClicked + = new EventHandler(OnUCButtonClicked);
}

private void OnUCButtonClicked(object sender,EventArgs e)
{
//从这里处理事件
MessageBox.Show(Horray! );
}
}

更新:两个实现通过使用Lambdas和默认的EventHandler代理定义,甚至可以安全地使用几行代码。

  class MyControl:UserControl 
{
public event EventHandler OnUserControlButtonClicked;
//如果要使用不同的EventArgs实现,请使用此功能:
// public event EventHandler< CustomEventArgs> OnUserControlButtonClicked;

public MyControl()
{
_myButton.Click + =(s,e)=>
{
if(OnUserControlButtonClicked!= null)
OnUserControlButtonClicked(this,e);
}
}
}

public FancyForm:Form
{
public FancyForm()
{
_myUserControl .OnUserControlButtonClicked + =(s,e)=>的MessageBox.show( Horray!);
}
}

更新2 :安全更多的行与C#6的力量:

  class MyControl:UserControl 
{
public event EventHandler OnUserControlButtonClicked;

public MyControl()
{
_myButton.Click + =(s,e)=> this.OnUserControlButtonClicked?.Invoke(this,e);
}
}


I have a Form and a UserControl. In my UserControl I have a button, and I would like to call a method from the Form. Can anyone help in C#?

解决方案

You need to create an event handler for the button-event of the usercontrol and fire it from the click event of the actual button.

class MyControl : UserControl
{
    public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
    public event ButtonClickedEventHandler OnUserControlButtonClicked;
}

Now you need to listen to the event of the actual button:

class MyControl : UserControl
{
    // See above

    public MyControl()
    {
        _myButton.Click += new EventHandler(OnButtonClicked);
    }

    private void OnButtonClicked(object sender, EventArgs e)
    {
        // Delegate the event to the caller
        if (OnUserControlButtonClicked != null)
            OnUserControlButtonClicked(this, e);
    }
}

From the main form (owner) you can now listen to the event:

public FancyForm : Form
{
    public FancyForm()
    {
        _myUserControl.OnUserControlButtonClicked += new EventHandler(OnUCButtonClicked);
    }

    private void OnUCButtonClicked(object sender, EventArgs e)
    {
        // Handle event from here
        MessageBox.Show("Horray!");
    }
}

Update: Both implementations can even safe a few lines of code by using Lambdas and default EventHandler delegate definitions.

class MyControl : UserControl
{
    public event EventHandler OnUserControlButtonClicked;
    // Use this, if you want to use a different EventArgs implementation:
    // public event EventHandler<CustomEventArgs> OnUserControlButtonClicked;

    public MyControl()
    {
        _myButton.Click += (s, e) =>
        {
            if (OnUserControlButtonClicked != null)
                OnUserControlButtonClicked(this, e);
        }
    }
}

public FancyForm : Form
{
    public FancyForm()
    {
        _myUserControl.OnUserControlButtonClicked += (s, e) => MessageBox.Show("Horray!");
    }
}

Update 2: Safe even more lines with the power of C# 6:

class MyControl : UserControl
{
    public event EventHandler OnUserControlButtonClicked;

    public MyControl()
    {
        _myButton.Click += (s, e) => this.OnUserControlButtonClicked?.Invoke(this, e);
    }
}

这篇关于C#用户控制按钮单击事件处理程序从主窗体调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:15