Share via


如何:使用 Windows Form ListView 控制項加入和移除項目

將專案新增至 Windows Forms ListView 控制項的程式主要是指定專案,並將屬性指派給它。 您可以隨時新增或移除清單專案。

以程式設計方式新增專案

  1. Add使用 屬性的 Items 方法。

    // Adds a new item with ImageIndex 3
    listView1.Items.Add("List item text", 3);
    
    
    ' Adds a new item with ImageIndex 3
    ListView1.Items.Add("List item text", 3)
    
    

以程式設計方式移除專案

  1. RemoveAt使用 屬性的 ItemsClear 方法。 方法 RemoveAt 會移除單一專案; Clear 方法會從清單中移除所有專案。

    // Removes the first item in the list.
    listView1.Items.RemoveAt(0);
    // Clears all the items.
    listView1.Items.Clear();
    
    
    ' Removes the first item in the list.
    ListView1.Items.RemoveAt(0)
    ' Clears all items:
    ListView1.Items.Clear()
    
    

另請參閱