C # ListView Usage Explained

Source: Internet
Author: User
First, the ListView class

1, the basic properties commonly used:

(1) Fullrowselect: Sets whether row selection mode. (False by default) Tip: This property is meaningful only in the details view.

(2) GridLines: Sets whether grid lines are displayed between rows and columns. (False by default) Tip: This property is meaningful only in the details view.

(3) Allowcolumnreorder: Sets whether the column headers can be dragged to change the order of the columns. (False by default) Tip: This property is meaningful only in the details view.

(4) View: Gets or sets how items are displayed in the control, including details, LargeIcon, List, SmallIcon, Tile (default = LargeIcon)

(5) MultiSelect: Sets whether multiple items can be selected. (default = False)

(6) HeaderStyle: Gets or sets the column header style.

Clickable: The column header acts like a button, and you can perform actions (such as sorting) when clicked.

Nonclickable: Column headers do not respond to mouse clicks.

None: Column headers are not displayed.

(7) LabelEdit: Sets whether the user can edit the label of items in the control, and for the detail view, only the contents of the first column of the row are edited. (default = False)

(8) Checkboxes: Sets whether the check box is displayed next to items in the control. (default = False)

(9) LargeImageList: Large icon set. Tip: Use only in the LargeIcon view.

(Ten) SmallImageList: Small icon set. Tip: Use only in the SmallIcon view.

(one) StateImageList: Image mask. These image masks can be used as overlays for largeimagelist and smallimagelist images that indicate the application-defined state of an item. (Not quite understand)

SelectedItems: Gets the item selected in the control.

CheckedItems: Gets the item selected by the current check box in the control.

Soritng: Sorts items in a list view. (Default to None)

Ascending: Items are sorted in ascending order.

Descending: Items are sorted in descending order.

None: The item is not sorted.

(scrollable): Sets whether scroll bars are displayed when there is not enough space to display all items. (Default is True)

() Hoverselection: Sets whether items are automatically selected when the mouse pointer hovers over the item. (default = False)

HotTracking: Sets whether its appearance becomes a hyperlink when the mouse pointer passes through the item text. (default = False)

HideSelection: Sets whether the selected item is still highlighted when the control has no focus. (default = False)

Showgroups: Sets whether items are displayed in groups. (the default is false);

Groups: Sets the grouped object collection.

TopItem: Gets or sets the first visible item in a control that can be used for positioning. (Effect similar to EnsureVisible method)

2. Common methods:

(1) BeginUpdate: Avoid describing the control before calling the EndUpdate method. When you insert large amounts of data, you can effectively avoid flicker in the control and can greatly increase the speed.

(2) EndUpdate: After the BeginUpdate method hangs the description, continue to describe the list view control. (End update)

(3) EnsureVisible: The list view scrolls to the option line of the specified index entry. (Effect similar to Topitem property)

(4) FindItemWithText: Finds the first ListViewItem that begins with a given text value.

(5) FindNearestItem: Finds the next item from a given point, according to the specified search direction. Tip: You can use this method only in the LargeIcon or SmallIcon view.

3. Common events:

(1) AfterLabelEdit: Occurs when the user finishes editing the label of an item, the LabelEdit property is required to be true.

(2) BeforeLabelEdit: Occurs when the user starts editing the item's label.

(3) ColumnClick: Occurs when a user clicks on a column header in a list view control.

Two, five views of the ListView:

1. LargeIcon: Each item is displayed as a maximized icon with a label underneath it. (See results)

2, SmallIcon: Each item is shown as a small icon, on its right with a label. (See results)

3, List: Each item is displayed as a small icon, on its right with a label. The items are arranged in a column with no column headers. (See results)

4, Details: can display any column, but only the first column can contain a small icon and label, the other column items can only display text information, there is a list header. (See results)

5. Tile: Each item is displayed as a full-sized icon with item labels and subkey information on its right. (Only Windows XP and Windows Server 2003 series support)

①details View:

This.listView1.SmallImageList = This.imagelist1; Binds the icon set of a ListView to ImageList1

(1) List header creation (remember, you need to create a list header first)

ColumnHeader  ch= New ColumnHeader ();  Nbsp;ch. Text = "column header 1";   Set column header  Ch. Width = +;    Set column width  ch. TextAlign = HorizontalAlignment.Left;   Sets the alignment of the column  THIS.LISTVIEW1.COLUMNS.ADD (CH);    Adds a column header to the ListView control.
ColumnHeader  ch= New ColumnHeader ();    Ch. Text = "column header 1";   Set column header    Ch. Width = +;    Set column width    ch. TextAlign = HorizontalAlignment.Left;   Sets the alignment of the column    THIS.LISTVIEW1.COLUMNS.ADD (CH);    Adds a column header to the ListView control.

Or

THIS.LISTVIEW1.COLUMNS.ADD ("column Heading 1",  HorizontalAlignment.Left);//one-step add
THIS.LISTVIEW1.COLUMNS.ADD ("column Heading 1",  HorizontalAlignment.Left);//one-step add

(2) Adding data items

This.listView1.BeginUpdate ();   Data updates, the UI is temporarily suspended until EndUpdate draws the control, which effectively avoids flicker and greatly increases load speed for  (int i = 0; i < i++)   //Add 10 rows of data {      ListViewItem LVI = new ListViewItem ();       Lvi. ImageIndex = i;     By binding with ImageList, the ImageList I icon Lvi is displayed       . Text = "subitem" + i;       Lvi. SubItems.Add ("2nd Column, section" +i+ "line");       Lvi. SubItems.Add ("3rd Column, section" +i+ "line");       THIS.LISTVIEW1.ITEMS.ADD (LVI); }  this.listView1.EndUpdate ();  Ends data processing, and the UI interface is drawn once.
This.listView1.BeginUpdate ();   Data updates, the UI hangs temporarily until EndUpdate draws the control, which effectively avoids flicker and greatly increases load speed for    (int i = 0; i < i++)   //Add 10 rows of data  {      ListViewItem LVI = new ListViewItem ();        Lvi. ImageIndex = i;     By binding with ImageList, the ImageList I icon Lvi is displayed        . Text = "subitem" + i;        Lvi. SubItems.Add ("2nd Column, section" +i+ "line");        Lvi. SubItems.Add ("3rd Column, section" +i+ "line");        THIS.LISTVIEW1.ITEMS.ADD (LVI);  }    This.listView1.EndUpdate ();  Ends data processing, and the UI interface is drawn once.

(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);       } }
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)  //checked item traversal {       listView1.Items.RemoveAt (LVI. Index); Remove//listview1.items.remove by index       (LVI);   Remove by Item}
foreach (ListViewItem lvi in Listview1.selecteditems)  //checked item traversal  {        listView1.Items.RemoveAt (LVI. Index); Remove//listview1.items.remove by index        (LVI);   Remove by Item  }

(5) Line height setting (implemented with ImageList)

ImageList imglist = new ImageList ();  Imglist.imagesize = new Size (1, 20);//Set row height 20//is width and height  listview1.smallimagelist = imglist; This sets the smallimagelist of the ListView and imglist it with a large
ImageList imglist = new ImageList ();    Imglist.imagesize = new Size (1, 20);//Set row height 20//is width and height    listview1.smallimagelist = imglist; This sets the smallimagelist of the ListView and imglist it with a large

(6) Clear

This.listView1.Clear ();  Removes all items and columns (including the list header) from the control.  this.listView1.Items.Clear ();  Only all items are removed.
This.listView1.Clear ();  Removes all items and columns (including the list header) from the control.    this.listView1.Items.Clear ();  Only all items are removed.

Operating effect:

②largeicon View:

This.listView1.View = View.LargeIcon;  This.listView1.LargeImageList = This.imagelist2;  This.listView1.BeginUpdate ();  for (int i = 0; i < i++) {       ListViewItem LVI = new ListViewItem ();        Lvi. ImageIndex = i;        Lvi. Text = "item" + I;        THIS.LISTVIEW1.ITEMS.ADD (LVI); }  this.listView1.EndUpdate ();
This.listView1.View = View.LargeIcon;    This.listView1.LargeImageList = This.imagelist2;    This.listView1.BeginUpdate ();     for (int i = 0; i < i++)   {        ListViewItem LVI = new ListViewItem ();          Lvi. ImageIndex = i;          Lvi. Text = "item" + I;          THIS.LISTVIEW1.ITEMS.ADD (LVI);   }     This.listView1.EndUpdate ();

Operating effect:

③smallicon View:

This.listView1.View = View.SmallIcon;  this.listview1.smallimagelist= This.imagelist1;  This.listView1.BeginUpdate ();  for (int i = 0; i < i++) {      ListViewItem LVI = new ListViewItem ();        Lvi. ImageIndex = i;        Lvi. Text = "item" + I;        THIS.LISTVIEW1.ITEMS.ADD (LVI); }  this.listView1.EndUpdate ();
This.listView1.View = View.SmallIcon;    this.listview1.smallimagelist= This.imagelist1;    This.listView1.BeginUpdate ();    for (int i = 0; i < i++)  {       ListViewItem LVI = new ListViewItem ();          Lvi. ImageIndex = i;          Lvi. Text = "item" + I;          THIS.LISTVIEW1.ITEMS.ADD (LVI);  }    This.listView1.EndUpdate ();

Operating effect:

④list View:

This.listView1.View = view.list;  this.listview1.smallimagelist= This.imagelist1;  This.listView1.BeginUpdate ();  for (int i = 0; i < i++) {      ListViewItem LVI = new ListViewItem ();       Lvi. ImageIndex = i;       Lvi. Text = "item" + I;       THIS.LISTVIEW1.ITEMS.ADD (LVI); }  this.listView1.EndUpdate ();
This.listView1.View = view.list;    this.listview1.smallimagelist= This.imagelist1;    This.listView1.BeginUpdate ();    for (int i = 0; i < i++)  {       ListViewItem LVI = new ListViewItem ();         Lvi. ImageIndex = i;         Lvi. Text = "item" + I;         THIS.LISTVIEW1.ITEMS.ADD (LVI);  }    This.listView1.EndUpdate ();

Operating effect:

Third, other applications

1. Group:

ListViewGroup MAN_LVG = new ListViewGroup (); Create a male group MAN_LVG.  Header = "Boy";  Sets the title of the group. MAN_LVG.   Name = "Man";  Sets the name of the group. MAN_LVG.   Headeralignment = HorizontalAlignment.Left; Sets the alignment of the group header text.  (default to left) ListViewGroup WOMEN_LVG = new ListViewGroup (); Create a female group WOMEN_LVG.  Header = "Girl"; WOMEN_LVG.  Name = "Women"; WOMEN_LVG.   Headeralignment = HorizontalAlignment.Center;    Group Title Center Align This.listView1.Groups.Add (MAN_LVG);   Add the Boys group to the ListView This.listView1.Groups.Add (WOMEN_LVG);  Add the Boys group to the ListView this.listView1.ShowGroups = true;       Remember to set the Showgroups property to True (the default is False), otherwise the group for (int i = 0; i < 5; i++) {ListViewItem LVI = new ListViewItem () is not displayed; Lvi.       ImageIndex = i; Lvi.       Text = "item" +I; Lvi.  ForeColor = Color.Blue; Sets the line color LVI.       SubItems.Add ("2nd Column, section" +i+ "line"); Lvi.       SubItems.Add ("3rd Column, section" +i+ "line"); MAN_LVG.   Items.Add (LVI); Group Add child//or LVI.  Group = MAN_LVG; Group Add subkey This.listView1.Items.Add (LVI); }
 ListViewGroup MAN_LVG = new ListViewGroup (); Create a male group MAN_LVG.  Header = "Boy";     Sets the title of the group. MAN_LVG.   Name = "Man";     Sets the name of the group. MAN_LVG.   Headeralignment = HorizontalAlignment.Left; Sets the alignment of the group header text.  (default to left) ListViewGroup WOMEN_LVG = new ListViewGroup (); Create a female group WOMEN_LVG.     Header = "Girl"; WOMEN_LVG.     Name = "Women"; WOMEN_LVG.   Headeralignment = HorizontalAlignment.Center;    Group Title Center Align This.listView1.Groups.Add (MAN_LVG);   Add the Boys group to the ListView This.listView1.Groups.Add (WOMEN_LVG);  Add the Boys group to the ListView this.listView1.ShowGroups = true; Remember to set the Showgroups property to True (the default is false), otherwise it will not show group for (int i = 0; i < 5; i++) {ListViewItem LVI = new Listviewit         EM (); Lvi.         ImageIndex = i; Lvi.         Text = "item" +I; Lvi.  ForeColor = Color.Blue; Sets the line color LVI.         SubItems.Add ("2nd Column, section" +i+ "line"); Lvi.         SubItems.Add ("3rd Column, section" +i+ "line"); MAN_LVG.   Items.Add (LVI); Group Add child//or LVI.  Group = MAN_LVG; Group Add subkey this.listview1.iTEMs.  ADD (LVI); }

Operating effect:

2. Find text (only the text that matches the prefix can be found and only the first one is found):

ListViewItem founditem= This.listView1.FindItemWithText (this.textbox1.text,true,0);    Parameter 1: the text to find; parameter 2: Whether the subkey is also to be found; Parameter 3: Start find location  if (founditem! = null) {     this.listView1.TopItem = Founditem;  Navigate to the item     founditem.forecolor = color.red;   }
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.