我希望当我点击form1上的button1来调用form2上的button2并执行button2事件下的代码时。
以下代码不起作用:
button2.PerformClick();
我得到的错误是“button2在当前上下文中不存在”,所以我尝试将修饰符设置为public,同时单击事件以设置公共void ...没有运气。
form2.button2.PerformClick();
也不起作用。
答案 0 :(得分:4)
您应该将要调用的代码放在Form2上的public
方法中,然后从form1调用该方法。
如果您需要form2的特定实例来调用该方法,那么您可以在某处保存Handle
的{{1}}属性,然后获取相应的表单。
form2
然后,您可以从Button1点击事件中调用它。
答案 1 :(得分: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”。
答案 2 :(得分: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();
评论是否适用于你并将其标记为答案。
答案 3 :(得分: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);
}
}
}