请教窗体传值问题!!!
c#中如何从一个窗体向另一个窗体传值??初学者 说的越具体越好!!!作为一个例子:
程序代码:
using System; using System.Windows.Forms; namespace Test { class Program { class MyEventArgs : EventArgs { public string Message {get;set;} } static event EventHandler<MyEventArgs> form1SentMessage; static event EventHandler<MyEventArgs> form2SentMessage; class MyForm1 : Form { public MyForm1() { Button button = new Button { Text = "Button1" }; button.Click += (s, a) => { form1SentMessage(null, new MyEventArgs { Message = "Hello from form1!" }); }; Controls.Add(button); form2SentMessage += (s, a) => { MessageBox.Show("Hey, the form2 has sent a message : '" + a.Message + "'", "form1", MessageBoxButtons.OK); }; } } class MyForm2 : Form { public MyForm2() { Button button = new Button { Text = "Button2" }; button.Click += (s, a) => { form2SentMessage(null, new MyEventArgs { Message = "Hello from form2!" }); }; Controls.Add(button); form1SentMessage += (s, a) => { MessageBox.Show("Hey, the form1 has sent a message : '" + a.Message + "'", "form2", MessageBoxButtons.OK); }; } } class MainForm : Form { public MainForm() { MyForm1 form1 = new MyForm1(); form1.Text = "Form1"; form1.Show(); MyForm2 form2 = new MyForm2(); form2.Text = "Form2"; form2.Show(); } } [STAThread] static void Main(string[] args) { Application.Run(new MainForm()); } } }