问题
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