我想运行Form1结束时,传递一个值或参数给Form2。
两个窗体之间传递数据(ZT)
[CODE]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;
}
}
}
}[/CODE]
这个是前几天在网上看到的
不过我还没来得及仔细看
正好拿过来
你试试吧
看看能不能实现