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

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 6
Reputation: Cory_Brown is an unknown quantity at this point 
Solved Threads: 0
Cory_Brown Cory_Brown is offline Offline
Newbie Poster

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

 
0
  #1
Sep 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 70
Reputation: jatin24 is an unknown quantity at this point 
Solved Threads: 19
jatin24's Avatar
jatin24 jatin24 is offline Offline
Junior Poster in Training

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

 
0
  #2
Sep 14th, 2009
try this:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,977
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

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

 
3
  #3
Sep 14th, 2009
Try this:
  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. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,666
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 474
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

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

 
3
  #4
Sep 14th, 2009
  1. string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,278
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 582
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

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

 
2
  #5
Sep 14th, 2009
Originally Posted by adatapost View Post
  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:
  1. double[] sArray2 = lst.ConvertAll<double>(new Converter<decimal, double>(Convert.ToDouble)).ToArray();
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 924
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

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

 
2
  #6
Sep 14th, 2009
Originally Posted by adatapost View Post
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

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

 
1
  #7
Sep 14th, 2009
mark as solved.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

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

 
2
  #8
Sep 14th, 2009
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
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: Cory_Brown is an unknown quantity at this point 
Solved Threads: 0
Cory_Brown Cory_Brown is offline Offline
Newbie Poster

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

 
0
  #9
Sep 14th, 2009
Thank you all for the responses. Marked as solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC