1

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 });
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Rog
  • 75
  • 2
  • 9
  • 1
    Possible duplicate of [How to get value of checked item from CheckedListBox?](https://stackoverflow.com/questions/4875540/how-to-get-value-of-checked-item-from-checkedlistbox) – Edney Holder May 16 '19 at 17:09
  • What is `indexlist`? – Rufus L May 16 '19 at 17:15
  • indexlist is a list of public class Indexes { public string indexName; public string indexPath; } – Rog May 16 '19 at 17:21
  • You can use [`GetItemValue`](https://stackoverflow.com/a/38305363/3110834) extension method which works well for all list controls (`CheckedListBox`, `ListBox`, `ComboBox`). – Reza Aghaei May 16 '19 at 17:21
  • I did run through several of the "Duplicate" and none seem to work for me. I am getting Errors when code tries to typecast from Item. – Rog May 16 '19 at 17:22
  • I did try this, but GetItemValue was not available to use.... var texts = this.cblIndexToSearch.CheckedItems.Cast() //.Select(x => this.cblIndexToSearch.GetItemText(x)); .Select(x => this.cblIndexToSearch.GetItemText (x)); This will show me the TEXT but need the Value. – Rog May 16 '19 at 17:22
  • Solutions in the suggested duplicate are not general solutions and all of them are assuming data source is a data table, which is not always the case. – Reza Aghaei May 16 '19 at 17:24
  • GetItemValue is an extension method which I have written in the linked post. It will act like GetItemText but for returning Value. It returns object, you can cast the return value to known type of your value. You need to copy the code to your project and make sure you are using the correct namespace. Then it will appear in list of methods in intellisense. – Reza Aghaei May 16 '19 at 17:30

1 Answers1

0

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.