CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    5

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    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
  •  





Click Here to Expand Forum to Full Width

Featured