【问题标题】:c# call button event that is on another winformc#调用另一个winform上的按钮事件
【发布时间】:2011-02-24 15:17:42
【问题描述】:

我希望当我点击 form1 上的 button1 时调用 form2 上的 button2 并执行 button2 事件下的代码。

以下代码不起作用:

button2.PerformClick();

我得到的错误是“button2 在当前上下文中不存在”,所以我尝试将修饰符设置为公共,也单击事件设置为公共无效...没有运气。 form2.button2.PerformClick(); 也不行。

【问题讨论】:

  • 为什么要执行点击操作而不是调用button2点击事件。只需声明一个公共方法,该方法将调用 button2_click 事件并在 button1 点击时调用此方法。

标签: c#


【解决方案1】:

您应该将要调用的代码放入 Form2 上的 public 方法中,然后从 form1 中调用该方法。

如果您需要 form2 的特定实例来调用该方法,则可以将 form2 中的 Handle 属性存储在某处,然后获取适当的表单,如下所示。

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

然后您可以从 Button1 点击事件中调用它。

【讨论】:

  • 这没什么意义。您必须存储 myForm2Handle,您不妨存储 MyForm。
  • @hansPassant,确保您可以将整个表单存储在内存中,或者您可以在需要时获取表单,这并没有什么区别。
【解决方案2】:

如果您在表单和执行按钮单击事件代码之间进行操作,则会遇到架构问题。你真的应该为此建立一个事件系统。

我建议 Form2 为 Form1 上的事件设置一个监听器:

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”。

【讨论】:

  • 搞错了,再次检查问题。
  • 好的,很公平:)交换了form1和form2。
【解决方案3】:

试试这个,看看它是否有效,它对我有用。 在第一种形式中创建您的方法

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();

评论是否对你有用并将其标记为答案。

【讨论】:

    【解决方案4】:

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

    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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      相关资源
      最近更新 更多