Click here to Skip to main content
Licence CPOL
First Posted 17 Jun 2007
Views 10,888
Bookmarked 7 times

The miracle of Tag

By | 17 Jun 2007 | Article
How to use the Tag property.

Introduction

Not a lot of people realize how powerful the Tag property actually is... (For people coming from other languages like Delphi where the Tag property is a String, using the Tag as an Object opens lots of opportunities.)

I will give you a quick overview and show you how you can use it.

Using the code

The Tag property is of type Object, making it a very powerful to work with. An Object is like the seed of OO.

Here is an example of how you can use it:

First, you will need to create a class with all the details you want to save.

public class Person
{
    public string Name;
    public string Surname;
}

Then, you populate it and link it to any Tag property. As easy as that:

// Create an temporary Person object
Person TempPerson = new Person();

// Populate it
TempPerson.Name = "Jack";
TempPerson.Surname = "The-Man!!!";

//Link the Temp object to the Tag
this.Tag = TempPerson;

// Get the Data back from the Tag
this.Text = 
    ((Person)this.Tag).Name + 
    " " +
    ((Person)this.Tag).Surname;

With the above code, I use the the Form (this) and link an object of any kind to the Tag property. Very handy!!!

In your day to day apps, you can use this to save limitless data on any Tag property (create a Node Index for example, and or link all the info you may need to a Tree Node.)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Bertus Kruger

Web Developer

New Zealand New Zealand

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNot a very good example PinmemberRobert Rohde3:07 18 Jun '07  
GeneralRe: Not a very good example PinmemberBertus Kruger9:47 18 Jun '07  
GeneralRe: Not a very good example [modified] Pinmembertaras_b18:45 19 Jun '07  
Yep,
 
I agree. Using tags is especially beneficial when working with collection items (g.e. ListBox, TreeView).
 
Imagine you have a collection of the objects that may be browsed/edited one by one within a form (using ListBox for navigation).
 
With traditional approach you have to:
 
- Declare member variable of List(T) dataList; with the data items.
 
- Insert dataList's item.ToString() in the ListBox.
 
- Now on user selection in the ListBox you need to populate some form controls to allow editing current item properties. In order to do this you need to obtain current selectionIndex and apply it to the dataList.
 
- When user edits item property you need to save the result back to the dataList. Again you need to use selectionIndex.
 
- When user adds data you need to add new item to the ListBox and add new item to the dataList.
 
- When user removes data you need to delete ListBox item and remove corresponding item from the dataList. And again you need to use selectionIndex.
 
- Sorting is even worse. You need to reorder both ListBox and dataList.
 
All this can be much simpler if ListBox.Item.Tag is used. No pain of using selectionIndex every time. No danger of desynchronisation of two collection. And basically less code.

 

 

 
-- modified at 4:06 Wednesday 20th June, 2007
GeneralRe: Not a very good example PinmemberBertus Kruger20:34 19 Jun '07  
QuestionUsage? Pinmemberandre123451:32 18 Jun '07  
AnswerRe: Usage? PinmemberBertus Kruger9:50 18 Jun '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 18 Jun 2007
Article Copyright 2007 by Bertus Kruger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid