简体   繁体   中英

C# Winform Getting Value from CheckListBox

I have a checkedListBox on a winform. I fill the check box with some code see below.

All works well.

Now I need to get the value of the checked items. I am having some issues.

When I do the foreach(var item in clbtest.CheckedItems)

I get this when doing a Immediate window... ? item

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

I don't know how to get at the Value field. I have tried several ways but nothing seems to work.

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

Hope this helps. Just create a checklistbox named "checkedListBox1" and the code should work. I detailed a little on what it does.

        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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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