zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
     
  • 相关阅读:
    中国百年校服史:青春飞扬的几代人[转]
    关于五笔和拼音输入法的最本质区别
    Windows Mobile device 开发详解..
    生成目录树
    安装CE 6.0和VS2005出现的两个问题解决
    在c与c++下struct的区别,已经在c++下struct与class的区别。
    一招克死所有病毒!上网不用防火墙.不看后悔哟
    VC解析XML文件
    pythonday1笔记
    an error occurred while completing process java.lang.reflect.InvocationTargetEx
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2002431.html
Copyright © 2011-2022 走看看