在C#中调用另一个窗体上的按钮事件

3
我希望当我在form1上点击button1时,能够调用form2上的button2并执行button2事件下的代码。
以下代码无法实现:
button2.PerformClick();

我遇到的错误是“button2在当前上下文中不存在”,因此我尝试将修饰符设置为public,还将单击事件设置为public void...但没有成功。form2.button2.PerformClick();也不起作用。

为什么您要执行一次点击操作而不是调用button2点击事件呢?只需声明一个公共方法,该方法将调用button2_click事件,并在button1点击时调用此方法。 - Ulhas Tuscano
4个回答

4

您应该将想要调用的代码放入Form2上的一个公共方法中,然后从Form1调用该方法。

如果您需要从特定实例的Form2调用该方法,则可以将form2的Handle属性存储在某个地方,然后按以下方式获取适当的表单。

var myForm = Form.FromHandle(myForm2Handle);
myForm.MyPublicMethod();

您可以在Button1的点击事件中调用此函数。

这没什么意义。你得存储myForm2Handle, 不如存储MyForm. - Hans Passant
@hansPassant,当然你可以将整个表单存储在内存中,或者只有在需要时获取表单,这并没有什么区别。 - msarchet

2

如果你在表单之间进行操作并执行按钮点击事件代码,则存在架构问题。你应该为此设置一个事件系统。

我建议,在Form1上设置一个事件,Form2将会有一个监听器来接收这个事件:

public class Form2{
   public Form2{
       // get your form instance
       Form1 MyForm1Instance = new Form1();
       // hook up the event
       MyForm1Instance.SomeEvent += new EventHandler(MyHandler);
   }
   public void MyHandler(){
       // handle event here; run your button_click code or whatever
   }
}

当您点击适当的按钮时,Form1只需要触发“SomeEvent”即可。


方向错误,请再次检查问题。 - Hans Passant
好的,没问题 :) 交换了form1和form2。 - Jerod Venema

1

试一下,看看是否有效,对我有效。 在第一个表单中创建您的方法。

public void DoSomething(parameters)
{
  //code to handle those parameters
}

在调用或第二种形式中,如果您想从中调用form1,请使用此选项。
Form1 f1 = new Form1();
        try
        {
            f1.DoSomething(arguments);
        }
        catch (Exception)
        {
            catch the exceptions
        }
        f1.Show();

如果这对您有用,请评论并将其标记为答案。


0

我认为你的问题在于你试图在没有创建对象的情况下调用实例方法。以下是演示可能的方法调用的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new TestForm1());
        }
    }

    public partial class TestForm1 : Form
    {
        private System.Windows.Forms.Button button1;

        public void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello! Im TestForm1 and Im going to call TestForm2's code!");

            // You must create TestForm2 because of button1_Click is not a static method!!!
            TestForm2 form2 = new TestForm2();

            form2.button1_Click(this, new EventArgs());
        }

        public TestForm1()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.Controls.Add(this.button1);
            this.Name = "TestForm1";
            this.Text = "TestForm1";
            this.ResumeLayout(false);
        }
    }

    public partial class TestForm2 : Form
    {
        private System.Windows.Forms.Button button1;

        public void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello! Im TestForm2");
        }

        public TestForm2()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.Controls.Add(this.button1);
            this.Name = "TestForm2";
            this.Text = "TestForm2";
            this.ResumeLayout(false);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接