5 Replies - 12223 Views - Last Post: 13 January 2013 - 03:36 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

How Do You Cast A IEnumerable List To A Listbox Object Collection?

Posted 11 January 2013 - 10:03 PM

It's easy to cast a Listbox.ObjectCollection to a list to perform enumerations on but what about if you want to go the other way? I KNOW YOU CAN USE A FOREACH( Like

 foreach (string d in the_domains)
                {
                    this.dList.Add(d);
                }



Now

 var the_domains = domains.Items.Cast<string>().Where(item => (item.Contains("Is Available")));
                foreach (string d in the_domains)
                {
                    this.lstbx_domains.Items.Add(d);
                }




works find but what if I want to now convert the_domains? I've tried a simple case like
(Listbox.ObjectCollection)the_domains


This works but it gets generates a exception.

Is This A Good Question/Topic? 0
  • +

Replies To: How Do You Cast A IEnumerable List To A Listbox Object Collection?

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,530
  • Joined: 08-April 09

Re: How Do You Cast A IEnumerable List To A Listbox Object Collection?

Posted 12 January 2013 - 09:38 AM

Maybe try using explicit cast, so instead var the_domains use type the_domains

You need to know what type the_domains is and is it convertible.

This post has been edited by Michael26: 12 January 2013 - 09:46 AM

Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: How Do You Cast A IEnumerable List To A Listbox Object Collection?

Posted 12 January 2013 - 01:44 PM

View PostMichael26, on 12 January 2013 - 09:38 AM, said:

Maybe try using explicit cast, so instead var the_domains use type the_domains

You need to know what type the_domains is and is it convertible.


Converting an IEnumerable List to a Listbox.Collection is what I'm trying to do?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: How Do You Cast A IEnumerable List To A Listbox Object Collection?

Posted 12 January 2013 - 02:07 PM

I'm sure that the listbox can add a collection in one stop.
lb.Clear
lb.AddRange(results)


Was This Post Helpful? 1
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: How Do You Cast A IEnumerable List To A Listbox Object Collection?

Posted 12 January 2013 - 05:59 PM

View PostAdamSpeight2008, on 12 January 2013 - 02:07 PM, said:

I'm sure that the listbox can add a collection in one stop.
lb.Clear
lb.AddRange(results)



I tried that that's what I'm saying the list in this case using your example pretend results is an IEnumerable string list and it needs to converted to a listbox collection or else add range won't work.
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: How Do You Cast A IEnumerable List To A Listbox Object Collection?

Posted 13 January 2013 - 03:36 AM

To convert results to a ListBox.ObjectCollection, you could do this:

ListBox.ObjectCollection oc = new ListBox.ObjectCollection(this.yourListbox, results.ToArray());



That's unnecessary though, as there is an overload of AddRange() that will effectively do that for you, and add the strings to the list box:

this.yourListBox.Items.AddRange(results.ToArray());


Alternatively, you could just do:

this.yourListBox.DataSource = results.ToArray();


If you choose to set .DataSource, just be aware that the list box's underlying collection cannot be modified (meaning you cannot add or remove items, for example) whilst .DataSource is set.

This post has been edited by CodingSup3rnatur@l-360: 14 January 2013 - 04:31 AM
Reason for edit:: Added comma to argument list.

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1