Solved

Extending ListBox Items

Posted on 2004-08-03
3
2,793 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now