我可以在form2按钮中调用form1 button1单击事件吗? [英] can i call the form1 button1 click event in form2 button

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

问题描述

我想在form2中调用form1的按钮单击事件吗?

i want to call the form1 button click event in form2 is it possible to call the event click of button?

推荐答案

首先,没有这样的事件概念称为通话事件".您可以调用和事件处理程序.更确切地说,编写一些方法以实现单击时要调用的某些操作.在两个地方使用此方法:1)在click事件处理程序中,2)需要在不单击的情况下调用它的方法.

现在,存在一个问题,如何使以一种形式实现的该方法可以以另一种形式访问.是的,您可以使此方法为internal. (无需像拉杰夫建议的那样公开它.切勿提供确实需要的更多访问权限.)这将解决问题,但您还需要将对一个表单实例的引用传递给另一表单实例.这不是很好,因为它破坏了表单的隔离,并可能在较大的应用程序中引发一些错误.

有更准确,更高级的方法.实际上,这是关于表单协作的流行问题.最可靠的解决方案是在表单类中实现适当的接口.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
First of all, there is no such concept as "call event". You can call and event handler. More exactly, write some method implementing some action to be called on click. Use this method in two places: 1) in the click event handler, 2) where you need to call it without click.

Now, there is a problem how to make this method implemented in one form accessible in another form. Yes, you can make this method internal. (No need to make it public as Rajeev suggested. Never give more access that it is really required.) This will solve the problem, but you also need to pass a reference to one form instance to another one. This is not very good as it breaks isolation of forms and can invite some bugs in bigger applications.

There are more accurate and advanced approaches. Actually, this is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

As you only pass the reference to the form as the reference to the interface, you only pass the access to what is really needed, no more.

—SA


执行以下操作....

Form1
---------
将按钮Click事件button1_Form1的Click公开.

Form2
-------

Do the following things....

Form1
---------
Make the button Click event button1_Click of the Form1 as public.

Form2
-------

Form1 f = new Form1();
 f.button1_Click(sender, e);


是的.但是更好的方法是..
您在Form1内创建一个公共方法,请从该方法调用ButtonClick事件.在初始化Form2时将表单对象传递给Form2,并在Form2上从Form1的对象调用此方法


yes you can. but the better approach is..
you create a public method inside Form1, call ButtonClick event from that method. pass your form object to Form2 at initialization of form2 and call this method from object of Form1 on Form2
i.e

partial class Form1
{
 Form1()
 {}
 
 protected Form1_Load(object,EventArgs)
 {
  Form2 frm = new Form2(this);
  frm2.Show();
 }

 protected void Button1_Click(object,EventArgs)
 { //Event Body
 }

 public void CallButton1Click()
 {
   Button1_Click(null,null)
 }
}

class Form2
{
 Form1 frm= null;
 Form2()
 {}
 Form2(Form argForm)
 {
   frm = (Form1) argForm
 }

 protected Form2_Load(object,EventArgs)
 {
   frm.CallButton1Click();
 }
}





它会为您工作





it will work for you


这篇关于我可以在form2按钮中调用form1 button1单击事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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