将项添加到 Windows 窗体 ListView 控件的过程主要包括:指定项以及为其指派属性。可在任何时候添加或移除列表项。在设计器中添加或移除项 在“属性”窗口中,单击 Items 属性旁的省略号按钮 (...)。 出现“ListViewItem 集合编辑器”。 若要添加项,请单击“添加”按钮。然后可设置新项的属性,如 Text 和 ImageIndex 属性。若要移除某项,请选择该项并单击“移除”按钮。 以编程方式添加项 使用 Items 属性的 Add 方法。 ' Visual Basic ' Adds a new item with ImageIndex 3 ListView1.Items.Add("List item text", 3)// C# // Adds a new item with ImageIndex 3 listView1.Items.Add("List item text", 3);// C++ // Adds a new item with ImageIndex 3 listView1->Items->Add(S"List item text", 3); 以编程方式移除项 使用 Items 属性的 RemoveAt 或 Clear 方法。RemoveAt 方法移除一个项,而 Clear 方法移除列表中的所有项。 ' Visual Basic ' Removes the first item in the list. ListView1.Items.RemoveAt(0) ' Clears all items: ListView1.Items.Clear()// C# // Removes the first item in the list. listView1.Items.RemoveAt(0); // Clears all the items. listView1.Items.Clear();// C++ // Removes the first item in the list. listView1->Items->RemoveAt(0); // Clears all the items. listView1->Items->Clear();
Windows 窗体 ListView 控件在位于“Details”视图中时,可为每个列表项显示多列。可使用这些列显示关于各个列表项的若干种信息。例如,文件列表可显示文件名、文件类型、大小和上次修改该文件的日期。有关如何在创建列后对其进行填充的信息,请参见使用 Windows 窗体 ListView 控件在列中显示子项。 在设计器中添加列 将控件的 View 属性设置为 Details。 在“属性”窗口中,单击 Columns 属性旁的省略号按钮 ()。 出现“ColumnHeader 集合编辑器”。 使用“添加”按钮添加新列。然后可以选择列标头并设置其文本(列的标题)、文本对齐方式和宽度。 以编程方式添加列 将控件的 View 属性设置为 Details。 使用列表视图的 Columns 属性的 Add 方法。 ' Visual Basic ' Set to details view ListView1.View = View.Details ' Add a column with width 20 and left alignment ListView1.Columns.Add("File type", 20, HorizontalAlignment.Left)// C# // Set to details view. listView1.View = View.Details; // Add a column with width 20 and left alignment. listView1.Columns.Add("File type", 20, HorizontalAlignment.Left);// C++ // Set to details view. listView1->View = View::Details; // Add a column with width 20 and left alignment. listView1->Columns->Add(S"File type", 20, HorizontalAlignment::Left);
Windows 窗体 ListView 控件可显示三个图像列表中的图标。“List”视图、“Details”视图和“SmallIcon”视图显示在 SmallImageList 属性中指定的图像列表中的图像。“LargeIcon”视图显示在 LargeImageList 属性中指定的图像列表中的图像。列表视图还能在大图标或小图标旁显示在 StateImageList 属性中设置的一组附加图标。有关图像列表的更多信息,请参见 Windows 窗体 ImageList 组件介绍和使用 Windows 窗体 ImageList 组件添加或移除图像。 在列表视图中显示图像 将适当的属性 SmallImageList、LargeImageList 或 StateImageList 设置为希望使用的现有 ImageList 组件。 这些属性可在设计器中使用“属性”窗口设置,也可在代码中设置。 ' Visual Basic ListView1.SmallImageList = ImageList1// C# listView1.SmallImageList = imageList1;// C++ listView1->SmallImageList = imageList1; 为每个具有关联图标的列表项设置 ImageIndex 或 StateImageIndex 属性。 这些属性可使用代码或在“ListViewItem 集合编辑器”中进行设置。若要打开“ListViewItem 集合编辑器”,请单击“属性”窗口中 Items 属性旁的省略号按钮 ()。 ' Visual Basic ' Sets the first list item to display the 4th image ListView1.Items(0).ImageIndex = 3// C# // Sets the first list item to display the 4th image listView1.Items[0].ImageIndex = 3;// C++ // Sets the first list item to display the 4th image listView1->Items->Item[0]->ImageIndex = 3;
private void CreateMyListView() { // Create a new ListView control. ListView listView1 = new ListView(); listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200)); // Set the view to show details. listView1.View = View.Details; // Allow the user to edit item text. listView1.LabelEdit = true; // Allow the user to rearrange columns. listView1.AllowColumnReorder = true; // Display check boxes. listView1.CheckBoxes = true; // Select the item and subitems when selection is made. listView1.FullRowSelect = true; // Display grid lines. listView1.GridLines = true; // Sort the items in the list in ascending order. listView1.Sorting = SortOrder.Ascending;
// Create three items and three sets of subitems for each item. ListViewItem item1 = new ListViewItem("item1",0); // Place a check next to the item. item1.Checked = true; item1.SubItems.Add("1"); item1.SubItems.Add("2"); item1.SubItems.Add("3"); ListViewItem item2 = new ListViewItem("item2",1); item2.SubItems.Add("4"); item2.SubItems.Add("5"); item2.SubItems.Add("6"); ListViewItem item3 = new ListViewItem("item3",0); // Place a check next to the item. item3.Checked = true; item3.SubItems.Add("7"); item3.SubItems.Add("8"); item3.SubItems.Add("9"); // Create columns for the items and subitems. listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left); listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left); listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left); listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center); //Add the items to the ListView. listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3}); // Create two ImageList objects. ImageList imageListSmall = new ImageList(); ImageList imageListLarge = new ImageList(); // Initialize the ImageList objects with bitmaps. imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp")); imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp")); imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp")); imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp")); //Assign the ImageList objects to the ListView. listView1.LargeImageList = imageListLarge; listView1.SmallImageList = imageListSmall; // Add the ListView to the control collection. this.Controls.Add(listView1); }
出现“ListViewItem 集合编辑器”。 若要添加项,请单击“添加”按钮。然后可设置新项的属性,如 Text 和 ImageIndex 属性。若要移除某项,请选择该项并单击“移除”按钮。
以编程方式添加项 使用 Items 属性的 Add 方法。
' Visual Basic
' Adds a new item with ImageIndex 3
ListView1.Items.Add("List item text", 3)// C#
// Adds a new item with ImageIndex 3
listView1.Items.Add("List item text", 3);// C++
// Adds a new item with ImageIndex 3
listView1->Items->Add(S"List item text", 3);
以编程方式移除项 使用 Items 属性的 RemoveAt 或 Clear 方法。RemoveAt 方法移除一个项,而 Clear 方法移除列表中的所有项。
' Visual Basic
' Removes the first item in the list.
ListView1.Items.RemoveAt(0)
' Clears all items:
ListView1.Items.Clear()// C#
// Removes the first item in the list.
listView1.Items.RemoveAt(0);
// Clears all the items.
listView1.Items.Clear();// C++
// Removes the first item in the list.
listView1->Items->RemoveAt(0);
// Clears all the items.
listView1->Items->Clear();
在设计器中添加列
将控件的 View 属性设置为 Details。
在“属性”窗口中,单击 Columns 属性旁的省略号按钮 ()。
出现“ColumnHeader 集合编辑器”。
使用“添加”按钮添加新列。然后可以选择列标头并设置其文本(列的标题)、文本对齐方式和宽度。
以编程方式添加列
将控件的 View 属性设置为 Details。
使用列表视图的 Columns 属性的 Add 方法。
' Visual Basic
' Set to details view
ListView1.View = View.Details
' Add a column with width 20 and left alignment
ListView1.Columns.Add("File type", 20, HorizontalAlignment.Left)// C#
// Set to details view.
listView1.View = View.Details;
// Add a column with width 20 and left alignment.
listView1.Columns.Add("File type", 20, HorizontalAlignment.Left);// C++
// Set to details view.
listView1->View = View::Details;
// Add a column with width 20 and left alignment.
listView1->Columns->Add(S"File type", 20, HorizontalAlignment::Left);
在列表视图中显示图像
将适当的属性 SmallImageList、LargeImageList 或 StateImageList 设置为希望使用的现有 ImageList 组件。
这些属性可在设计器中使用“属性”窗口设置,也可在代码中设置。
' Visual Basic
ListView1.SmallImageList = ImageList1// C#
listView1.SmallImageList = imageList1;// C++
listView1->SmallImageList = imageList1;
为每个具有关联图标的列表项设置 ImageIndex 或 StateImageIndex 属性。
这些属性可使用代码或在“ListViewItem 集合编辑器”中进行设置。若要打开“ListViewItem 集合编辑器”,请单击“属性”窗口中 Items 属性旁的省略号按钮 ()。
' Visual Basic
' Sets the first list item to display the 4th image
ListView1.Items(0).ImageIndex = 3// C#
// Sets the first list item to display the 4th image
listView1.Items[0].ImageIndex = 3;// C++
// Sets the first list item to display the 4th image
listView1->Items->Item[0]->ImageIndex = 3;
{
// Create a new ListView control.
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200)); // Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9"); // Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center); //Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3}); // Create two ImageList objects.
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList(); // Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp")); //Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge;
listView1.SmallImageList = imageListSmall; // Add the ListView to the control collection.
this.Controls.Add(listView1);
}