WinForm--(子窗体改变父窗体中控件的属性-委托)

子窗体(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)

相关文章
相关标签/搜索