使用属性将数据传递给 Form2,使用事件返回保存的数据:
表格1
private void button1_Click(object sender, EventArgs e)
{
Form2 edit = new Form2();
edit.SaveEvent += new Form2.SaveEventHandler(edit_SaveEvent); //Add event handler
edit.name = People[this.listView1.SelectedItems[0].Index].Name;
edit.age = People[this.listView1.SelectedItems[0].Index].Age;
edit.ShowDialog(this);
}
void edit_SaveEvent(object sender, SaveEventArgs e)
{
//Do Your work here with e.newAge and e.newName
((Form2)sender).Close(); //Close Form2
}
表格2
public partial class Form2 : Form
{
public delegate void SaveEventHandler(object sender, SaveEventArgs e);
public event SaveEventHandler SaveEvent;
public string name { get; set; }
public string age { get; set; }
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = name;
textBox2.Text = age;
}
private void button1_Click(object sender, EventArgs e)
{
SaveEvent(this, new SaveEventArgs(textBox1.Text, textBox2.Text));
}
}
public class SaveEventArgs
{
public SaveEventArgs(string name, string age) {newName = name; newAge = age; }
public String newName {get; private set;} // readonly
public String newAge {get; private set;}
}