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.

8
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by Cory_Brown
Featured Replies
  • 3
    ddanbe 2,525   7 Years Ago

    Try this: [CODE=c#]// 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(); }[/CODE] Read More

  • 3
    __avd 1,826   7 Years Ago

    [code] string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>(); [/code] Read More

  • 2
    sknake 1,622   7 Years Ago

    [QUOTE=adatapost;985608][code] string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>(); [/code][/QUOTE] That is handy! For unboxing casts I have always used the .ConvertAll() but this is much easier to call :) What I have been doing: [code] double[] sArray2 = lst.ConvertAll<double>(new Converter<decimal, double>(Convert.ToDouble)).ToArray(); [/code] Read More

  • [QUOTE=adatapost;985608][code] string[] ar = listBox1.SelectedItems.Cast<string>().ToArray<string>(); [/code][/QUOTE] 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.;) Read More

  • mark as solved. Read More

  • 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 Read More

0

try this:

int n = listBox1.SelectedItems.Count;
            string[] array = new string[n];
            for (int i = 0; i < n; i++)
            {
                array[i] = listBox1.SelectedValue.ToString();
            }
3

Try this:

// 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();
            }
Comments
Great man
very observant
2
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:

double[] sArray2 = lst.ConvertAll<double>(new Converter<decimal, double>(Convert.ToDouble)).ToArray();
Comments
ThreadSafe++
2
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.;)

Comments
Thanks!
2

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

Comments
Nice you did this test!
Test++
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.