简体   繁体   English

来自另一个控件的C#触发/调用事件

[英]c# trigger/call events from another control

In VFP9, if i have two buttons (butt1 & butt2) If i want to trigger butt2 click event from butt1 i just do this from butt1 click event butt2.click() and it will call whatever code i have in the procedure/event in butt2. 在VFP9中,如果我有两个按钮(butt1&butt2),如果我想触发来自butt1的butt2 click事件,我只需通过butt1 click事件butt2.click()执行此操作,它将调用过程/事件中的任何代码屁股2。 How do i do that in c#? 我该如何在C#中做到这一点?

How do I call event of a certain control from another control like below 我如何从另一个控件中调用某个控件的事件,如下所示

.

private void button2_Click(object sender, EventArgs e)
        {
            button1.click()
        }

You can call the handler directly like this button1_Click(button1, null); 您可以像这样直接调用处理程序button1_Click(button1, null);

Example Usage: 用法示例:

Button button1;
Button button2;

public Form1()
{
    button1.Click += new EventHandler(button1_Click);
    button2.Click += new EventHandler(button2_Click);

}

void button2_Click(object sender, EventArgs e)
{
    button1_Click(button1, null);
}
public void button1_Click(object sender, EventArgs e)
{
    //Action when click occurs
}

Why are you trying to simulate button click, if you just want to execute some logic? 如果您只想执行一些逻辑,为什么要尝试模拟按钮单击? Wrap this logic into separate method, an call it from click handlers you choose: 将此逻辑包装到单独的方法中,从您选择的点击处理程序中调用它:

private Foo1() {}

private Foo2() {}

private button1_Click(object sender, EventArgs e)
{
  Foo1();
  Foo2();
}

private button2_Click(object sender, EventArgs e)
{
  Foo2();
}

只需将form_loaddesigner.cs的button1 click事件绑定到button2 click事件
button2.Click+=new EventHandler(button1_Click);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM