[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-03-29
8
Medium Priority
?
627 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:Pretzel_Jesus
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29040465
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29040511
"Custom control" should actually say "custom object".
0
 

Author Comment

by:Pretzel_Jesus
ID: 29042797
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29044136
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
 

Author Comment

by:Pretzel_Jesus
ID: 29047066
String values only for the name of the item, and string values only for the tag.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29049165
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
 

Author Comment

by:Pretzel_Jesus
ID: 29052053
I guess a listviewitem. Whatever object populated the standard checkedlistbox control.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 29052845
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question