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

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

若要以程式設計方式新增項目

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

    // 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. 使用 Items 屬性的 RemoveAtClear 方法。 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()
    
    

另請參閱