| | |
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:
Solved Threads: 0
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.
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.
try this:
C# Syntax (Toggle Plain Text)
int n = listBox1.SelectedItems.Count; string[] array = new string[n]; for (int i = 0; i < n; i++) { array[i] = listBox1.SelectedValue.ToString(); }
Try this:
c# Syntax (Toggle Plain Text)
// What I would do List<string> Strlist = new List<string>(); foreach (string str in listBox1.SelectedItems) { Strlist.Add(str); } // Or try the corrected jatin24 style int n = listBox1.SelectedItems.Count; string[] array = new string[n]; for (int i = 0; i < n; i++) { array[i] = listBox1.SelectedItems[i].ToString(); }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
C# Syntax (Toggle Plain Text)
string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();
•
•
•
•
C# Syntax (Toggle Plain Text)
string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();

What I have been doing:
C# Syntax (Toggle Plain Text)
double[] sArray2 = lst.ConvertAll<double>(new Converter<decimal, double>(Convert.ToDouble)).ToArray();
•
•
Join Date: Jul 2009
Posts: 924
Reputation:
Solved Threads: 147
•
•
•
•
C# Syntax (Toggle Plain Text)
string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>();
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
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- How do I get a method to populate and array? (Java)
- Listbox index to 2D index (VB.NET)
- Loading A Text File into a Array into a Listbox (Visual Basic 4 / 5 / 6)
- Error when calling a method of dynamic array (C++)
- Populating an array with results from a mysql query... (PHP)
- How can i call my FileOutput method to output the array ?? really confused... (Java)
Other Threads in the C# Forum
- Previous Thread: "NullReferenceException was unhandled" - Visual Studio C# warning
- Next Thread: how to make temporary array?
| Thread Tools | Search this Thread |
.net access ado.net algorithm alignment array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install java label list listbox listener mandelbrot math mono mouseclick mysql networking operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






