Get Selected Value of SelectedItems in a TextBox (

2019-09-07 15:30发布

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条回答
看我几分像从前
2楼-- · 2019-09-07 15:51

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>());
查看更多
登录 后发表回答