Solved

Extending ListBox Items

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

809 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