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

Visual C#实现窗体间数据传递之二 (2)

2013年11月19日 ⁄ 综合 ⁄ 共 3203字 ⁄ 字号 评论关闭
导读:
  7.选择菜单【项目】|【添加Windows窗体】后,弹出【添加新项-VC#中不同窗体数据传递方法01】对话框。在此对话框中的【名称(N):】文本框中输入【Form2】后,单击【打开】按钮,则在VC#中不同窗体数据传递方法01项目中添加了一个新的窗体,名称为【Form2】。
  8.把Visual Studio .Net的当前窗口切换到【Form2.cs(设计)】窗口,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到【Form2.cs(设计)】窗体中,并执行相应操作:
  · 二个TextBox组件,用以显示向主窗体请求获得的数据。
  · 二个Label组件。
  · 一个Button组件,名称为button1。
  9.把Visual Studio .Net的当前窗口切换到【Form2.cs】窗口,即:Form2.cs的代码编辑窗口。并用下列代码替换替代系统产生的InitializeComponent过程。
  {
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.label1 = new System.Windows.Forms.Label ( ) ;
this.button1 = new System.Windows.Forms.Button ( ) ;
this.label3 = new System.Windows.Forms.Label ( ) ;
this.SuspendLayout ( ) ;
this.textBox1.Location = new System.Drawing.Point ( 95 , 42 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 125 , 21 ) ;
this.textBox1.TabIndex = 2 ;
this.textBox1.Text = "";
this.textBox2.Location = new System.Drawing.Point ( 94 , 80 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 127 , 21 ) ;
this.textBox2.TabIndex = 3 ;
this.textBox2.Text = "";
this.label2.Location = new System.Drawing.Point ( 27 , 83 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 5 ;
this.label2.Text = "提示信息:" ;
this.label1.Location = new System.Drawing.Point ( 38 , 45 ) ;
this.label1.Name = "label1" ;
this.label1.TabIndex = 4 ;
this.label1.Text = "欢迎词:" ;
this.button1.Location = new System.Drawing.Point ( 80 , 136 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 135 , 53 ) ;
this.button1.TabIndex = 6 ;
this.button1.Text = "从Form1中获取数据" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label3.Location = new System.Drawing.Point ( 102 , 210 ) ;
this.label3.Name = "label3" ;
this.label3.TabIndex = 7 ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control[] {
this.label3 ,
this.button1 ,
this.textBox2 ,
this.textBox1 ,
this.label2 ,
this.label1 } ) ;
this.MaximizeBox = false ;
this.MinimizeBox = false ;
this.Name = "Form2" ;
this.Text = "Form2" ;
this.ResumeLayout ( false ) ;
}
  10.由于主窗体是把其中的跟踪条的数值通过从窗体中的label组件来显示的,所以必须把Form2.cs文件中创建label3组件时定义的"private"类型修改为"public"类型,修改后的创建label3组件的代码为:
  public System.Windows.Forms.Label label3 ;
  由于Form2类的实例是通过Form1类的实例来初始化,所以在创建label3组件后面添加下列代码,下列代码是创建一个Form1类的实例,其作用是初始化Form2类的实例(即从窗体):
  private Form1 mF_Form ;
  11.修改Form2类的构造函数,具体操作如下,用下列代码替换Form2.cs中缺省的构造函数:
  public Form2 ( Form1 myForm )
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent ( ) ;
this.mF_Form = myForm ;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
  12.在Form2.cs中的Main函数后,添加下列代码,下列代码的功能是实现向主窗体提出数据请求,并获取主窗体的数据,当然这些数据的类型必须修改为"public"类型。
  private void button1_Click ( object sender , System.EventArgs e )
{
textBox1.Text = this.mF_Form.textBox1.Text ;
textBox2.Text = this.mF_Form.textBox2.Text ;
}
  13.至此,在上述步骤都成功完成,并全部保存后,Visual C#实现窗体间传递数据第二种情况的处理方法就全部完成了。此时单击快捷键【F5】就可以运行程序了,当调整Form1窗体的跟踪条数值,我们会发现Form2窗体中的label3组件显示的数值也随之变化;当在Form1窗体的二个TextBox组件中输入数据后,单击Form2窗体中的【从Form2中获取数据】按钮,则程序能够成功的从Form1中获取数据,并通过Form2中对应的TextBox组件显示出来。图01就是程序运行后的界面:
  
  
  图01:【VC#中不同窗体数据传递方法02】程序的运行界面
  小结
  本节介绍的这种实现窗体间数据传送的方法要比第一篇介绍的方法要难许多,其难点就在于如何修改构造函数。对于那些要从其他窗体中要访问的数据,必须设定修改为"public",否则无法实现窗体间数据传递。
  这二篇文章介绍的都是主窗体向从窗体传递数据,在下一篇文章中,将介绍从窗体向主窗体传递数据的实现方法。

本文转自
http://developer.ccidnet.com/art/322/20021224/34599_2.html

抱歉!评论已关闭.