Click here to Skip to main content
Licence CPOL
First Posted 17 Jun 2007
Views 10,877
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  
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
Web02 | 2.5.120517.1 | Last Updated 18 Jun 2007
Article Copyright 2007 by Bertus Kruger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid