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>();
        }
    }
}
  1. 主窗体代码:
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();
        }
    }
}
 
原文地址:https://www.cnblogs.com/xiaofengfeng/p/2002431.html