943,879 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1138
  • C# RSS
Sep 14th, 2009
0

What is the method for populating an array with selections from a listbox

Expand Post »
I need to populate an array with the items a user has selected in a listbox.

Example: Listbox contains A, B, C, D, E
User selects B, D, E

create an array containing B, D, E

What method would I use to do this? I've found plenty of info for populating a listbox with the contents of an array, but not the other way around.

Thanks in advance for any help.
Similar Threads
Reputation Points: 18
Solved Threads: 0
Newbie Poster
Cory_Brown is offline Offline
21 posts
since Sep 2009
Sep 14th, 2009
0

Re: What is the method for populating an array with selections from a listbox

try this:

C# Syntax (Toggle Plain Text)
  1.  
  2. int n = listBox1.SelectedItems.Count;
  3. string[] array = new string[n];
  4. for (int i = 0; i < n; i++)
  5. {
  6. array[i] = listBox1.SelectedValue.ToString();
  7. }
Reputation Points: 31
Solved Threads: 21
Junior Poster in Training
jatin24 is offline Offline
74 posts
since Aug 2009
Sep 14th, 2009
3

Re: What is the method for populating an array with selections from a listbox

Try this:
c# Syntax (Toggle Plain Text)
  1. // What I would do
  2. List<string> Strlist = new List<string>();
  3. foreach (string str in listBox1.SelectedItems)
  4. {
  5. Strlist.Add(str);
  6. }
  7.  
  8. // Or try the corrected jatin24 style
  9. int n = listBox1.SelectedItems.Count;
  10. string[] array = new string[n];
  11. for (int i = 0; i < n; i++)
  12. {
  13. array[i] = listBox1.SelectedItems[i].ToString();
  14. }
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,740 posts
since Oct 2008
Sep 14th, 2009
3

Re: What is the method for populating an array with selections from a listbox

C# Syntax (Toggle Plain Text)
  1. string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 14th, 2009
2

Re: What is the method for populating an array with selections from a listbox

Click to Expand / Collapse  Quote originally posted by adatapost ...
C# Syntax (Toggle Plain Text)
  1. string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();
That is handy! For unboxing casts I have always used the .ConvertAll() but this is much easier to call

What I have been doing:
C# Syntax (Toggle Plain Text)
  1. double[] sArray2 = lst.ConvertAll<double>(new Converter<decimal, double>(Convert.ToDouble)).ToArray();
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 14th, 2009
2

Re: What is the method for populating an array with selections from a listbox

Click to Expand / Collapse  Quote originally posted by adatapost ...
C# Syntax (Toggle Plain Text)
  1. string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();
Wanted to +rep you, but I guess I already did somewhere and it wouldn't let me. Anyway, that is a very clean line of code I haven't seen before--kudos! I hope I remember it the next time I do that.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 14th, 2009
1

Re: What is the method for populating an array with selections from a listbox

mark as solved.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 14th, 2009
2

Re: What is the method for populating an array with selections from a listbox

In terms of long lists and performance, Danny's solution is the best and to make it better use 'for' loop instead of 'foreach'
My test on 10000 items
Danny (for not foreach) | 39060 ticks
Danny | 48825 ticks
adatapost | 761670 ticks
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Sep 14th, 2009
0

Re: What is the method for populating an array with selections from a listbox

Thank you all for the responses. Marked as solved.
Reputation Points: 18
Solved Threads: 0
Newbie Poster
Cory_Brown is offline Offline
21 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: "NullReferenceException was unhandled" - Visual Studio C# warning
Next Thread in C# Forum Timeline: how to make temporary array?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC