[英]c# call button event that is on another winform
我想當我單擊form1上的button1來調用form2上的button2並執行button2事件下的代碼。
以下代碼不起作用:
button2.PerformClick();
我得到的錯誤是“ button2在當前上下文中不存在”,因此我嘗試將修飾符設置為public,同時單擊event以在public void上進行設置……不走運。 form2.button2.PerformClick();
也行不通。
您應該將要調用的代碼放入Form2上的public
方法中,然后從form1調用該方法。
如果您需要form2的特定實例來調用該方法,則可以將form2
的Handle
屬性存儲在某個位置,然后按以下方式獲取適當的表單。
var myForm = Form.FromHandle(myForm2Handle);
myForm.MyPublicMethod();
然后,您可以從Button1單擊事件中調用它。
如果要在表單之間執行並執行按鈕單擊事件代碼,則會遇到體系結構問題。 您確實應該為此配備一個事件系統。
我建議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”。
試試這個,看看它是否對我有用。 在第一種形式中創建您的方法
public void DoSomething(parameters)
{
//code to handle those parameters
}
在調用形式或第二種形式中,如果要從
Form1 f1 = new Form1();
try
{
f1.DoSomething(arguments);
}
catch (Exception)
{
catch the exceptions
}
f1.Show();
評論它是否適用於您並將其標記為答案。
我認為您的問題是您嘗試在不創建對象的情況下調用實例方法。 這是示例代碼,向您演示方法調用的可能方式:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.