Windows Develop Bookmark and Share   
 index > Windows Forms General > How can I reach the Tag of a checkedListBox-Item??
 

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
Barthi  Tuesday, June 20, 2006 3:46 PM

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.

Brendan Grant  Tuesday, June 20, 2006 6:18 PM

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?

Brendan Grant  Tuesday, June 20, 2006 3:54 PM
string baBez = (string)listboxBAS.Items[indexBA].Tag; // this doesn't work?
JRQ  Tuesday, June 20, 2006 4:20 PM
You can't use
foreach
(ListViewItem item in listBA.Items)
because the CheckedListBox has no GetEnumerator()-Methode.

Any other suggestions?
Barthi  Tuesday, June 20, 2006 4:23 PM
No, only Equals(), getHashCode(), getType() and ToString().
Barthi  Tuesday, June 20, 2006 4:29 PM

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

Brendan Grant  Tuesday, June 20, 2006 4:34 PM
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.
Brendan Grant  Tuesday, June 20, 2006 4:34 PM
But what kind of objects? only strings?
Barthi  Tuesday, June 20, 2006 4:45 PM
Try this then;

ListViewItem item = (ListViewItem)listboxBAS.Items[indexBA];
string baBez = (string)item.Tag;
JRQ  Tuesday, June 20, 2006 5:01 PM
No, this doesn't work either.

"listBA.Items[indexBA]" only returns Strings
Barthi  Tuesday, June 20, 2006 5:06 PM
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

Barthi  Tuesday, June 20, 2006 5:19 PM

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.

Brendan Grant  Tuesday, June 20, 2006 6:18 PM
Genius, man!

Thanks a lot!!
Barthi  Tuesday, June 20, 2006 6:41 PM
It works for me too.
THANKS!
albaras  Thursday, October 16, 2008 12:41 PM

You can use google to search for other answers

Custom Search

More Threads

• Clickable elements
• How can I burn a CD using C#?
• Problem with Application.StartupPath in a particular UserControl
• ActiveDocumentHost
• LockScreen, get and change Keys.
• Webbrowser Displays Word docs ok but opens xls in Excel
• How to override the default HTTPHeaders in WebBrowser.Navigate(,,,) method
• Do Not Merge Menus of MDI & Child
• How to identify the particular item in a ListBox right-clicked
• C++ HWND -> .Net Control