What is the purpose of the 'Tag' property of Delphi VCL components?

HMcG picture HMcG · Nov 6, 2009 · Viewed 13.6k times · Source

Is there any specific purpose for the 'Tag' property of Delphi VCL components? I have Googled a few examples using it as, for example, a 'color' property or using the value as a pointer address, but is it 'good practice' to use it, or is it considered 'bad practice' as it ties the program logic to the UI?

Answer

Carl Smotricz picture Carl Smotricz · Nov 6, 2009

The "tag" property is there as a "cargo container" for whatever you might want to do with it.

Something it's often used for is in event handlers when you have a lot of similar components sharing one event handler. The event handler can find its caller and then query its tag value to get some more information about what it's supposed to be acting on.

EDIT:

Example: A calculator app might tag the number buttons with their respective numbers... silly and incomplete example, but you get the idea. The event handler could then pull the number to add into the display and accumulator right out of the tag instead of having to go figure out which button is meant to do what.