What use is the Tag property in .net

后端 未结 4 1737
暗喜
暗喜 2020-12-09 04:13

I have noticed the Tag properties with controls. Is it okay to use this to reference my custom objects, or should I stay away from it as it would require boxing and unboxin

4条回答
  •  [愿得一人]
    2020-12-09 04:22

    From links also mentioned by others here:

    at https://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx one reads:

    Any type derived from the Object class can be assigned to this property. If the Tag property is set through the Windows Forms designer, only text can be assigned.

    A common use for the Tag property is to store data that is closely associated with the control. For example, if you have a control that displays information about a customer, you might store a DataSet that contains the customer's order history in that control's Tag property so the data can be accessed quickly.

    and at https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx one reads:

    ...example creates a root tree node to assign child tree nodes to. A child tree node for each Customer object in an ArrayList is added to the root tree node as well as a child tree node for each Order object assigned to the Customer object. The Customer object is assigned to the Tag property, and the tree nodes representing Customer objects are displayed with Orange text. This example requires that you have a Customer and Order object defined, a TreeView control on a Form, and an ArrayList named customerArray that contains Customer objects.

提交回复
热议问题