在.net中輕松掌握Windows窗體間的數(shù)據(jù)交互(一)
zhzuo(秋楓)
Windows 窗體是用于 Microsoft Windows 應用程序開發(fā)的、基于 .NET Framework 的新平臺。此框架提供一個有條理的、面向對象的、可擴展的類集,它使您得以開發(fā)豐富的 Windows 應用程序。一個Windows窗體就代表了.NET架構里的System.Windows.Forms.Form類的一個實例。
作者在CSDN技術論壇.NET板塊下的C#分類經(jīng)常看到有人問起如何在兩個Form間傳遞數(shù)據(jù),訪問修改對方窗體里面的值。對于有經(jīng)驗的程序員來說不是什么高深的東西,而對于初學者來說這些基礎的東西往往是一個問題,并且存在這種現(xiàn)象,往往比較復雜的東西他們會,要用什么了就去學什么,實際上并沒有真正的去理解掌握它,基礎不扎實,所以就有了想通過自己對窗體編程積累的經(jīng)驗來寫一些這方面的文章,以供學.NET的朋友參考,也借此機會同各位朋友進行交流,寫得不合理的地方請各位朋友提寶貴意見,下面我分了三個部分來講。
一.使用帶參數(shù)的構造函數(shù) 我們要做的準備工作就是新建兩個窗體,下面是兩個窗體的布局,很簡單:
<第一個例子>
說明:Form1為主窗體,包含控件:文本框textBoxFrm1,多選框checkBoxFrm1和按鈕buttonEdit;
Form2為子窗體,包含控件:文本框textBoxFrm2,多選框checkBoxFrm2和按鈕buttonOK,buttonCancel。
當我們新建一個窗體的時候,設計器會生成默認的構造函數(shù):
public Form2()
{
InitializeComponent();
}
它不帶參數(shù),既然我們要把Form1中的一些數(shù)據(jù)傳到Form2中去,為什么不在Form2的構造函數(shù)里做文章呢?
假設我們要實現(xiàn)使Form2中的文本框顯示Form1里textBoxFrm1的值,修改子窗體的構造函數(shù):
public Form2(string text)
{
InitializeComponent();
this.textBoxFrm2.Text = text;
}
增加Form1中的修改按鈕點擊事件,處理函數(shù)如下:
private void buttonEdit_Click(object sender, System.EventArgs e)
{
Form2 formChild = new Form2(this.textBoxFrm1.Text);
formChild.Show();
}
我們把this.textBoxFrm1.Text作為參數(shù)傳到子窗體構造函數(shù),以非模式方式打開,這樣打開的formChild的文本框就顯示了”主窗體”文本,是不是很簡單,接下來我們傳一個boolean數(shù)據(jù)給子窗體。
Public Form2(string text,bool checkedValue)
{
InitializeComponent();
this.textBoxFrm2.Text = text;
this.checkBoxFrm2.Checked = checkedValue;
}
在主窗體中的修改按鈕點擊處理,我采用了打開模式窗口的方式,其實在這個例子中看不出有什么分別,
private void buttonEdit_Click(object sender, System.EventArgs e)
{
Form2 formChild = new Form2(this.textBoxFrm1.Text,this.checkBoxFrm1.Checked);
formChild.ShowDialog();
}
結果在預料之中,但是這里明顯存在不足,在子窗體里的數(shù)據(jù)修改后不能傳給主窗體,也就是說主窗體不受子窗體的影響。而在實際的開發(fā)過程中我們經(jīng)常使用子窗體來修改主窗體里面的數(shù)據(jù),那怎么解決呢?
在.NET中有兩種類型,值類型和引用類型。值類型是從ValueType繼承而來,而ValueType又是從Object繼承;對于引用類型它直接繼承Object類型。這下讓我們看看怎樣通過Form2來修改Form1里的數(shù)據(jù)。
還是讓我們來修改Form2的代碼。
Private TextBox textBoxFrm12;
private CheckBox checkBoxFrm12;
public Form2(TextBox heckbo,CheckBox heckbox)
{
InitializeComponent();
this.textBoxFrm2.Text = heckbo.Text;
this.checkBoxFrm2.Checked = heckbox.Checked;
this.textBoxFrm12 = heckbo;
this.checkBoxFrm12 = heckbox;
}
現(xiàn)在我們傳了兩個引用類型的數(shù)據(jù):TextBox類型,和CheckBox;另外在Form2中增加了兩個類數(shù)據(jù)成員textBoxFrm12、checkBoxFrm12用來分別保存構造函數(shù)傳來的變量,不過他們并不屬于Form2的Controls容器。修改Form2的確定按鈕點擊事件函數(shù):
private void buttonOK_Click(object sender, System.EventArgs e)
{
this.textBoxFrm12.Text = this.textBoxFrm2.Text;
this.checkBoxFrm12.Checked = this.checkBoxFrm2.Checked;
this.Close();
}
上面的代碼我們通過把textBoxFrm2的Text和checkBoxFrm2.Checked賦給textBoxFrm12和checkBoxFrm12完成了對主窗體中的textBoxFrm1和checkBoxFrm2的修改,因為textBoxFrm1和textBoxFrm12是同一個引用,而checkBoxFrm2和checkBoxFrm12也是。
到這里為止功能是實現(xiàn)了,但是總覺得不是很合理,讓兩個窗體控件傳來傳去,現(xiàn)在我舉一個恰當一點的例子。
修改了兩個窗體:
<第二個例子>
說明:在這個例子中我們的兩個窗體都加了一個ListBox用來顯示ArrayList中的內(nèi)容。
主窗體中控件:listBoxFrm1,buttonEdit;
子窗體中控件:listBoxFrm2,textBoxAdd,buttonAdd,buttonEdit,buttonOK。
這次我們用ArrayList來作為傳遞數(shù)據(jù),在Form1中定義類數(shù)據(jù)成員:
private ArrayList listData1;
在構造函數(shù)中增加了對listData1進行內(nèi)存分配,并生成數(shù)據(jù)最終綁定到listBoxFrm1,
public Form1()
{
InitializeComponent();
this.listData1 = new ArrayList();
this.listData1.Add("DotNet");
this.listData1.Add("C#");
this.listData1.Add("Asp.net");
this.listData1.Add("WebService");
this.listData1.Add("XML");
this.listBoxFrm1.DataSource = this.listData1;
}
另外,對修改按鈕點擊事件處理函數(shù)的修改如下:
private void buttonEdit_Click(object sender, System.EventArgs e)
{
Form2 formChild = new Form2(this.listData1);
formChild.ShowDialog();
this.listBoxFrm1.DataSource = null;
this.listBoxFrm1.DataSource = this.listData1;
}
相對與主窗體,對子窗體作相應修改,也在Form2中增加了類數(shù)據(jù)成員:
private ArrayList listData2;
用來保存對主窗體中l(wèi)istData1的引用。
修改構造函數(shù):
public Form2(ArrayList listData)
{
InitializeComponent();
this.listData2 = listData;
foreach(object o in this.listData2)
{
this.listBoxFrm2.Items.Add(o);
}
}
這里讓listData2同listData1指向同一個引用;另外沒有對listBoxFrm進行綁定,采用了填充。
好了,下面是對數(shù)據(jù)操作的時候了。
添加處理函數(shù)代碼如下:
private void buttonAdd_Click(object sender, System.EventArgs e)
{
if(this.textBoxAdd.Text.Trim().Length>0)
{
this.listData2.Add(this.textBoxAdd.Text.Trim());
this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());
}
else
MessageBox.Show("請輸入添加的內(nèi)容!");
}
刪除處理代碼如下:
private void buttonDel_Click(object sender, System.EventArgs e)
{
int index = this.listBoxFrm2.SelectedIndex;
if(index!=-1)
{
this.listData2.RemoveAt(index);
this.listBoxFrm2.Items.RemoveAt(index);
}
else
MessageBox.Show("請選擇刪除項或者沒有可刪除的項!");
}
退出Form2子窗體:
private void buttonOK_Click(object sender, System.EventArgs e)
{
this.Close();
}
編譯運行程序,在子窗體中對數(shù)據(jù)進行修改,關閉后,主窗體就會顯示更新后的數(shù)據(jù)。
這里有一點要提醒一下,比較兩個例子,我們都傳的是引用類型,一個是String,另一個是ArrayList,為什么string類型不能修改主窗體的數(shù)據(jù)呢?其實在.Net中對string類型的修改并不是修改原來的值,原來的值沒有變化,而是重新生成一個新的字符串,下面是一個很好的說明。
public class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string str1 = "abc";
string str2 = str1;
str1 = "123";
Console.WriteLine(str1);
Console.WriteLine("--------------");
Console.WriteLine(str2);
Console.WriteLine("--------------");
ArrayList al1 = new ArrayList();
al1.Add("abc");
ArrayList al2 = al1;
al2.Add("123");
foreach(object o in al1)
Console.WriteLine((string)o);
Console.WriteLine("--------------");
foreach(object o in al2)
Console.WriteLine((string)o);
Console.ReadLine();
}
}
運行一下看看輸出結果就明白了,另外對值類型的數(shù)據(jù)操作要使用ref關鍵字。
總結,我們通過帶參數(shù)的構造函數(shù)實現(xiàn)了窗體間的數(shù)據(jù)交互,代碼看上去也比較清楚,在實際開發(fā)過程中,可以把DataSet,DataTable,或者是DataView當作參數(shù),當然如果只是想修改一行,可以傳個DataRow或者DataRowView。在下面的文章中我們來看看怎樣使用另外兩種方法來實現(xiàn)數(shù)
|