C # Pass values between forms through arraylist
Main form code
// Arraylist for storing data
Private arraylist listdata;
Public form1 ()
{
Initializecomponent ();
// Initialize arraylist
Listdata = new arraylist ();
Listdata. Add (". Net ");
Listdata. Add ("Java ");
Listdata. Add ("XML ");
Listdata. Add ("WebService ");
// Bind data
Listbox1.datasource = listdata;
}
// Open the subwindow
Private void buttonopen_click (Object sender, eventargs E)
{
// Arraylist is passed as a parameter
Form2 frm2 = new form2 (listdata );
Frm2.showdialog ();
// Modify the arraylist in the subwindow and bind the data again.
Listbox1.datasource = NULL;
// Listbox1.item. Clear (); the error message "the item set cannot be modified after the datasource attribute is set" is displayed.
Listbox1.datasource = listdata;
}
For the error "unable to modify the collection of items after setting datasource properties", refer to http://www.ideaext.com/read.php/312.htm
Subwindow code
Arraylist listdata;
Public form2 (arraylist listdata)
{
Initializecomponent (); // Note: You must retain this option when modifying the default constructor. Otherwise, the control in the subwindow cannot be used as an example, the error message "the instance to which the object is not referenced" is displayed. I just made this mistake.
This. listdata = listdata;
Foreach (Object DA in listdata)
{
Listbox1.items. Add (DA );
}
}
// Add a value to 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 ("Enter the added content! ");
}
// Delete values in arraylist and 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 ("Please select to delete items or there are no items to delete! ");
}
// Close the window. The modified value is rebound to The ListBox of the main window.
Private void buttonexit_click (Object sender, eventargs E)
{
This. Close ();
}
Here, we need to remind you that we can compare the two examples by passing the reference type, one is string and the other is arraylist. why can't the data of the main form be modified for the string type? In fact, in. net, modifying the string type is not modifying the original value, but the original value is not changing. Instead, a new string is generated. Below is a good description.
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 Al = al1;
Al2.add ("123 ");
Foreach (Object o in al1)
Console. writeline (string) O );
Console. writeline ("--------------");
Foreach (Object o in Al)
Console. writeline (string) O );
Console. Readline ();
}
}
Output result:
123
--------------
ABC
--------------
ABC
123
--------------
ABC
123
You can see the output result. In addition, you need to use data operations of the value type.RefKeyword.
In summary, we use constructors with parameters to implement data interaction between forms, and the code looks clear. In actual development, dataset, datatable, or dataview can be used as parameters, if you only want to modify a row, you can upload a datarow or datarowview.