Get Selected Value of SelectedItems in a TextBox (separated with Commas) from MultiSelect ListBox?

本秂侑毒 提交于 2019-12-13 07:27:08

问题


Please tell me how can I get ValueMember of ListBox SelectedItems? I have read many tutorials but still I am unable to solve it. Any help will be greatly appreciated.

int c = subjects_Listbox.Items.Count - 1;
for (int i = 0; i >= 0; i--)
{
    if (subjects_Listbox.GetSelected(i))
    {
        txt.Text += subjects_Listbox.SelectedIndices[i].ToString();
        txt.Text += ", ";
    }
}

回答1:


Your for loop is incorrect. Just try this (this iterate through all SelectedIndices of your ListBox and will add them to your TextBox):

foreach (var item in subjects_Listbox.SelectedIndices)
{
     txt.Text += item;
     txt.Text += @", ";
}

Or even better:

txt.Text = string.Join(",", subjects_Listbox.SelectedIndices.Cast<int>());


来源:https://stackoverflow.com/questions/34238374/get-selected-value-of-selecteditems-in-a-textbox-separated-with-commas-from-mu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!