从form2单击form1的按钮 [英] clicking form1`s button from form2

查看:159
本文介绍了从form2单击form1的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.
我正在使用Windows窗体C#.即时通讯试图使form2按钮从form1单击
如何做到这一点,请
例如:

Hello.
Im using windows forms c#. im trying to make form2 button click from form1
how can done that help please
For example:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            AddNew an = new AddNew();
an.btnOk.Click == an.txtProductName.Text = "Fine";


这行不通,您有什么建议?
THanks:)


This does not work what is your advice?
THanks :)

推荐答案

在要执行单击操作的表单上提供公共访问器,然后从其他表单中调用它.

例如
表格1(带按钮的表格)
Provide a public accessor on the form you want the click operation performed, then call this from the other form.

e.g.
Form 1 (The one with the button)
Public void doClick()
{
   buttonXYZ.performClick();
}



表格2



Form 2

form1.doClick();



显然,您需要使对象引用在表格2中已经处于活动状态



You obviously need to have an object reference already active within form 2


与其模拟按钮单击,不如执行按钮单击处理程序的操作要好得多完成.
Instead of simulating a button click, it would be much better to perform the action that the button''s click handler would otherwise have done.


首先,您需要在form1中引用form2的按钮.
然后,您只需执行_myButton_Event(null,null).
First you will need a reference to the button of form2 in form1.
Then you can simply do _myButton_Event(null,null).


这篇关于从form2单击form1的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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