?? visual c#實現窗體間數據傳遞(2).txt
字號:
Visual C#實現窗體間數據傳遞(2)
在上一篇文章中我們曾經指出指出,窗體間數據傳遞第一種情況的解決方法存在一個主要的缺點,就是窗體間傳遞的參數數目是固定的,并且類型也是固定的。這是因為,上文中修改了從命名空間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 ( ) ;
//顯示從窗體
}
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 調用后添加任何構造函數代碼
//
}
2.在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就是程序運行后的界面:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -