tags:

views:

213

answers:

5

What is the use of Tag property in Tree view control C#? How can we work with it?

+4  A: 

A common use for the Tag property is to store data that is closely associated with the control (from MSDN). Any type derived from the Object class can be assigned to this property.

Neil
+1  A: 

Every control that inherits from Control in winform has a Tag property where you can store metadata for later use, for example you can store database id in that property for every item and load data from database on tree node click

ArsenMkrt
+3  A: 

It's a cheap way of avoiding inheritance to add just one Property.

Jan Bannister
A: 

As ArsenMkrt said, every control that inherits from Windows.Forms.Control has the Tag property. This is of type System.Object, so you can store anything you want.

The idea of the Tag property probably comes from VB6, which also has this, but in VB6 it is limited to String values.

awe
A: 

When writing a UI, sooner or later you will find yourself handing an event in which you know the UI control that the event came from, but you also need to know what backing data that control is associated with. Usually, that problem can be solved with data binding, but not always. In the latter case, you can manually populate the Tag property with whatever you need to make the code work.

Christian Hayter