ListView控件演示01

此代码示例来自于微软的MSDN,在此基础进行了一些修改。

下面的代码示例创建一个 ListView 控件,其中带有三个指定的 ListViewItem 对象,
而这三个对象中的每一项又带有三个指定的 ListViewItem.ListViewSubItem 对象。
该示例还创建 ColumnHeader 对象以在详细信息视图中显示子项。
在代码示例中还创建两个 ImageList 对象,以便为 ListViewItem 对象提供图像。
这些 ImageList 对象被添加到 LargeImageList 和 SmallImageList 属性中。

运行程序前,先将Icons文件夹的图标赋值到C盘下。

 

程序截图:

 

程序代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Demo01 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void CreateListView() { // Create a new ListView control. ListView listView = new ListView(); listView.Bounds = new Rectangle(new Point(10, 10), new Size(300, 200)); // Set the view to show details. listView.View = View.Details; // Allow the user to edit item text. listView.LabelEdit = true; // Allow the user to rearrange columns. listView.AllowColumnReorder = true; // Display check boxes. listView.CheckBoxes = true; // Select the item and subitems when selection is made. listView.FullRowSelect = true; // Display grid lines. listView.GridLines = true; // Sort the items in the list in ascending order. listView.Sorting = SortOrder.Ascending; // Create three items and three sets of subitems for each item. ListViewItem item1 = new ListViewItem("item1", 0); // Place a check mark 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 mark 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. listView.Columns.Add("Item Column", -2, HorizontalAlignment.Left); listView.Columns.Add("Column 2", -2, HorizontalAlignment.Left); listView.Columns.Add("Column 3", -2, HorizontalAlignment.Left); listView.Columns.Add("Column 4", -2, HorizontalAlignment.Center); //Add the items to the ListView. listView.Items.AddRange(new ListViewItem[] { item1, item2, item3 }); // Create two ImageList objects. ImageList SmallImageList = new ImageList(); ImageList LargeImageList = new ImageList(); // Initialize the ImageList objects with bitmaps. SmallImageList.Images.Add(Bitmap.FromFile("C://Icons//Folder.gif")); SmallImageList.Images.Add(Bitmap.FromFile("C://Icons//Folder.gif")); LargeImageList.Images.Add(Bitmap.FromFile("C://Icons//Folder.gif")); LargeImageList.Images.Add(Bitmap.FromFile("C://Icons//Folder.gif")); //Assign the ImageList objects to the ListView. listView.LargeImageList = LargeImageList; listView.SmallImageList = SmallImageList; // Add the ListView to the control collection. this.Controls.Add(listView); } private void MainForm_Load(object sender, EventArgs e) { this.CreateListView(); } } }

 

 

你可能感兴趣的:(ListView,object,user,Class,imagelist,icons)