C # usage of ListView
1. ListView class
1. Common basic attributes:
(1) FullRowSelect: Set whether to select the row mode. (False by default) Prompt: This attribute is valid only in the Details View.
(2) GridLines: Set whether to display grid lines between rows and columns. (False by default) Prompt: This attribute is valid only in the Details View.
(3) AllowColumnReorder: determines whether to drag the column header to change the column order. (False by default) Prompt: This attribute is valid only in the Details View.
(4) View: gets or sets the display mode of items in the control, including Details, LargeIcon, List, SmallIcon, and Tile (LargeIcon by default)
(5) MultiSelect: Set whether multiple items can be selected. (False by default)
(6) HeaderStyle: Get or set the column header style.
Clickable: the column header is similar to a button. You can perform operations (such as sorting) When you click it ).
NonClickable: the column header does not respond to mouse clicks.
None: the column header is not displayed.
(7) LabelEdit: sets whether the user can edit the label of items in the control. For the Detail View, only the content in the first column of the row can be edited. (False by default)
(8) CheckBoxes: Set whether to display the check box next to each item in the control. (False by default)
(9) LargeImageList: Large icon set. Tip: it is only used in the LargeIcon view.
(10) SmallImageList: small icon set. Tip: it is only used in the SmallIcon view.
(11) StateImageList: Image Mask. These image masks can be used as overwrites for LargeImageList and SmallImageList images that can be used to indicate the state defined by the application of the item. (I do not understand it yet)
(12) SelectedItems: gets the items selected in the control.
(13) CheckedItems: Get the items selected by the current check box in the control.
(14) Soritng: sorts the items in the list view. (The default value is None)
Ascending: items are sorted in Ascending order.
Descending: The items are sorted in Descending order.
None: The items are not sorted.
(15) Scrollable: sets whether to display the scroll bar when there is not enough space to display all items. (True by default)
(16) HoverSelection: Specifies whether to automatically select an item when the mouse pointer is hovering over an item. (False by default)
(17) HotTracking: determines whether the appearance of a mouse pointer changes to the form of a hyperlink when it passes through the item text. (False by default)
(18) HideSelection: sets whether the selected item is highlighted when the control has no focus. (False by default)
(19) ShowGroups: sets whether to display items in group mode. (The default value is false );
(20) Groups: Set the group object set.
(21) TopItem: gets or sets the first visible item in the control, which can be used for locating. (The effect is similar to the EnsureVisible method)
2. Common Methods:
(1) BeginUpdate: do not describe the control before calling the EndUpdate method. When a large amount of data is inserted, the control flash can be effectively avoided and the speed can be greatly improved.
(2) EndUpdate: Describes the List View control after the BeginUpdate method suspends the description. (End update)
(3) EnsureVisible: scroll to the option row of the specified index item in the List View. (The effect is similar to the TopItem attribute)
(4) FindItemWithText: searches for the first ListViewItem starting with a given text value.
(5) FindNearestItem: searches for the next item from the given point based on the specified search direction. Tip: This method can be used only in the LargeIcon or SmallIcon view.
3. Common events:
(1) AfterLabelEdit: The LabelEdit attribute is required to be true when the user edits the label of an item.
(2) BeforeLabelEdit: It occurs when the user starts to edit the tag of an item.
(3) ColumnClick: It occurs when you click the column header in the List View control.
Ii. Five Views of ListView:
1,LargeIcon: Each item is displayed as a maximize icon with a label under it. (For the effect, see)
2,SmallIcon: Each item is displayed as a small icon with a label on the right. (For the effect, see)
3,List: Each item is displayed as a small icon with a label on the right. Items are arranged in columns without column headers. (For the effect, see)
4,Details: Any column can be displayed, but only the first column can contain a small icon and label. Other column items can only display text information with a list header. (For the effect, see)
5,Tile: Each item is displayed as a full-size icon with the item label and item Information on the right side. (Only supported by Windows XP and Windows Server 2003 Series)
① Details View:
This. listView1.SmallImageList = this. imageList1; // bind the listView icon set to imageList1.
(1) create a list header (remember to create a list header first)
ColumnHeader ch= new ColumnHeader(); ch.Text = "Column Title 1" ; // Set the column title ch.Width = 120; // Set the column width ch.TextAlign = HorizontalAlignment.Left; // Set the column alignment this .listView1.Columns.Add(ch); // Add the column header to the ListView control. |
Or
this .listView1.Columns.Add( "Column Title 1" , 120, HorizontalAlignment.Left); // Add one step |
(2) Add data items
this .listView1.BeginUpdate(); // Data update, UI temporarily suspended until the EndUpdate draw control, can effectively avoid flickering and greatly increase the loading speed for ( int i = 0; i < 10; i++) // Add 10 rows of data { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = i; // Bind to imageList to display the I-entry icon in imageList lvi.Text = "subitem" + i; lvi.SubItems.Add( "Column 2nd, column" +i+ "Line" ); lvi.SubItems.Add( "Column 3rd, column" +i+ "Line" ); this .listView1.Items.Add(lvi); } this .listView1.EndUpdate(); // End the data processing and draw the UI at one time. |
(3) display items
foreach (ListViewItem item in this .listView1.Items) { for ( int i = 0; i < item.SubItems.Count; i++) { MessageBox.Show(item.SubItems[i].Text); } } |
(4) remove an item
foreach (ListViewItem lvi in listView1.SelectedItems) // Print the selected items { listView1.Items.RemoveAt(lvi.Index); // Remove by index // ListView1.Items. Remove (lvi); // Remove by item } |
(5) Row Height settings (implemented using imageList)
ImageList imgList = new ImageList(); imgList.ImageSize = new Size(1, 20); // Set the Row Height to 20 // width and height respectively listView1.SmallImageList = imgList; // Set the SmallImageList of listView here and use imgList to extend it |
(6) Clear
this .listView1.Clear(); // Remove all items and columns (including list headers) from the control ). this .listView1.Items.Clear(); // Only remove all items. |
Running effect:
② LargeIcon View:
this .listView1.View = View.LargeIcon; this .listView1.LargeImageList = this .imageList2; this .listView1.BeginUpdate(); for ( int i = 0; i < 10; i++) { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = i; lvi.Text = "item" + i; this .listView1.Items.Add(lvi); } this .listView1.EndUpdate(); |
Running effect:
③ SmallIcon View:
this .listView1.View = View.SmallIcon; this .listView1.SmallImageList= this .imageList1; this .listView1.BeginUpdate(); for ( int i = 0; i < 10; i++) { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = i; lvi.Text = "item" + i; this .listView1.Items.Add(lvi); } this .listView1.EndUpdate(); |
Running effect:
④ List View:
this .listView1.View = View.List; this .listView1.SmallImageList= this .imageList1; this .listView1.BeginUpdate(); for ( int i = 0; i < 10; i++) { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = i; lvi.Text = "item" + i; this .listView1.Items.Add(lvi); } this .listView1.EndUpdate(); |
Running effect:
3. Other applications
1. GROUP:
ListViewGroup man_lvg = new ListViewGroup(); // Create a boys' group man_lvg.Header = "Boys" ; // Set the group title. // Man_lvg.Name = "man"; // set the group name. man_lvg.HeaderAlignment = HorizontalAlignment.Left; // Set the group title text alignment. (Left by default) ListViewGroup women_lvg = new ListViewGroup(); // Create a girl group women_lvg.Header = "Girl" ; //women_lvg.Name = "women"; women_lvg.HeaderAlignment = HorizontalAlignment.Center; // Align group titles in the center this .listView1.Groups.Add(man_lvg); // Add the boys' group to listview. this .listView1.Groups.Add(women_lvg); // Add the boys' group to listview. this .listView1.ShowGroups = true ; // Remember to set the ShowGroups attribute to true (default value: false); otherwise, no group is displayed. for ( int i = 0; i < 5; i++) { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = i; lvi.Text = "item" +i; lvi.ForeColor = Color.Blue; // Set the row color lvi.SubItems.Add( "Column 2nd, column" +i+ "Line" ); lvi.SubItems.Add( "Column 3rd, column" +i+ "Line" ); man_lvg.Items.Add(lvi); // Add a subitem to a group // Or lvi. Group = man_lvg; // Add a subitem to a Group this .listView1.Items.Add(lvi); } |
Running effect:
2. Search for text (only texts matching the prefix can be found and only the first matching item can be found ):
ListViewItem foundItem= this .listView1.FindItemWithText( this .textBox1.Text, true ,0); // Parameter 1: The text to be searched; parameter 2: whether the subitem is also searched; parameter 3: start searching for the location if (foundItem != null ) { this .listView1.TopItem = foundItem; // Locate this item foundItem.ForeColor = Color.Red; } |