Winforms中的Tag属性有什么用?

发布于 2024-09-25 21:41:16 字数 241 浏览 3 评论 0原文

我注意到带有控件的标签属性。是否可以使用它来引用我的自定义对象,或者我应该远离它,因为它需要装箱和拆箱,这已被提及为不安全且不建议。

TreeNode tn = new TreeNode();
CustClass o = new CustClass()
o.number = 123;
tn.Tag = o;

class CustClass
{
    public int number {get; set;}
}

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 unboxing which has been mentioned as unsafe and is not recommended.

TreeNode tn = new TreeNode();
CustClass o = new CustClass()
o.number = 123;
tn.Tag = o;

class CustClass
{
    public int number {get; set;}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心作怪 2024-10-02 21:41:16

Tag 属性的目的是让您可以将其用于您想要的任何目的。您可以安全地将任何您想要的东西存放在那里。

通常会声明一个专门用于存储在 Tag 属性中的类(就像您对 CustClass 所做的那样)。但如果你只需要其中一个值,那么直接在其中存储一个 int 并没有什么问题。

您在哪里读到装箱/拆箱是“不安全”的?这绝对不是真的。 (有些人声称它效率低,但事实并非如此。)此外,您的代码甚至根本不包含装箱示例。 CustClass 是一个引用类型。只有值类型在分配给 object 时才会被装箱。

The purpose of the Tag property is for you to use it for any purpose you want. You can safely store anything in there you want.

It is customary to declare a class that is specifically intended for being stored in the Tag property (like you did with your CustClass). But if you need only one value in it, then there is nothing wrong with storing an int in it directly.

Where did you read that boxing/unboxing is “unsafe”? That is absolutely not true. (Some people claim that it is inefficient, but even that is not true.) Furthermore, your code does not even contain an example of boxing at all. CustClass is a reference type. Only value types are boxed when assigned to object.

你与昨日 2024-10-02 21:41:16

我认为按照您想要的方式使用它不会成为装箱或拆箱问题。据我所知,该 Tag 属性仅供程序员使用,因此您可以按照您需要的方式使用它。

请参阅Control.Tag MSDN 上的属性,以进一步参考如何使用此属性。

例如,我曾经使用它在 Windows 窗体应用程序中向用户输入指令。当控件 GotFocus 事件触发时,指令 Label.Text 属性被分配了包含指令字符串的控件 Tag 属性的值。

I don't think this would be a boxing or unboxing problem to use it the way you want. As far as I know, that Tag property is for the programmer's purpose only, so you can use it quite the way you need it to be used.

Please see Control.Tag property on MSDN for further reference on how to use this property.

For instance, I used to use it to input instructions to the user in Windows Forms applications. When the control GotFocus event triggered, the instructions Label.Text property was assigned the value of my control Tag property which contained the instruction string.

柠檬心 2024-10-02 21:41:16

来自其他人也提到的链接:

https: //msdn.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx
其中写道:

从 Object 类派生的任何类型都可以分配给此类型
财产。如果通过 Windows 窗体设置 Tag 属性
设计器,只能指定文本。

Tag 属性的一个常见用途是存储紧密相连的数据。
与控制相关。例如,如果您有一个控件
显示有关客户的信息,您可以存储一个数据集
在该控件的 Tag 属性中包含客户的订单历史记录
因此可以快速访问数据。

以及 https://msdn.microsoft.com /en-us/library/system.windows.forms.treenode.tag.aspx 内容如下:

...示例创建一个根树节点来为其分配子树节点。一个
ArrayList 中每个 Customer 对象的子树节点被添加到
每个 Order 对象的根树节点以及子树节点
分配给客户对象。 Customer 对象被分配给
Tag 属性,代表 Customer 对象的树节点是
以橙色文本显示。此示例要求您有一个
定义了客户和订单对象,窗体上的 TreeView 控件,以及
一个名为 customerArray 的 ArrayList,其中包含 Customer 对象。

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.

月隐月明月朦胧 2024-10-02 21:41:16

好吧,您可以创建自己的 TreeNode 派生类:

class MyNode : TreeNode {
    public int number {get; set;}
}

但是,当您从树中检索节点时,您将进行强制转换,这比强制转换 Tag 属性没有任何改进。并且您应该重写 Clone() 方法。

一种更简洁的方法是利用 TreeNode.Name 并将其作为字典中的键来查找自定义数据。当 CustClass 变得不平凡时,这很好。 Name 属性不用于其他任何用途。

Well, you could create your own TreeNode derived class:

class MyNode : TreeNode {
    public int number {get; set;}
}

But then you'll be casting when you retrieve the node from the tree, no improvement over casting the Tag property. And you ought to override the Clone() method.

A cleaner approach is to leverage TreeNode.Name and make that a key in a dictionary to find your custom data back. Good when CustClass gets to be non-trivial. The Name property isn't used for anything else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文