-
July 26th, 2009, 04:21 PM
#1
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.
-
July 27th, 2009, 01:07 AM
#2
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.
Last edited by BigEd781; July 27th, 2009 at 01:10 AM.
-
July 27th, 2009, 01:59 AM
#3
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 -
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|