WinForm窗体之间传递参数

1.       UI设计
    主窗体FrmMain:
        两个文本框:textBox1、textBox2       存储FrmMain中的参数
        一个按钮:button1                    启动子窗体
    子窗体FrmChild:
两个文本框:textBox1、textBox2 存储FrmChild中的参数
        一个按钮:button1                    关闭子窗体,返回FrmMain
  2.      DeliveryParamsArgs类:存储从子窗体FrmChild返回到父窗体FrmMain中的参数
      
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DeliveryParam
{
    public class DeliveryParamsArgs : EventArgs
{
        //存储参数的各种数据结构,可以自由扩展
        private ArrayList paramsList = null;
        private IDictionary<String, Object> paramsDictionary = null;

        public ArrayList ParamsList
        {
            get { return paramsList; }
            set { paramsList = value; }
        }

        public IDictionary<String, Object> ParamsDictionary
        {
            get { return paramsDictionary; }
            set { paramsDictionary = value; }
        }

        public DeliveryParamsArgs()
        {
            paramsList = new ArrayList();
            paramsDictionary = new Dictionary<String, Object>();
        }
    }
}
 

3.      主窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DeliveryParam
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private string m_parm1 = null;
        private string m_parm2 = null;

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

        private void button1_Click(object sender, EventArgs e)
        {
            this.m_parm1 = this.textBox1.Text.ToString();
            this.m_parm2 = this.textBox2.Text.ToString();

            FrmChild child = new FrmChild();
           
            child.DeliveryList = new System.Collections.ArrayList();
            //将FrmMain的参数传递给FrmChild
            child.DeliveryList.Add(this.m_parm1);
            child.DeliveryList.Add(this.m_parm2);

            child.DeliveryParamsEvent += new FrmChild.DeliveryParamsHandler(child_DeliveryParamsEvent);//绑定事件
            child.Show();
        }

        void child_DeliveryParamsEvent(DeliveryParamsArgs e)
        {
            this.textBox1.Text = e.ParamsList[0].ToString();
            this.textBox2.Text = e.ParamsList[1].ToString();
        }
    }
}
 

4.      子窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace DeliveryParam
{
    public partial class FrmChild : Form
    {
        public FrmChild()
        {
            InitializeComponent();
           
        }

        //窗体FrmMain中传递过了的参数集合
        private ArrayList deliveryList = null;

        public ArrayList DeliveryList
        {
            get { return deliveryList; }
            set { deliveryList = value; }
        }

        //窗体Frmchild中需要传递给FrmMain的参数
        private string c_parm1 = null;
        private string c_parm2 = null;

        private void FrmChild_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = deliveryList[0].ToString();
            this.textBox2.Text = deliveryList[1].ToString();
        }

        public delegate void DeliveryParamsHandler(DeliveryParamsArgs e);//定义委托  
        public event DeliveryParamsHandler DeliveryParamsEvent;//定义事件

        private void button1_Click(object sender, EventArgs e)
        {
            DeliveryParamsArgs arrayParams = new DeliveryParamsArgs();
            this.c_parm1 = this.textBox1.Text.ToString();
            this.c_parm2 = this.textBox2.Text.ToString();

            arrayParams.ParamsList.Add(this.c_parm1);
            arrayParams.ParamsList.Add(this.c_parm2);

            arrayParams.ParamsDictionary.Add("c_parm1", this.c_parm1);
            arrayParams.ParamsDictionary.Add("c_parm2", this.c_parm2);

            //激发事件,写在你想要的地方  
            if (DeliveryParamsEvent != null)
            {
                DeliveryParamsEvent(arrayParams);
            }
            this.Close();
            this.Dispose();
        }
    }
}

WinForm窗体间参数传递Code下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值