C#表單間的資料傳值(使用ArrayList)

來源:互聯網
上載者:User

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();
        }
    }

輸出結果:

123
--------------
abc
--------------
abc
123
--------------
abc
123

看看輸出結果就明白了,另外對實值型別的資料操作要使用ref關鍵字。
    總結,我們通過帶參數的建構函式實現了表單間的資料互動,代碼看上去也比較清楚,在實際開發過程中,可以把DataSet,DataTable,或者是DataView當作參數,當然如果只是想修改一行,可以傳個DataRow或者DataRowView。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.