Solved

Extending ListBox Items

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

838 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