I add the melon box to the sheet via the data source. In case of multiple selection, it is necessary to add it to the collection. When outputting, we get the same value a couple of times

List<string> comission = new List<string>(); for (int i = 0; i < listBox2.Items.Count; i++) { if (listBox2.GetSelected(i) == true ) { comission.Add(listBox2.SelectedValue.ToString()); ; } } foreach (string ar in comission) { label3.Text += ar + "\n"; } 

    1 answer 1

    Because you always add the same value that lies in the SelectedValue , which depends on the SelectedItem , which always contains the first selected item or null if nothing is selected. Use the SelectedItems property:

     List<string> comission = new List<string>(); foreach (var item in listBox2.SelectedItems) { comission.Add(item.ToString()); } foreach (string ar in comission) { label3.Text += ar + "\n"; } 

    Of course, you can do one foreach .

    If you need to do exactly through for - use the Items property. Then the add line changes to:

     comission.Add(listBox2.Items[i].ToString()); 
    • System.Data.Common.DataRecordInternal This is what derives - Leonid Smirnov
    • Listbox is filled out of the database? What bindings go? - Zufir
    • listBox2.DataSource = dl.GetAllCommission (); listBox2.DisplayMember = "fio"; listBox2.ValueMember = "id"; - Leonid Smirnov
    • comission.Add((listBox2.Items[i] as DbDataRecord).Items["fio"]); - does that work? - Zufir
    • Unfortunately not - Leonid Smirnov