2011-02-24 48 views
2

我希望當我在Button1的單擊Form1上調用BUTTON2是在窗口2和執行是BUTTON2事件下的代碼。C#呼叫按鈕事件是在另一個WinForm的

下面的代碼將無法正常工作:

button2.PerformClick(); 

錯誤我得到的是「BUTTON2不會在目前情況下存在」,所以我試圖修改器設置爲公開,還可以單擊事件對公衆設置無效...沒有運氣。 form2.button2.PerformClick();也不起作用。

+0

爲什麼你要執行一個點擊操作,而不是調用button2點擊事件。只需聲明一個公共方法,它將調用button2_click事件並在button1上單擊調用此方法。 – Tuscan 2011-02-24 16:30:31

回答

4

你應該把你要調用到Form2上一個public方法,然後調用該方法從Form1的代碼。

如果您需要某個特定的form2實例來調用該方法,則可以將HandleHandle屬性存儲在某個地方,然後按如下所示獲取適當的表單。

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

然後,您可以通過Button1單擊事件調用此方法。

+0

它沒有什麼意義。你必須存儲myForm2Handle,你不妨存儲MyForm。 – 2011-02-24 16:36:10

+0

@hansPassant,當然你可以將整個表單存儲在內存中,或者你可以在需要時獲取表單,但這並沒有真正改變。 – msarchet 2011-02-24 16:54:22

+0

這只是一個參考。 – 2011-02-24 17:09:33

2

,如果你的形式和執行按鈕單擊事件代碼之間達到你有一個結構問題。你應該有一個適合這個的事件系統。

我建議窗體2將有一個監聽器設置了一個事件在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」當您單擊相應的按鈕。

+0

錯誤的方法,再次檢查問題。 – 2011-02-24 16:16:38

+0

確定,足夠公平:)交換了form1和form2。 – jvenema 2011-02-24 18:28:47

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

試試這個,看看它是否有效,它適用於我。 第一形式要從

Form1 f1 = new Form1(); 
     try 
     { 
      f1.DoSomething(arguments); 
     } 
     catch (Exception) 
     { 
      catch the exceptions 
     } 
     f1.Show(); 

評論稱Form1的事件創建方法

public void DoSomething(parameters) 
{ 
    //code to handle those parameters 
} 
在調用或第二種形式使用

這一點,如果它適用於u和其標記爲回答。