| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 10045 人关注过本帖, 3 人收藏
标题:[原创]两个窗体之间传递数据
只看楼主 加入收藏
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TransData
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NumericUpDown numericUpDown1;
public event TranDataEventHandler OnTranData;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public Form2(int Pam1,string Pam2)//重载的构造函数为Form1-->Form2传数据做准备
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.numericUpDown1.Value=Pam1;
this.textBox2.Text=Pam2;

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "回传";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(160, 128);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(120, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 64);
this.label1.Name = "label1";
this.label1.TabIndex = 3;
this.label1.Text = "数字参数";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 128);
this.label2.Name = "label2";
this.label2.TabIndex = 4;
this.label2.Text = "字符参数";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(160, 64);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.TabIndex = 5;
this.numericUpDown1.Value = new System.Decimal(new int[] {
10,
0,
0,
0});
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 317);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
TranDataEventArgs ea=new TranDataEventArgs((int)this.numericUpDown1.Value,this.textBox2.Text);//生成事件所携带的数据
if(OnTranData!=null)//进行一次判断,防止窗体1不使用事件时出现异常
{
OnTranData(this,ea);//使用事件
}
}
}
public delegate void TranDataEventHandler(object sender,TranDataEventArgs e);//事件处理函数委托
public class TranDataEventArgs:System.EventArgs//包含事件代理所需要的数据,必须从此类继承
{
int iData;
string sData;
public TranDataEventArgs(int Pam1,string Pam2)
{
this.iData=Pam1;
this.sData=Pam2;
}
public int IntData
{
get
{
return this.iData;
}
}
public string StrData
{
get
{
return this.sData;
}
}
}
}


2006-05-27 09:55
xxxxx52
Rank: 4
等 级:贵宾
威 望:13
帖 子:689
专家分:0
注 册:2006-4-30
收藏
得分:0 
请问这个numericUpDown1是什么呢?

好的资料下载网站http:///in.asp?id=xuelion2006 嘿嘿帮点一下拉~
2006-05-27 10:05
keikei
Rank: 2
等 级:新手上路
威 望:4
帖 子:152
专家分:0
注 册:2006-5-15
收藏
得分:0 

不是很明白似乎!

2006-05-27 20:31
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 
numericUpDown1是控件,只允许输入数字的.VS2003自带的.

2006-05-28 18:16
xxxxx52
Rank: 4
等 级:贵宾
威 望:13
帖 子:689
专家分:0
注 册:2006-4-30
收藏
得分:0 
Form1里面有个错误 不知道为什么
private void frm_OnTranData(object sender, TranDataEventArgs e)//TranDataEventArgs 找不到类型命名空间
{
//在事件处理函数中使用回传的数据
this.numericUpDown1.Value=e.IntData;
this.textBox1.Text=e.StrData;
}

好的资料下载网站http:///in.asp?id=xuelion2006 嘿嘿帮点一下拉~
2006-05-31 16:49
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 
public class TranDataEventArgs:System.EventArgs//包含事件代理所需要的数据,必须从此类继承
{
int iData;
string sData;
public TranDataEventArgs(int Pam1,string Pam2)
{
this.iData=Pam1;
this.sData=Pam2;
}
public int IntData
{
get
{
return this.iData;
}
}
public string StrData
{
get
{
return this.sData;
}
}
}
是在窗体2的代码页里定义的,只要跟主程序在同一个命名空间下就没问题.因为这个类是自定义的,如果不和主程序在同一个命名空间下或者引用它的程序没有带着完全限定的命名空间,就会出现你说的错误.

2006-05-31 21:17
楚地风云
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-6-9
收藏
得分:0 
委托传值 比较实用
2006-06-09 03:11
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 

嗯..还不错
学习啦!


msdn == 葵花宝典!!!
QQ:122768959
2007-10-19 16:21
梦心
Rank: 4
来 自:福建平和
等 级:贵宾
威 望:13
帖 子:1910
专家分:0
注 册:2007-5-11
收藏
得分:0 
要好好学习!

我清高和我骄傲的倔强,在风中大声的唱:我不听,我不听~~做我自己最特别,呼呼~~啦啦~~~
我的博客园地址: [url]http://[/url]
2007-10-19 18:34
JennyZhang
Rank: 1
来 自:广州
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-12-15
收藏
得分:0 
第二种方法只需要一个委托就可以了吧!
当然前面的构造函数方法更简单一点。

交流是一种分享,分享是一种快乐!
2007-12-16 17:32
快速回复:[原创]两个窗体之间传递数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013260 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved