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