C#表單間傳值

來源:互聯網
上載者:User

關於C#表單間的資料傳值的方法好幾種,在項目中都各有應用,雖然簡單,這裡記錄下來,分享給大家!
一、使用帶參數的建構函式
主表單
private void button_Click(object sender, System.EventArgs e)
{
// 將主表單的控制項值作為參數傳遞到子表單
Form2 formChild = new Form2(this.textBoxFrm1.Text,this.checkBoxFrm1.Checked);
formChild.ShowDialog();
}

子表單(Form2)

// 修改預設無參表單建構函式,接受傳遞過來的值,且傳遞給子表單控制項。
Public Form2(string text,bool checkedValue)
{
InitializeComponent();
this.textBoxFrm2.Text = text;
this.checkBoxFrm2.Checked = checkedValue;
}

但是這種傳值存在不足,在子表單裡的資料修改後不能傳給主表單,也就是說主表單不受子表單的影響。而在實際的開發過程中我們經常使用子表單來修改主表單裡面的資料。
二、 在.NET中有兩種類型,實值型別和參考型別。
實值型別是從ValueType繼承而來,而ValueType又是從Object繼承;對於參考型別它直接繼承Object類型。這下讓我們看看怎樣利用參考型別來通過Form2來修改Form1裡的資料。
子表單代碼
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;
}
// 關閉子表單代碼
private void buttonOK_Click(object sender, System.EventArgs e)
{
// 給textbox和checkbox對象賦值。這裡修改了同一塊記憶體。
this.textBoxFrm12.Text = this.textBoxFrm2.Text;
this.checkBoxFrm12.Checked = this.checkBoxFrm2.Checked;
this.Close();
}

這裡也實現了傳值的功能,但是總覺得不是很合理,總是讓兩個表單控制項傳來傳去,很煩,如果控制項多了呢。當然我們可以將需要修改值的控制項封裝到自訂的結構體中,但是....我們再來看看下面一種傳值方式吧。
三、C#表單間通過ArrayList傳值
主表單代碼

// 儲存資料的ArrayList
private ArrayList listData;
public Form1()
{
InitializeComponent();
// 初始化ArrayList
listData = new ArrayList();
listData.Add(".Net");
listData.Add("Java");
listData.Add("XML");
listData.Add("WebService");
// 綁定資料
listBox1.DataSource = listData;
}
// 開啟子視窗
private void buttonOpen_Click(object sender, EventArgs e)
{
// ArrayList 作為參數傳遞
Form2 frm2 = new Form2(listData);
frm2.ShowDialog();
// 在子視窗修改ArrayList 後重新綁定資料
listBox1.DataSource = null;
// listBox1.Item.Clear(); 提示"設定DataSource屬性後無法修改項集合"錯誤。
listBox1.DataSource = listData;
}

關於"設定DataSource屬性後無法修改項集合"錯誤,參考http://www.ideaext.com/read.php/312.htm
子視窗
ArrayList listData;
public Form2(ArrayList listData)
{
InitializeComponent(); // 注意,在修改預設構造方法的時候需要保留此項,否則子視窗的控制項無法執行個體話,提示"未將對象引用設定到對象的執行個體"錯誤。我剛剛就犯了這錯誤,呵呵
this.listData = listData;
foreach (object da in listData)
{
listBox1.Items.Add(da);
}
}
// 給ArrayList 裡添加值
private void buttonAdd_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim().Length > 0)
{
this.listData.Add(this.textBox1.Text.Trim());
this.listBox1.Items.Add(this.textBox1.Text.Trim());
}
else
MessageBox.Show("請輸入添加的內容!");
}
// 刪除ArrayList 和listbox中的值
private void buttonDel_Click(object sender, EventArgs e)
{
int index = this.listBox1.SelectedIndex;
if (index != -1)
{
this.listData.RemoveAt(index);
this.listBox1.Items.RemoveAt(index);
}
else
MessageBox.Show("請選擇刪除項或者沒有可刪除的項!");
}
// 關閉視窗,修改後的值就重新綁定到主視窗listbox上了
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();
}

這裡有一點要提醒一下,比較兩個例子,我們都傳的是參考型別,一個是String,另一個是ArrayList,為什麼string類型不能修改主表單的資料呢?其實在.Net中對string類型的修改並不是修改原來的值,原來的值沒有變化,而是重建一個新的字串,下面是一個很好的說明。
public class PdtGrp
{
[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();
}
}

運行一下看看輸出結果就明白了,另外對實值型別的資料操作要使用ref關鍵字。
總結,我們通過帶參數的建構函式實現了表單間的資料互動,代碼看上去也比較清楚,在實際開發過程中,可以把DataSet,DataTable,或者是DataView當作參數,當然如果只是想修改一行,可以傳個DataRow或者DataRowView。
四、使用自訂屬性或方法
下面我們來講講怎樣使用自訂屬性或方法來完成資料修改功能而不使用Form2_Load事件。
主表單的修改按鈕點擊處理函數如下:
private void buttonEdit_Click(object sender, System.EventArgs e)
{
Form2 formChild = new Form2();
formChild.ListData2 = this.listData1;
formChild.ShowDialog();
this.listBoxFrm1.DataSource = null;
this.listBoxFrm1.DataSource = this.listData1;
}
並且我們去掉了主表單的ListData1屬性,
//public ArrayList ListData1
//{
// get{return this.listData1;}
//}
而在子表單中加上ListData2屬性,
public ArrayList ListData2
{
set
{
this.listData2 = value;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
}
}
也可以把屬性改成方法,
public void SetListData(ArrayList listData)
{
this.listData2 = listData;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
}
而在主表單的修改按鈕處理函數中也要相應改動:
formChild.ListData2 = this.listData1;
改為
formChild.SetListData(this.listData1);
總結,我們通過Form類的Owner屬性來建立主從表單間的橋樑,這個是不是類似於把主表單作為子表單的建構函式參數傳入實現的功能差不多;另外又採用了屬性和方法來完成資料的互動,我覺得這種實現方法很實用,特別是用在不需要執行個體化類或著已經有了執行個體的情況下傳遞資料。下一節我們來講如何使用靜態類來完成資料的互動。
五、使用靜態類
之前使用帶參數的建構函式、屬性以及方法實現了資料的互動,接下來的是使用靜態類來完成表單間的資料互動。這也是經常要用到的一種資料互動方法。
下面是定義的一個類:
using System;
using System.Collections;
namespace ZZ
{
public class AppDatas
{
//待用資料成員
private static ArrayList listData;
//靜態建構函式
static AppDatas()
{
listData = new ArrayList();
listData.Add("DotNet");
listData.Add("C#");
listData.Add("Asp.net");
listData.Add("WebService");
listData.Add("XML");
}
//靜態屬性
public static ArrayList ListData
{
get{return listData;}
}
//靜態方法
public static ArrayList GetListData()
{
return listData;
}
}
}
上麵包含了一個靜態類成員,listData,一個靜態建構函式static AppDatas(),用來初始化listData的資料。還有一個靜態屬性ListData和一個靜態GetListData()方法,他們實現了同樣的功能就是返回listData

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.