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();
            }
        }
    }
    
     
  • 相关阅读:
    [Oracle]跨越 DBLINK 访问表时,数据缓存在何处的Data Buffer 中?
    [Oracle]查看数据是否被移入 DataBuffer 的方法
    [Oracle]如何为数据库设置Event(eg: ORA-00235)
    [Oracle][Corruption]究竟哪些检查影响到 V$DATABASE_BLOCK_CORRUPTION
    [Oracle]OpenVMS 运行 Oracle 时的推荐值
    [Oracle]System 表空间的文件丢失
    [Oracle]如果误删了某个数据文件,又没有被备份,能否恢复?
    OFS环境,删除Resource 时出现错误失败,应该如何继续
    基于酷Q的工作秘书机器人
    C++编写简单的俄罗斯方块游戏
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2002431.html
Copyright © 2011-2022 走看看