C# 窗体1与窗体2之间实例参数的传递

先有两个类于手参数传递

 窗体1

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void ReceiveParameterFromAnotherForm(me parameter)
        {

            MessageBox.Show(parameter.bb);

        }
        Dbc dbc= new Dbc();
        private void button1_Click(object sender, EventArgs e)
        {
            dbc.aa = "来自窗体1";
            Form2 fr= new Form2(dbc);
            fr.OnFormClosedWithAnotherClass += Fr_OnFormClosedWithAnotherClass;
            fr.ShowDialog();
        }

        private void Fr_OnFormClosedWithAnotherClass(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }

//用于传递的参数
    public class Dbc
    {
        public string aa;

    }
    public class me
    {
        public string bb;
    }

窗体2

 public partial class Form2 : Form
    {
        public event EventHandler OnFormClosedWithAnotherClass;
        public Form2()
        {
            InitializeComponent();
        }
        me messsage = new me();

        public Form2(Dbc dbc)
        {
            InitializeComponent();
            messsage.bb = dbc.aa+ "+进入窗体2";

        }
        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form1 mainWindow = (Form1)Application.OpenForms["Form1"];
            if (mainWindow != null)
            {
                mainWindow.ReceiveParameterFromAnotherForm(messsage);
            }
        }
    }

窗体1将dbc参数传递给窗体2,窗体2执行完成,关闭后在窗体1上会调用窗体2关闭的事件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值