Saturday, May 22, 2010

How to show listbox1 form1 contents into form2 listbox2 using c#.net?

Hello,





When you want to pass data between forms (such as listbox items), you should realize a form is a class. Your form1 is a class where you can provide more information, in this case you could provide a polymorphic parameter overloaded constructor such as:





public form2(List%26lt;int%26gt; items) {


foreach(int item in items) listbox2.Items.add(item);


}





So when you want to display form2, you supply the data within the constructor. You could just pass in the Items property as well where it will treat it as reference. You could create a property called ListItems in form2 and when creating the form2 in order to show it from form1, you could just call that property with the items.





form2 form = new form2();


form.ListItems = listbox.Items;


form.show();





There are many ways to pass data between forms, you have to realize it is a class and you treat it as how you would pass data between classes.





Good Luck


No comments:

Post a Comment