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

楼主的方法好像比较复杂
我在机子上试了半个小时也没出来

现在的方法比较简单
是网上学来的
并非和楼主一样的原创
我贴出来和大家一起分享

//在Form1 中
private void button1_Click ( object sender , System.EventArgs e )
{
Form2 myForm = new Form2 ( textBox1.Text , textBox2.Text ) ;
myForm.Show ( ) ;
}


//在Form2 中
public string str1, str2; //定义两个变量

public Form2(string sendData01, string sendData02) // 把“string sendData01, string sendData02” 添加到括号中去
{

InitializeComponent();
str1 = sendData01; //增加这两句
str2 = sendData02;
}


str1 和 str2 就可以直接使用了。


2006-05-21 14:50
googleknow
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2006-5-19
收藏
得分:0 

要不断的学习!

2006-05-21 18:40
凌枫影
Rank: 2
等 级:新手上路
威 望:3
帖 子:163
专家分:0
注 册:2006-5-10
收藏
得分:0 

楼上的兄弟发贴好积极哦

要不断的学习


2006-05-21 18:52
keikei
Rank: 2
等 级:新手上路
威 望:4
帖 子:152
专家分:0
注 册:2006-5-15
收藏
得分:0 
我试试,验正一下《
2006-05-22 09:57
keikei
Rank: 2
等 级:新手上路
威 望:4
帖 子:152
专家分:0
注 册:2006-5-15
收藏
得分:0 
21楼的方法可行,简单易学!
2006-05-22 10:38
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 

我今天准备下,明天贴出示例代码.
还有就是21楼的兄弟,你用的方法只适合引用类型,如果是值类型就不好使了.


2006-05-22 12:29
keikei
Rank: 2
等 级:新手上路
威 望:4
帖 子:152
专家分:0
注 册:2006-5-15
收藏
得分:0 
等待!!恩----实在看不懂楼主第二种方法,而且为传一值我费尽脑汁也传不过去,恩--等待!!!
2006-05-22 12:49
凌枫影
Rank: 2
等 级:新手上路
威 望:3
帖 子:163
专家分:0
注 册:2006-5-10
收藏
得分:0 
同样期待中!!!

2006-05-22 19:50
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 
真的很对不起,手上工作太多了,没有按照约定准时上传,在此深表歉意!
这次我上传的项目功能不大,只是为了阐明我之前所说的观点,如果还有疑问欢迎继续跟贴.

2006-05-27 09:50
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 

怎么没上传上来呢,那就把代码贴上来吧.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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

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

//
// 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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 56);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "数据参数";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 88);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "字符参数";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(152, 56);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.TabIndex = 2;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(152, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 160);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "打开新窗口";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 309);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2((int)this.numericUpDown1.Value,this.textBox1.Text);//使用已经重载的窗体构造函数
frm.OnTranData+=new TranDataEventHandler(frm_OnTranData);//使用事件
frm.Show();
}

private void frm_OnTranData(object sender, TranDataEventArgs e)
{
//在事件处理函数中使用回传的数据
this.numericUpDown1.Value=e.IntData;
this.textBox1.Text=e.StrData;
}
}
}


2006-05-27 09:55
快速回复:[原创]两个窗体之间传递数据
数据加载中...
 
   



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

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