• C#中tag作用


    MSDN里的一个解释:
    一个 Object,它包含有关控件的数据。
    Tag 属性的一个常见用途,是存储与控件密切关联的数据。例如,如果有一个显示客户信息的控件,则可以将包含客户订购历史的 DataSet 存储在该控件的 Tag 属性中,以便可以快速访问数据。
    你确实可以另外设一个变量做标记,用来存储如bool,string,int 等等。
    但你也可以把一些控件的相关数据存放在TAG中,之所以使用TAG,我认为是出于可以快速的访问与此控件相关的数据,或者说是因为你觉得临时的定义一个变量来存储那个控件的数据的话,有点麻烦,还不如将这个数据直接暂时
    储存在它的TAG属性中算了,免得去编写定义变量的代码.反正几乎每个控件都有TAG属性,你可以将相关控件的相关数据 存放在TAG中。

    ----------------------------------------------------------------------------------------------------------------------

    Control.Tag 属性 
    获取或设置包含有关控件的数据的对象。
    属性值
    类型:System.Object
    一个 Object,它包含有关控件的数据。默认为 null。
    下面的代码示例显示一个窗体并将 Customer 存储在其 Tag 属性中。该示例要求已经定义了一个从 Form 派生的、名为 CustomerForm 的类,并且已经定义了一个 Customer。
    C#
    private void buttonNewCustomer_Click(object sender, EventArgs e)
    {
    /* Create a new customer form and assign a new
    * Customer object to the Tag property. */
    CustomerForm customerForm = new CustomerForm();
    customerForm.Tag = new Customer();
    customerForm.Show();
    }

    -------------------------------------------------------------------------------------------------
    http://www.dotnetperls.com/tag

    The Tag property stores an object reference. Windows Forms programs can use object models of arbitrary complexity. But the Tag property is a simple way to link a certain object to a certain control. It is useful in certain situations.

    Property

    Example

    To start, this program shows the use of the Tag property inside the object model of the Windows Forms program. The Form1 constructor, which instantiates the control, assigns the Tag property to a new ExampleTag—a custom type.

    Then:This object reference can be accessed at any time and in any method later in the event loop of the Windows program.

    Program that uses Tag property: C#
    
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
    	public Form1()
    	{
    	    InitializeComponent();
    	    // Set tag on constructor.
    	    this.Tag = new ExampleTag(1, 2);
    	}
    
    	private void Form1_Load(object sender, EventArgs e)
    	{
    	    // When form loads, render tag to the title bar.
    	    this.Text = this.Tag.ToString();
    	}
        }
    
        class ExampleTag
        {
    	public int _a;
    	public int _b;
    	public ExampleTag(int a, int b)
    	{
    	    // Store the fields.
    	    this._a = a;
    	    this._b = b;
    	}
    	public override string ToString()
    	{
    	    // Write the fields to a string.
    	    return string.Format("Tag a = {0}, b = {1}", this._a, this._b);
    	}
        }
    }
    
    Output
    
    The window is displayed with the title bar reading the tag ToString output.

    New keyword, constructor invocation

    In the Form1 constructor, the ExampleTag constructor is called. It returns an ExampleTag object reference, which is copied to the Tag reference field. The ExampleTag remains in memory. It is pointed to by the Tag property's backing store.

    Next:In the Form1_Load event handler, we see that you can access the Tag property on the enclosing control.

    And:This is the same Tag that we previously set. We could also modify the ExampleTag object at this point.

    Object keyword

    The Form1_Load event accesses the Tag reference as the base type object. However, when you invoke the ToString method upon a base class, the most derived method is actually called, typically through a matrix data structure.

    ToString Method

    Thus:You can see that the title bar of the program is equal to the result of the ToString method.

    Object-oriented programming

    When planning a program, the object model is an important part of the program's architecture. If you use the Tag property too much, you will conflate the user interface specific information in the program with the data model.

    Caution:This makes portability of your program to new interfaces more difficult.

    Summary

    The C# programming language

    We explored an example usage of the Tag property in the Windows Forms widget layout system. The Tag property essentially provides a user-defined field that can store any form of object in a persistent way.

    And:It provides a convenient hook to your data. But it conflates user interface concepts and more abstract object models.

  • 相关阅读:
    mac前端解除端口占用命令
    Mac安装Brew
    Mac Webstrom 快捷键
    linux yarn安装
    js-新兴的API,最佳实践,离线应用于客户端存储
    js-高级技术
    js-Ajax与Comet
    js-错误处理与调试,JSON
    js-DOM2,表单脚本
    js-事件
  • 原文地址:https://www.cnblogs.com/managersi/p/3627163.html
Copyright © 2020-2023  润新知