Form1代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 窗体间传递数值
{
public partial class Form1 : Form
{
//
public string text = null;
//
public Form1()
{
InitializeComponent();
}
void form2_MyEvent(string text)
{
textBox1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
text = textBox1.Text;
Form2 form2 = new Form2(text);
form2.MyEvent += new MyDelegate(form2_MyEvent);
form2.Show();
}
}
}
Form2代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 窗体间传递数值
{
public delegate void MyDelegate(string text);
public partial class Form2 : Form
{
public event MyDelegate MyEvent;
public Form2(string text)
{
InitializeComponent();
this.textBox1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
MyEvent(this.textBox1.Text);
}
}
}