| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2024 人关注过本帖
标题:[求助]请问窗体间怎么传递参数
只看楼主 加入收藏
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
结帖率:33.33%
收藏
 问题点数:0 回复次数:15 
[求助]请问窗体间怎么传递参数
如提。
我想运行Form1结束时,传递一个值或参数给Form2。
搜索更多相关主题的帖子: 参数 窗体 结束 运行 
2006-08-10 12:07
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

两个窗体之间传递数据(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]

这个是前几天在网上看到的
不过我还没来得及仔细看
正好拿过来
你试试吧
看看能不能实现


此号自封于2006年11月30日
2006-08-10 12:11
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
万分感谢!我试试!

2006-08-10 12:26
swc
Rank: 3Rank: 3
等 级:论坛游民
威 望:6
帖 子:394
专家分:83
注 册:2006-4-7
收藏
得分:0 

小声地问,这代码怎么才能运行?


实践、学习、再实践、再学习......
2006-08-10 15:13
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

目前情况是代码可以运行
但是不知道怎么样传递参数
丈二和尚一个


此号自封于2006年11月30日
2006-08-10 16:24
炫舞鱼
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-5-16
收藏
得分:0 
先在下一个窗口定义一个  字符串
在你想传值的界面进行附值就可以了
例如 接受参数窗口 这窗口名字就 mm.vb
private aa as string;
传值窗口
mm demo=new mm();
mm.aa="你要附值的内容"
2006-08-11 08:31
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
Form2构造函数:

private int a=0;
public Form2(int a)
{
this.a=a;
}

Form1传参:

Form2 f=new Form2(15);
f.Show();

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-08-11 11:36
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

楼上的我已经用了
好用

Form1里我写的代码

[CODE]
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{

}
private int a;
private void button1_Click_1(object sender, System.EventArgs e)
{
a=Convert.ToInt32 (this.textBox1 .Text) ;
Window.Form2 x=new Form2 ();
x.Show ();
x.For (a);
}[/CODE]

FORM2里写的代码

[CODE]private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private int a;
public void For(int a)
{
this.a=a;
}
private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1 .Text =this.a.ToString ();

}[/CODE]

先在1里面写几个数字 点按牛然后打开2的窗口 点2的按牛 就可以显示1的值了


此号自封于2006年11月30日
2006-08-11 12:25
swc
Rank: 3Rank: 3
等 级:论坛游民
威 望:6
帖 子:394
专家分:83
注 册:2006-4-7
收藏
得分:0 

楼上的解决的是往后(即向创建的窗口)传值的问题.可是往前(即创建的窗口返回一个值)传值该怎么实现呢?


实践、学习、再实践、再学习......
2006-08-11 13:30
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
以下是引用swc在2006-8-11 13:30:41的发言:

楼上的解决的是往后(即向创建的窗口)传值的问题.可是往前(即创建的窗口返回一个值)传值该怎么实现呢?


此号自封于2006年11月30日
2006-08-11 14:05
快速回复:[求助]请问窗体间怎么传递参数
数据加载中...
 
   



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

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