繁体   English   中英

C#Winform从CheckListBox获取价值

[英]C# Winform Getting Value from CheckListBox

我在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 });
    }
}

希望这可以帮助。 只需创建一个名为“ 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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM