您的位置:首页 > 其它

WinForm窗体之间传递参数

2011-04-01 17:38 555 查看
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>();
}
}
}


主窗体代码:

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: