Solved

Extending ListBox Items

Posted on 2004-08-03
3
2,794 Views
Last Modified: 2012-06-27
Hi,

Since ListBox can contain items of type object, I am trying to extend an item such that I can set its Tag property. Example:

ListBox1.Items[0].Tag = "1";
ListBox1.Items[0].Tag = "2";

I dont want to bound the ListBox for this approach. Any ideas on how it can be done?

Thanks, Nauman
0
Comment
Question by:nauman_ahmed
3 Comments
 
LVL 8

Accepted Solution

by:
rajaloysious earned 500 total points
ID: 11707837
public Class CLst
{
   private string m_strDisplayname ;
   public string m_Tag;

   public void new(string DisplayName , string Tag )
   {
       m_strDisplayname = DisplayName
       m_Tag = Tag
   }

   public overrides string ToString()
   {      
       return m_strDisplayname  + ", " + m_Tag;
    }
    public  string Tag
    {
        get
        {
            return m_Tag;
        }
        set
        {
      m_Tag=value ;
        }
    }

}

And my list box population is:
while dr.Read()
   ListBox1.Items.Add(New Company(dr.Item("displayname"), dr.Item("tag")));
   ListBox1.Items(0).Tag = "1"

You can change the constructor as you like. Only thing is that you should always have a ToString function in that class..

Good Luck
Cheers...

0
 
LVL 25

Author Comment

by:nauman_ahmed
ID: 11708732
Thanks rajaloysious...It worked. I modified it into the following:

public class CListItem
      {
            private string m_strDisplayname ;
            public string m_Tag;

            public CListItem(string DisplayName,string Tag)
            {
                  m_strDisplayname = DisplayName;
                  m_Tag = Tag;
            }

            public override string ToString()
            {
                  return m_strDisplayname;
            }
            public string Tag
            {
                  get{return m_Tag;}
                  set{m_Tag = value;}
            }


      }

Thanks, Nauman
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11708821
the tag field has a different usage usually ... the Tag property represents an object that you can associate with the ListItem ...

Instead of placing in a "0" and a "1" so that you can index back into an array or datarow ... you would put a reference to that item ...

Also the above code wont work as is it needs to be ...

( (Company) ListBox1.Items(0)).Tag = "1" ;

what I would suggest instead is very similar ..

public Class TaggedItem
{
   private string m_strDisplayname ;
   public object m_Tag;

   public void new(string DisplayName, object Tag)
   {
       m_strDisplayname = DisplayName ;
       m_Tag = Tag;
   }

   public overrides string ToString()
   {      
       return m_strDisplayname ;
    }
    public  object Tag
    {
        get
        {
            return m_Tag;
        }
        set
        {
     m_Tag=value ;
        }
    }

}

then lets say you wereusing an array...

for(int i=0;i<Array.Length;i++) {
    ListBox1.Items.Add(new TaggedItem(Array[i].SomeProperty, Array[i]));
}

this would allow you direct access to Array[i] via the tag field instead of having to then go look it up....

However this is not the general way of doing things ... the general way is simply to override the ToString() method on the object itself (no need for a tag since when you access selected Item you get the actual object)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now