ListViewItem.Tag 属性

获取或设置包含数据的对象以与该项关联。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(False)> _
<BindableAttribute(True)> _
Public Property Tag As Object
用法
Dim instance As ListViewItem
Dim value As Object

value = instance.Tag

instance.Tag = value
[LocalizableAttribute(false)] 
[BindableAttribute(true)] 
public Object Tag { get; set; }
[LocalizableAttribute(false)] 
[BindableAttribute(true)] 
public:
property Object^ Tag {
    Object^ get ();
    void set (Object^ value);
}
/** @property */
public Object get_Tag ()

/** @property */
public void set_Tag (Object value)
public function get Tag () : Object

public function set Tag (value : Object)

属性值

包含与该项相关联的信息的对象。

备注

Tag 属性可用于存储要与某项关联的任何对象。虽然您可以存储任何项,但 Tag 属性通常用于存储与项相关的字符串信息,例如唯一标识符或该项的数据在数据库中的索引位置。

示例

下面的代码示例演示如何初始化 ListViewItem 并设置 TagText 属性。若要运行此示例,请将以下代码放入一个包含名为 ListView1ListView 的窗体中,并从该窗体的构造函数或 Load 事件处理方法中调用 InitializeListViewItems

Private Sub InitializeListViewItems()
    ListView1.View = View.List
    Dim aCursor As Cursor

    Dim favoriteCursors() As Cursor = New Cursor() _
                {Cursors.Help, Cursors.Hand, Cursors.No, Cursors.Cross}

    ' Populate the ListView control with the array of Cursors.
    For Each aCursor In favoriteCursors

        ' Construct the ListViewItem object
        Dim item As New ListViewItem

        ' Set the Text property to the cursor name.
        item.Text = aCursor.ToString

        ' Set the Tag property to the cursor.
        item.Tag = aCursor

        ' Add the ListViewItem to the ListView.
        ListView1.Items.Add(item)
    Next
End Sub
private void InitializeListViewItems()
{
    ListView1.View = View.List;
    

    Cursor[] favoriteCursors = new Cursor[]{Cursors.Help, 
        Cursors.Hand, Cursors.No, Cursors.Cross};

    // Populate the ListView control with the array of Cursors.
    foreach ( Cursor aCursor in favoriteCursors )
    {

        // Construct the ListViewItem object
        ListViewItem item = new ListViewItem();

        // Set the Text property to the cursor name.
        item.Text = aCursor.ToString();

        // Set the Tag property to the cursor.
        item.Tag = aCursor;

        // Add the ListViewItem to the ListView.
        ListView1.Items.Add(item);
    }
}
private:
   void InitializeListViewItems()
   {
      ListView1->View = View::List;
      array<System::Windows::Forms::Cursor^>^favoriteCursors = {Cursors::Help,Cursors::Hand,Cursors::No,Cursors::Cross};
      
      // Populate the ListView control with the array of Cursors.
      System::Collections::IEnumerator^ myEnum = favoriteCursors->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         System::Windows::Forms::Cursor^ aCursor = safe_cast<System::Windows::Forms::Cursor^>(myEnum->Current);
         
         // Construct the ListViewItem object
         ListViewItem^ item = gcnew ListViewItem;
         
         // Set the Text property to the cursor name.
         item->Text = aCursor->ToString();
         
         // Set the Tag property to the cursor.
         item->Tag = aCursor;
         
         // Add the ListViewItem to the ListView.
         ListView1->Items->Add( item );
      }
   }
private void InitializeListViewItems()
{
    listView1.set_View(View.List);

    Cursor favoriteCursors[] = new Cursor[] { Cursors.get_Help(),
        Cursors.get_Hand(), Cursors.get_No(), Cursors.get_Cross() };
    // Populate the ListView control with the array of Cursors.
    for (int iCtr = 0; iCtr < favoriteCursors.length; iCtr++) {
        Cursor aCursor = favoriteCursors[iCtr];
        // Construct the ListViewItem object
        ListViewItem item = new ListViewItem();
        // Set the Text property to the cursor name.
        item.set_Text(aCursor.ToString());
        // Set the Tag property to the cursor.
        item.set_Tag(aCursor);
        // Add the ListViewItem to the ListView.
        listView1.get_Items().Add(item);
    }
} //InitializeListViewItems

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

ListViewItem 类
ListViewItem 成员
System.Windows.Forms 命名空间