【C#学习笔记】使用委托与事件实现子窗体与父窗体的传值与交互————(2020.8.19学习笔记)

目录

1.思路
2.实例
3.实现效果

思路

在子窗体类中声明委托与事件,然后在父窗体给子窗体的事件绑定方法,最后在子窗体中触发事件并传值给事件,这样在父窗体被绑定的方法就会被执行,子窗体的值也能被父窗体接收

实例

Child_Dialog.cs(子窗口)

public partial class Child_Dialog : Form
    {
        public delegate void TextChange(string Text,Color color);
      
        public event TextChange TextChange_Event;
        

        public Child_Dialog()
        {
            InitializeComponent();
            
        }

        private void Tigger_Click(object sender, EventArgs e)
        {
            TextChange_Event("已触发",Color.Red);
            this.Close();
        }
    }

Father_Form.cs(父窗口)

public partial class Father_Form : Form
    {

        public Father_Form()
        {
            InitializeComponent();
        }

        private void Btn_Run_Click(object sender, EventArgs e)
        {
            Child_Dialog child_Dialog = new Child_Dialog();
            child_Dialog.TextChange_Event += Text_Change;
            child_Dialog.Show();
        }

     
        private void Text_Change(string text,Color color)
        {
            label1.Text = text;
            label1.BackColor = color;
        }
        
    }
实现效果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值