ListBox获取文本框

ListBox获取文本框

本文介绍了从MultiSelect ListBox获取文本框(用逗号分隔)中SelectedItems的选定值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我如何获取 ListBox ValueMember ?我已经阅读了许多教程,但仍然无法解决.任何帮助将不胜感激.

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 += ", ";
    }
}

推荐答案

您的 for 循环不正确.只需尝试一下(这将遍历您的 ListBox 的所有 SelectedIndices ,并将它们添加到您的 TextBox 中):

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 += @", ";
}

甚至更好:

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

这篇关于从MultiSelect ListBox获取文本框(用逗号分隔)中SelectedItems的选定值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:06