1

I am trying to get all the values from a multi-select listbox stored into a string variable. The multi-select listbox has the group name and its corresponding group ID. I need to have the group name displayed, but when the user selects the group name, it stores the group ID value into a CSV format variable ("23,8,4,13").

I've been trying to text using a text box, but so far have been only to return just one value.

I get the group name and values from a GET request.

private void Form1_Load(object sender, EventArgs e)
        {
            //Makes the request to API when the form loads
            Request rClient = new Request();

            //endpoint is to GET the groups
            rClient.endPoint = rClient.endPoint + "groups";
            rClient.httpMethod = httpVerb.GET;

            string strResponse = string.Empty;

            strResponse = rClient.makeRequest();

            List<RootObject> grpName = JsonConvert.DeserializeObject<List<RootObject>>(strResponse);

            lstbxGroups.ValueMember = "id";
            lstbxGroups.DisplayMember = "name";
            lstbxGroups.DataSource = grpName;
            lstbxGroups.SelectedIndex = -1;

        }

private void btnAdd_Click(object sender, EventArgs e)
        {

            foreach (object item in lstbxGroups.SelectedItems)
            { txtbxTest.Text = lstbxGroups.SelectedValue.ToString(); }

        }

Would someone please help me or direct me in the right direction to get the selected values? Thanks much in advanced!

Lilith Mae
  • 132
  • 1
  • 11
  • I'm sorry, but I followed those suggestions and get: System.InvalidCastException Unable to cast object of type 'AddUser_API.RootObject' to type 'System.Data.DataRowView'. – Lilith Mae Jan 14 '19 at 16:25
  • The API response is the data source of the listbox, which returns to me the groupName and groupID. The groupName is the Display Member and the groupID is the Value Member. The user should be able to select multiple objects, but return the Value Member to me in a string. – Lilith Mae Jan 14 '19 at 16:28
  • 1
    I closed it with a correct duplicate. Read the accepted answer in the linked post. – Reza Aghaei Jan 14 '19 at 16:38
  • 1
    Also posted my suggested answer, for [listbox selected items in winform](https://stackoverflow.com/a/54185632/3110834), which didn't have a generic clean solution. – Reza Aghaei Jan 14 '19 at 16:43
  • Thank you so much for pointing me in the right direction. I created the class and followed the generic solution after, but now I receive upon testing: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.Data.DataRowView'.' – Lilith Mae Jan 14 '19 at 16:53
  • 1
    Read the [duplicate post](https://stackoverflow.com/questions/40819000/how-to-retrieve-selected-values-for-selected-items-in-a-listbox) which describes how to get a list of selected values. By having a list of selected values you can easily convert it to string, for example: `var txt = string.Join(",", values.Select(x=>x.ToString());`. – Reza Aghaei Jan 14 '19 at 16:57
  • 1
    The other [linked post](https://stackoverflow.com/a/54185632/3110834) is about getting selected texts which is not what you are looking for. – Reza Aghaei Jan 14 '19 at 16:58
  • Alright! This was extremely helpful and thank you for being patient. I was able to retrieve data they way I needed it by first creating the class `ListControlExtensions`, then in my form main doing this: `var texts = this. lstbxGroups.SelectedItems.Cast() .Select(x => this.lstbxGroups.GetItemValue(x)); txtbxTest.Text = (string.Join(",", texts));` – Lilith Mae Jan 14 '19 at 17:12
  • 1
    Great! You're welcome :) – Reza Aghaei Jan 14 '19 at 17:15

0 Answers0