Adding Property To Checked ListBox Items (C#) 500pts

I want to make a custom control that inherits from the standard CheckedListBox control. The only thing I want to change is that I want to give the Items a "tag" property, similar to the tag property found in the items of a TreeView control. I am new to custom controls, is there a way to simply add a (string) tag property to the "Item" fairly easily? I need this control to be portable, so all code should reside within the control itself. I am programming in C#. Thanks.
Pretzel_JesusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
The "tag" property of the items for TreeView items are on the items themselves, not the collection that holds them. You only need to ensure that the items you are adding to your Items collection on your CheckedListBox have tag properties, which most GUI components do, I believe. If you are going to be adding custom controls to your CheckedListBox, then you need to expose a Tag property on your custom control. The Items property of CheckedListBox houses items which are type Object, so in order to access the Tag property, you are going to have to cast each item as you access it to the type of object it is.
0
käµfm³d 👽Commented:
"Custom control" should actually say "custom object".
0
Pretzel_JesusAuthor Commented:
Hi,

As I had mentioned, I am fairly new to custom controls. What I want is a special control that I can use which inherits the standard checkedlistbox control. Each item added needs to have an additional property that I want to call TAG and it houses a string. The standard checkedlistbox control does not allow this to happen. Can you please provide some code explaining how this can be accomplished?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

käµfm³d 👽Commented:
And as I had mentioned, it is NOT the CheckedListBox that handles this--it is each individual item added to the CheckedListBox's Items collection. For a TreeView, the "Items" collection is actually called "Nodes." Nodes is a collection of TreeNode objects. TreeNode is the class which has a Tag property--TreeView does not add this property to each TreeNode; the TreeNode class was designed to have a Tag property.

What kind of objects will you be adding to your custom CheckedListBox?
0
Pretzel_JesusAuthor Commented:
String values only for the name of the item, and string values only for the tag.
0
käµfm³d 👽Commented:
No....  I mean what kind of objects will be in your Items collection for the CheckedListBox (e.g. TreeNode, ListViewItem, [a custom object], etc. )
0
Pretzel_JesusAuthor Commented:
I guess a listviewitem. Whatever object populated the standard checkedlistbox control.
0
käµfm³d 👽Commented:
Well a standard CheckedListBox's Items collection is populated with data structures of type OBJECT, so you can put whatever you want in there. If you are going to be putting ListViewItems in this CheckedListBox, then you are in luck--ListViewItem is already equipped with a Tag property. Here is a demonstration:
public Form1()
{
    ListViewItem item = new ListViewItem("Test text");

    InitializeComponent();

    item.Tag = "this is the tag";
    this.checkedListBox1.Items.Add(item);   
}

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    MessageBox.Show(((ListViewItem)this.checkedListBox1.Items[e.Index]).Tag.ToString());
}

Open in new window

Untitled.jpg
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.