是否可以从其他表单触发点击事件? [英] Is it possible to trigger a click event from another form?

查看:132
本文介绍了是否可以从其他表单触发点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行另一个表单上的按钮的代码。可以用其他形式来做吗?如果您说可以将其宣布为公开,则可以:

I need to run the code of a button that is on another form. is it possible to do it from a different form? if you say that it is possible by declaring it public then:


  1. 我如何宣布控制权公开?

  2. 如何将正确的事件传递给 button_click ?它需要两个参数-我该如何传递它们呢?

  1. how do i declare a control public?
  2. how do i pass the correct events into button_click? it takes two parameters - how do i pass them,?


推荐答案

可以在表单 公共中进行控制,但不建议这样做,您可以执行以下操作:

It's possible to make a control in a Form public, but not recommended, you can do the following:

1)以第一种形式声明一个新事件(form1)ButtonFirstFormClicked

1) Declare a new event in the first form (form1) ButtonFirstFormClicked

public event EventHandler ButtonFirstFormClicked;

并在Button.Click事件处理程序中触发此事件

and trigger this event in the Button.Click event handler

void button_Clicked(object sender, EventArgs e)
{

    // if you're working with c# 6.0 or greater
    ButtonFirstFormClicked?.Invoke(sender, e);

    // if you're not then comment the above method and uncomment the below
    //if (ButtonFirstFormClicked!= null)
    //     ButtonFirstFormClicked(sender, e);
}  

2)在第二种形式(form2)中订阅事件

2) In the second form (form2) subscribe the event

form1.ButtonFirstFormClicked += (s, e)
{
 // put your code here...
} 

祝你好运!

这篇关于是否可以从其他表单触发点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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