从MultiSelect ListBox中获取文本框(以逗号分隔)中所选项目的选定值成员?

时间:2015-12-12 11:34:11

标签: c# .net winforms listbox listboxitems

请告诉我如何获得ListBox SelectedItems的ValueMember。任何帮助将不胜感激。我能够获得所选的索引,但我希望在用逗号分隔的文本框中选中每个选定的多选列表框显示成员的值。

int sum;
for(sum=0;n>0;sum+=n--);

我的数据源是subjects_Listbox.DataSource = dt; subjects_Listbox.DisplayMember = "subject_title"; subjects_Listbox.ValueMember = "subject_id"; ,因此我的selectedItems类型为DataView

1 个答案:

答案 0 :(得分:0)

以下是示例代码。没有优化,但工作

    private string[] listItem = { "a", "b", "c", "d", "e", "f","g","h","i" };
    private List<string> selectedItem = new List<string>();

    public Form1()
    {
        InitializeComponent();
        LoadItem();
    }

    private void LoadItem() 
    {
        for (int i = 0; i < listItem.Count(); i++)
        {
            lstItem.Items.Add(listItem[i]);
        }
    }

    private void btnGetSelectedItem_Click(object sender, EventArgs e)
    {
        int CountSelectedItem = lstItem.SelectedItems.Count; 
        for (int i = 0; i < CountSelectedItem; i++)
        {
            string text = lstItem.SelectedItems[i].ToString();
            selectedItem.Add(text);   
        }

        for (int i = 0; i < selectedItem.Count; i++)
        {
            txtSelectedItem.Text = txtSelectedItem.Text + "," + selectedItem[i]; 
        }
    }

我在ListBox中填充了一些项目。按钮单击生成带有列表框中所选项目的文本框。