如何在另一个按钮中调用一个按钮单击事件 [英] How to call one button click event in another

查看:80
本文介绍了如何在另一个按钮中调用一个按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在另一个

解决方案

中调用一个按钮单击事件。相反,设置一个私有方法来执行该函数并从两个事件处理程序中调用它。


你可以尝试下面的,



 命名空间 EventHandlerCall 
{
使用系统;
使用 System.Windows.Forms;

public partial class frmMain:表格
{
public frmMain()
{
InitializeComponent();
btnOne.Click + = new EventHandler(common_Click);
btnTwo.Click + = new EventHandler(common_Click);
}

private void common_Click( object sender,EventArgs e)
{
CommonFuncationality();
}

private void CommonFuncationality()
{
MessageBox.Show( Hello world);
}
}
}





:)


  private   void  btnA_Click( object  sender,EventArgs e)
{
bnnB_Click(sender,e);
}
私有 void btnB_Click( object sender,EventArgs e)
{
ExecuteFunction();
}





但最佳做法是:

 < span class =code-keyword> private   void  btnA_Click( object  sender,EventArgs e )
{
ExecuteFunction();
}
私有 void btnB_Click( object sender,EventArgs e)
{
ExecuteFunction();
}
private void ExecuteFunction()
{
// 您的代码
}



积分可转入此处


How to call one button click event in another

解决方案

Don't. Instead, set up a private method that performs the function and call it from both event handlers.


You might try below,

namespace EventHandlerCall
{
    using System;
    using System.Windows.Forms;

    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            btnOne.Click += new EventHandler(common_Click);
            btnTwo.Click += new EventHandler(common_Click);
        }

        private void common_Click(object sender, EventArgs e)
        {
            CommonFuncationality();
        }

        private void CommonFuncationality()
        {
            MessageBox.Show("Hello world");
        }
    }
}



:)


private void btnA_Click(object sender, EventArgs e)
{
    btnB_Click(sender, e);
}
private void btnB_Click(object sender, EventArgs e)
{
    ExecuteFunction();
}



But best practice is:

private void btnA_Click(object sender, EventArgs e)
{
   ExecuteFunction();
}
private void btnB_Click(object sender, EventArgs e)
{
   ExecuteFunction();
}
private void ExecuteFunction()
{
    // Your code here
}


Credit goes to here


这篇关于如何在另一个按钮中调用一个按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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