您的位置:首页 > 其它

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

2008-01-31 19:41 274 查看
导读:   在上一篇文章中我们曾经指出指出,窗体间数据传递第一种情况的解决方法存在一个主要的缺点,就是窗体间传递的参数数目是固定的,并且类型也是固定的。这是因为,上文中修改了从命名空间System.Windows.Forms中的Form类派生而得到的Form2类的构造函数,由于构造函数中的参数和类型都是固定的,而主窗体向从窗体传递数据,就是通过构造函数中的参数来实现的,所以就造成了上面的那个缺点。其实在这种方法中还存在一个缺点,就是每一次窗体间的数据传递,就必须构建一个窗体,并且这种数据传递是一次性的。这些缺点对于窗体间传递少量数据,一般不会有太大影响,但如果要传递大量数据,并且要通过主窗体来实时向从窗体传递数据,使用这种方法就勉为其难了。   下面介绍另外一种从主窗体向从窗体传递数据的实现方法,这种方法能够完全解决上面的二个缺点,程序在主窗体中就像操作窗体中加入的组件一样,灵活的操作从窗体。   

设计思路   此方法实现二个功能:   

其一,主窗体能够实时地向从窗体传送数据,表现为当更改主窗体中的跟踪条(TrackBar)的数值,从窗体中定义的一个Label组件就显示出跟踪条的当前数值;   

其二,从窗体能够向主窗体提出数据请求,并且能够获取主窗体中各组件显示的数据。程序表现为,当单击从窗体中的【从Form1中获取数据】按钮,程序能够把主窗体中的二个TextBox组件显示的内容传递到从窗体,并且通过从窗体中的二个TextBox组件分别显示出来。   第一个功能的实现思路是把从窗体看成是主窗体的一个实例,加入到从窗体中的组件,可以看出是从窗体中定义的一个个变量,由于从窗体中加入的组件的组件缺省定义类型是Private(私有的),所以要想访问这些组件,必须改变为Public(共有的);而第二个功能的实现思路是通过修改Form2的构造函数,构造函数实现功能是通过Form1类的实例(即为主窗体)来创建并初始化Form2类的实例(即为从窗体)。这样对于从窗体来说,主窗体则为其一个实例,从而也就可以向主窗体提出数据请求,当然要把需要访问的各组件定义类型从缺省的Private(私有的)类型修改为Public(共有的)。上述二个功能的实现方法,第二种方法比较复杂,希望各位能够结合后面的具体实现代码来理解。   

第二种窗体间的数据传递情况实现步骤   

1.首先创建一个Visual C#的项目文件,项目名称为【VC#中不同窗体数据传递方法02】。   

2.把Visual Studio .Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到【Form1.cs(设计)】窗体中,并执行相应操作:   

· 二个TextBox组件,用以输入向Form2窗体传送的数据   

· 二个Label组件   

· 一个TrackBar组件,名称为trackBar1。   

3.把Visual Studio .Net的当前窗口切换到【Form1.cs】窗口,即:Form1.cs的代码编辑窗口。并用下列代码替换替代系统产生的InitializeComponent过程。   

private void InitializeComponent ( )

{

this.label1 = new System.Windows.Forms.Label ( ) ;

this.label2 = new System.Windows.Forms.Label ( ) ;

this.textBox1 = new System.Windows.Forms.TextBox ( ) ;

this.textBox2 = new System.Windows.Forms.TextBox ( ) ;

this.trackBar1 = new System.Windows.Forms.TrackBar ( ) ;

( ( System.ComponentModel.ISupportInitialize ) ( this.trackBar1 ) ).BeginInit ( ) ;

this.SuspendLayout ( ) ;

this.label1.Location = new System.Drawing.Point ( 27 , 41 ) ;

this.label1.Name = "label1" ;

this.label1.TabIndex = 0 ;

this.label1.Text = "欢迎词:" ;

this.label2.Location = new System.Drawing.Point ( 27 , 83 ) ;

this.label2.Name = "label2" ;

this.label2.TabIndex = 1 ;

this.label2.Text = "提示信息:" ;

this.textBox1.Location = new System.Drawing.Point ( 108 , 38 ) ;

this.textBox1.Name = "textBox1" ;

this.textBox1.TabIndex = 2 ;

this.textBox1.Text = "";

this.textBox2.Location = new System.Drawing.Point ( 109 , 78 ) ;

this.textBox2.Name = "textBox2" ;

this.textBox2.TabIndex = 3 ;

this.textBox2.Text = "";

this.trackBar1.LargeChange = 1 ;

this.trackBar1.Location = new System.Drawing.Point ( 12 , 182 ) ;

this.trackBar1.Maximum = 100 ;

this.trackBar1.Name = "trackBar1" ;

this.trackBar1.Size = new System.Drawing.Size ( 272 , 42 ) ;

this.trackBar1.TabIndex = 1 ;

this.trackBar1.ValueChanged += new System.EventHandler ( this.trackBar1_ValueChanged ) ;

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.trackBar1 , this.textBox2 , this.textBox1 , this.label2 , this.label1 } ) ;

this.MaximizeBox = false ;

this.MinimizeBox = false ;

this.Name = "Form1" ;

this.Text = "Form1" ;

this.Load += new System.EventHandler ( this.Form1_Load ) ;

( ( System.ComponentModel.ISupportInitialize ) ( this.trackBar1 ) ).EndInit ( ) ;

this.ResumeLayout ( false ) ;

}   

4.由于从窗体向主窗体提出的数据请求是二个TextBox组件的"Text"属性值,所以要修改Form1.cs文件中这二个TextBox组件的定义类型,把缺省定义为"private"类型修改为"public"类型,修改后的这二个TextBox组件在Form1.cs中的定义语句如下:

  public System.Windows.Forms.TextBox textBox1 ;

public System.Windows.Forms.TextBox textBox2 ;   

在上述代码后面再添加下面代码,下面代码是创建一个Form2类的实例m_Form,即从窗体:   

private Form2 m_Form ;   

5.在Form1.cs中的Main函数后,添加下列代码,下列代码的功能是实现当修改主窗体中的跟踪条数值后,从窗体中的label3组件的显示数值能够随之而变化,这样就实现主窗体实时传递数据到从窗体了:   

private void trackBar1_ValueChanged ( object sender , System.EventArgs e )

{ m_Form.label3 .Text = trackBar1.Value.ToString ( ) ; }   

6.在添加完上面代码,并在其后部,再添加下列代码,下列代码的功能是使用Form2类的构造函数,并通过Form1类的实例来创建并初始化Form2类的实例。在项目文件中加入Form2类,并修改Form2类的构造函数工作将在本节的第7到11步骤中完成。   

private void Form1_Load ( object sender , System.EventArgs e )

{ m_Form = new Form2 ( this ) ; //通过主窗体来创建、初始化从窗体 m_Form.Show ( ) ; //显示从窗体 }

本文转自 http://developer.ccidnet.com/art/322/20021224/34599_1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: