What is the use of Tag property in Tree view control C#? How can we work with it?
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
It's a cheap way of avoiding inheritance to add just one Property.
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.
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.