Solved

Extending ListBox Items

Posted on 2004-08-03
3
2,798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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