How can I reach the Tag of a checkedListBox-Item??

Hi!

I created a CheckedListBox.
Every contained item got an unique Tag:

ListViewItem baItem = new ListViewItem(bauabschnitt.BA_Bezeichnung);
baItem.Tag = bauabschnitt.BA_ID;
listBA.Items.Add(baItem.Text);

Now I want to loop through the selectedItems and retrieve the Tag of each item.

But I only can do something like this:

foreach (int indexBA in listBA.CheckedIndices) {
string baBez = (string)listboxBAS.Items[indexBA];
...
}

Isn't it possible to retrieve the Tag of an item

Thanks,
Barthi


Answer this question

How can I reach the Tag of a checkedListBox-Item??

  • Neeti

    Genius, man!

    Thanks a lot!!

  • CTRon

    string baBez = (string)listboxBAS.Items[indexBA].Tag; // this doesn't work

  • R_oo_T

    Try this then;

    ListViewItem item = (ListViewItem)listboxBAS.Items[indexBA];
    string baBez = (string)item.Tag;

  • Big-O

    No, this doesn't work either.

    "listBA.Items[indexBA]" only returns Strings

  • SEH

    The display you are seeing in your CheckedListBox is due to it automatically calling the ToString() method on the items it contains. In order to override that we need to specify the the name of the property within those items you want displayed instead. In this case we simply set DisplayMember to Text ala:

    listBA.DisplayMember = "Text";

    Just for note, DisplayMember may not show up in your intellisense display, rest assured though that it does exist as part of the CheckedListBox class and use it anyway, the compiler will be fine with it.



  • Jandler2

    You can't use
    foreach
    (ListViewItem item in listBA.Items)
    because the CheckedListBox has no GetEnumerator()-Methode.

    Any other suggestions

  • Expressmann

    If you are sure that each item in your CheckedListBox is a ListView Item then you can simply iterate through like so:

    foreach (ListViewItem item in listBA.Items)

    {

    //item.Tag is available

    }

    If the type is different then you may need to do a little more filtering like itterating through looking for objects and then checking types.

    Does this work for you



  • dev.McClannahan411

    That was my first thought as well unfortunately the Items collection within the CheckedListBox control only returns objects, unlike ListView which does return ListViewItem-ed typed values.

  • Ramirez3964

    No, only Equals(), getHashCode(), getType() and ToString().

  • Simsabim

    Bah, I screwed up on my sample code to you... to confirm though... you are adding your own instances of the ListViewItem class to the CheckedListBox control

    If so... we do need to do a little more filtering:

    foreach (object o in listBA.Items)
    {
    ListViewItem item = o as ListViewItem;
    if (item != null)
    {
    Console.WriteLine(item.Tag);
    }
    }



  • casgo

    But what kind of objects only strings

  • DeepakGH

    Ok, I found my mistake:

    I only added text to the listview.
    When I add ListViewItems, I can use

    ListViewItem baItem = (ListViewItem)listBA.Items[indexBA];
    string baBez = (string)baItem.Tag;

    But the ugly thing is, that in my list every entry looks like this:

    ListViewItem: {1.BA}
    ListViewItem: {2.BA}

    Is there a way to just show the following

    1.BA
    2.BA


  • How can I reach the Tag of a checkedListBox-Item??