现在的位置: 首页 > 综合 > 正文

Visual C#中实现窗体间的数据传递之一

2013年07月22日 ⁄ 综合 ⁄ 共 5302字 ⁄ 字号 评论关闭

导读:
  一个稍微复杂一点的程序一般都有二个或者更多的窗体。有时在程序设计中,数据不仅要在同一个窗体中传递,还要在窗体间传递,这种传递是主窗体与从窗体之间数据的互相传递。从本文开始,我们将列举不同窗体间数据传递的四种情况,和用Visual C#实现这四种情况的具体方法。下面先介绍用Visual C#实现窗体间传递数据中第一种情况——从主窗体向从窗体传递字符串。在阅读完本文后,你还尝试一下利用此方法在窗体间传送数值等数据。
  本文中程序设计、调试、运行的软件环境:
  Windows2000 服务器版
  Visual Studio.Net正式版,.Net FrameWork SDK版本号3705
  实现步骤:
  1.启动Visual Studio .Net
  2.选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框
  3.将【项目类型】设置为【Visual C#项目】
  4.将【模板】设置为【控制台应用程序】
  5.在【名称】文本框中输入【VC#中不同窗体数据传递方法01】
  6.在【位置】的文本框中输入【E:/VS.NET项目】,然后单击【确定】按钮,这样VC#中不同窗体数据传递方法01项目就创建完成了
  7.把Visual Studio.Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到【Form1.cs(设计)】窗体中,并执行相应操作:
  · 二个TextBox组件,用以输入向Form2窗体传送的数据
  · 二个Label组件
  · 一个Button组件,名称为button1,并在拖入【Form1.cs(设计)】窗体后,双击它,则Visual Stuido .Net产生其Click事件对应的处理代码。
  8.把Visual Studio .Net的当前窗口切换到【Form1.cs】窗口,即:Form1.cs的代码编辑窗口。并用下列代码替换替代系统产生的InitializeComponent过程。
  private void InitializeComponent ( )
{
this.button1 = new System.Windows.Forms.Button ( ) ;
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.label1 = new System.Windows.Forms.Label ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.SuspendLayout ( ) ;
this.button1.Location = new System.Drawing.Point ( 103 , 149 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 75 , 36 ) ;
this.button1.TabIndex = 0 ;
this.button1.Text = "发送" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.textBox1.Location = new System.Drawing.Point ( 93 , 56 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 122 , 21 ) ;
this.textBox1.TabIndex = 1 ;
this.textBox1.Text = "";
this.textBox2.Location = new System.Drawing.Point ( 93 , 99 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 123 , 21 ) ;
this.textBox2.TabIndex = 2 ;
this.textBox2.Text = "";
this.label1.Location = new System.Drawing.Point ( 29 , 57 ) ;
this.label1.Name = "label1" ;
this.label1.TabIndex = 3 ;
this.label1.Text = "欢迎词:" ;
this.label2.Location = new System.Drawing.Point ( 16 , 100 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 4 ;
this.label2.Text = "提示信息:" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 263 , 223 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control[ ] {
this.textBox2 ,
this.textBox1 ,
this.button1 ,
this.label2 ,
this.label1 } ) ;
this.Name = "Form1" ;
this.Text = "Form1" ;
this.Load += new System.EventHandler ( this.Form1_Load ) ;
this.ResumeLayout ( false ) ;
}
  此时,VC#中不同窗体数据传递方法01项目的主窗体Form1的设计界面就完成了,具体如图1所示:
  
  
  图1:VC#中不同窗体数据传递方法01项目主窗体设计界面
  9.把Visual Studio.Net的当前窗口切换到Form1.cs的代码编辑窗口,并用下列代码替换Form1.cs中button1组件的Click事件对应的处理代码。
  private void button1_Click ( object sender , System.EventArgs e )
{
Form2 myForm = new Form2 ( textBox1.Text , textBox2.Text ) ;
myForm.Show ( ) ;
}
  10.选择菜单【项目】|【添加Windows窗体】后,弹出【添加新项-VC#中不同窗体数据传递方法01】对话框。在此对话框中的【名称(N):】文本框中输入【Form2】后,单击【打开】按钮,则在VC#中不同窗体数据传递方法01项目中添加了一个新的窗体,名称为【Form2】。
  11.把Visual Studio.Net的当前窗口切换到【Form2.cs(设计)】窗口,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到【Form2.cs(设计)】窗体中,并执行相应操作:
  · 二个TextBox组件,用以显示从Form1传送来的字符串数据
  · 二个Label组件
  · 一个Button组件,名称为button1,并在拖入【Form2.cs(设计)】窗体后,双击它,则Visual Stuido .Net产生其Click事件对应的处理代码。
  12.把Visual Studio.Net的当前窗口切换到Form2.cs的代码编辑窗口,并在定义Form2代码的后部添加下列代码,下列代码是定义二个字符串变量,用以接收从Form1传送来的字符串数据:
  private string str1 , str2 ;
  13.并用下列代码替换系统产生的Form2对应的代码:
  public Form2 ( string sendData01 , string sendData02 )
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent ( ) ;
str1 = sendData01 ;
str2 = sendData02 ;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
  14.用下列代码替换Form2.cs中的由Visual Studio .Net系统产生的InitializeComponent过程:
  private void InitializeComponent ( )
{
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.button1 = new System.Windows.Forms.Button ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.label1 = new System.Windows.Forms.Label ( ) ;
this.SuspendLayout ( ) ;
this.textBox2.Location = new System.Drawing.Point ( 109 , 93 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 123 , 21 ) ;
this.textBox2.TabIndex = 7 ;
this.textBox2.Text = "";
this.textBox1.Location = new System.Drawing.Point ( 109 , 50 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 122 , 21 ) ;
this.textBox1.TabIndex = 6 ;
this.textBox1.Text = "";
this.button1.Location = new System.Drawing.Point ( 75 , 143 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 119 , 43 ) ;
this.button1.TabIndex = 5 ;
this.button1.Text = "显示传送来数据" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label2.Location = new System.Drawing.Point ( 32 , 94 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 9 ;
this.label2.Text = "提示信息:" ;
this.label1.Location = new System.Drawing.Point ( 45 , 51 ) ;
this.label1.Name = "label1" ;
this.label1.TabIndex = 8 ;
this.label1.Text = "欢迎词:" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 265 , 228 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control[ ] {
this.textBox2 ,
this.textBox1 ,
this.button1 ,
this.label2 ,
this.label1 } ) ;
this.Name = "Form2" ;
this.Text = "Form2" ;
this.ResumeLayout ( false ) ;
}
  15.用下列代码替换Form2.cs中button1组件Click事件对应的处理代码,下列代码的作用是把从Form1窗体中接收来的字符串数据通过同样的方式显示出来:
  private void button1_Click ( object sender , System.EventArgs e )
{
textBox1.Text = str1 ;
textBox2.Text = str2 ;
}
  16.至此,在上述步骤都正确完成,并全部保存后,VC#中不同窗体数据传递方法01项目的全部工作就完成了。图02是VC#中不同窗体数据传递方法01程序的运行界面。其中Form2中显示出来的字符串就是从主窗体中传递过去的。
  
  
  图02:VC#中不同窗体数据传递方法01程序的运行界面
  总结
  用Visual C#实现窗体间数据传递的第一种情况的全部工作就完成了,细心的读者已经发现,这种方法明显存在一个主要缺点,就是主窗体向从窗体传递数据的数目是固定的。这个缺点将在下一篇文章中彻底加以解决。

本文转自
http://developer.ccidnet.com/art/322/20021220/34309_1.html

抱歉!评论已关闭.