C#Winform从CheckListBox获取价值

时间:2019-05-16 17:04:45

标签: c# winforms checkedlistbox

我在winform上有一个checkedListBox。 我在复选框中填写了一些代码,如下所示。

一切正常。

现在,我需要获取已检查项目的值。我遇到一些问题。

当我做foreach(var item in clbtest.CheckedItems)

在执行“立即”窗口时得到此信息吗?项目

{ Text = "Depos", Value = "Q:\\Scanning Department\\_DT SEARCH INDEXES\\Depos" }
Text: "Depos"
Value: "Q:\\Scanning Department\\_DT SEARCH INDEXES\\Depos"

我不知道如何获得“价值”字段。我尝试了几种方法,但似乎无济于事。

private void FillIndexes()
{
    string stext = cblIndexToSearch.Text;
    cblIndexToSearch.Items.Clear();

    cblIndexToSearch.Text = "";
    cblIndexToSearch.DisplayMember = "Text";
    cblIndexToSearch.ValueMember = "Value";

    foreach (var item in indexlist)
    {
        cblIndexToSearch.Items.Insert(0, 
            new { Text = item.indexName, Value = @item.indexPath });
    }
}

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助。只需创建一个名为“ checkedListBox1”的复选框,代码就可以正常工作。我详细介绍了它的作用。

        checkedListBox1.Items.Clear();

        checkedListBox1.Text = "";
        checkedListBox1.DisplayMember = "Text";
        checkedListBox1.ValueMember = "Value";


        checkedListBox1.Items.Insert(0,
                new { Text = "Rawr", Value = "Whatever 2011"});

        string temp = checkedListBox1.Items[0].ToString(); //gets the string of the item. (switch 0 to the index you want)
        string string_Value = temp.Split(new string[] { "Value = " }, StringSplitOptions.None)[1]; //splits the string by the value part and returns the string value.
        string_Value = string_Value.Substring(0, string_Value.Length - 2); ; //removes the last 2 characters since they are not part of the value.
        //you now have the value in string form.