Using Listbox items outside the form
On a form there is a listbox containing serveral items (single column). These items are added to dynamically. No datasource is used.
I want to create a class that needs access to these items. I was thinking something in the line of:
class Test
private List<string> testList
public void setTestList (List<string> list)
{
testList = list;
}
....
But I can't seem to pass the Listbox items as a list to the setTestList() method.
Is there a way to pass the Listbox items as a list?
Or do I need another approach?
Setting the Listbox modifier to public is not really an option.
Any information is welcome.
Re: Using Listbox items outside the form
Well, the list box items are not of the type List<string>, so that is why that will not work. The items are contained in a ListBox.ObjectCollection object ("Items"), so your parameter would need to be of the same type. However, I think this would be a pretty bad design choice here.
first I would ask; for what reason do you need access to these items on your second form? If you only need some of the data they contain, just pass that in. If you need to add or remove items, just make an event(s) in your second form class that can be handled by the main form. The secondary form can fire the events when data needs to be changed. This approach eliminates unnecessary coupling between the two classes.
Re: Using Listbox items outside the form
First copy the list box items( text or anything ) to an array - may be to a list as you have mentioned :
Code:
List<string> testList
and then pass this as a parameter -