0

I have a ListBox that has X amount (Varies from 9 - 90) of items. I'm trying to populate (On a button click) a string array with the items that are selected in the ListBox. Here is what I have so far

private void generateTam_Click(object sender, EventArgs e)
    {
        String sCombinedTam = "";
        String sTamResponseStart = "Dear $contacts.name.first,@\n@\nYour request has been received and completed.@\nThe following actions were taken:@\n";
        String sTamResponseEnd = "Thank you for choosing %company%, and have a great day!!@\n@\n$incidents.assigned.acct_id@\n%company%";

        sTamResponseStart = sTamResponseStart.Replace("@\n", System.Environment.NewLine); //Replaces token @\n with NewLine
        //Gets Actions Selected, Sends to Array
        String[] sActionItemsSelected = new String[actionsListBox.Items.Count];
        for (int x = 0; x < actionsListBox.Items.Count; ++x)
        {
            if (actionsListBox.GetSelected(x) == true)
            {
                actionsListBox.Items.CopyTo(sActionItemsSelected, 0);

            }
        }
        //Gets Profiles Selected, Sends to Array
        String[] sProfileItemsSelected = new String[profilesListBox.Items.Count];
        for (int x = 0; x < profilesListBox.Items.Count; ++x)
        {
            if (profilesListBox.GetSelected(x) == true)
            {
                profilesListBox.Items.CopyTo(sProfileItemsSelected, x);
            }
            else if (profilesListBox.GetSelected(x) == false)
            {
                sProfileItemsSelected[x] = "";
            }
        }
        //Combines strings for end response
        for (int i = 0; i < sActionItemsSelected.Length; ++i)
        {
            sCombinedTam = sCombinedTam + sActionItemsSelected[i] + "@\n";
        }
        sCombinedTam = sCombinedTam.Replace("@\n", System.Environment.NewLine);
        sTamResponseEnd = sTamResponseEnd.Replace("@\n", System.Environment.NewLine);
        sCombinedTam = sTamResponseStart + sCombinedTam + sTamResponseEnd;
        notesTextBox.Text = sCombinedTam;

        //Outputs ENTIRE index ListBoxes not just selected items.

    }

The problem comes at the end, instead of setting notesTextBox.Text to the combination string with only the ListBox Selected Items it sets it to the combination string with EVERY ListBox option.

Any help would be greatly appreciated.

2
  • what is your expected output for sCombinedTam? Commented Apr 9, 2014 at 0:29
  • @jhyap Something like this Dear $contacts.name.first,@\n@\nYour request has been received and completed.@\nThe following actions were taken:@\n [Insert Action Selected 1] [Insert Action Selected 2] ... [Insert Action Selected 90] Thank you for choosing %company%, and have a great day!!@\n@\n$incidents.assigned.acct_id@\n%company% Where @\n is replaced with a new line. Commented Apr 9, 2014 at 0:31

3 Answers 3

3

Use the ListBox.SelectedItems property to get all selected items in a single string, separated by the newline string (you can remove the last for loop, as well as some of your other code).

You can cast the collection of selected items back to whatever type they are; in your case, a string.

var selectedItems =
    String.Join(Environment.NewLine, listBox1.SelectedItems.Cast<string>());
Sign up to request clarification or add additional context in comments.

4 Comments

Doing sActionItemsSelected = String.Join(Environment.NewLine, actionsListBox.SelectedItems); Returns the following System.Windows.Forms.ListBox+SelectedObjectCollection Not the string like I was expecting.
The window this is contained in is WPF but the application itself is WinForms.
Yes just a collection of strings.
It looks like I am using WinForms controls as I look at it now.
0

Using linq

items = (from string s in listBox1.SelectedItems select s).ToArray();

Comments

0

This worked for me:

Dim selectedItems As String() = listBox1.SelectedItems.Cast(Of String).ToArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.