C#窗体间通过ArrayList传值

    技术2022-05-12  12

    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。


    最新回复(0)