子窗体(FromA)改变父窗体(MainForm)中控件的属性(利用委托)
代码(MainForm.cs):
namespace TestSelect { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { FormA fm = new FormA(); fm.change+=new FormA.ChangeMainForm(ChangeLable); fm.ShowDialog(); } private void ChangeLable(string str) { label1.Text = str; } } }
代码(FormA.cs):
namespace TestSelect { public partial class FormA : Form { public FormA() { InitializeComponent(); } public delegate void ChangeMainForm(string str); public event ChangeMainForm change; private void button1_Click(object sender, EventArgs e) { change(textBox1.Text); } } }
其实简单的改变父窗体不需要用到这个东西,不过个人感觉还是挺好的!(例:窗体关系A-B-C,需要C change A)