Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use listView

Shulou Source: shulou.com Published: 2022-06-03 15:17:30 09月27日 Update

This article is about how to use listView. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

1. Basic use:

ListView.View = View.Details;// Settings View listView.SmallImageList = imageList;// Settings Icon / / add columns listView.Columns.Add ("Local path", 150, HorizontalAlignment.Left); listView.Columns.Add ("remote path", 150, HorizontalAlignment.Left); listView.Columns.Add ("upload status", 80, HorizontalAlignment.Left); listView.Columns.Add ("time consuming", 80, HorizontalAlignment.Left); / / add rows var item = new ListViewItem (); item.ImageIndex = 1 Item.Text = name; / / local path item.SubItems.Add (path); / / remote path item.SubItems.Add ("ok"); / / execution status item.SubItems.Add ("0.5"); / / time statistics listView.BeginUpdate (); listView.Items.Add (item); listView.Items [listView.Items.Count-1] .EnsureVisible (); / / scroll to the last listView.EndUpdate ()

Second, dynamically add records, ListView does not flicker:

1. Create a new C# class named ListViewNF (NF=Never/No Flickering)

two。 Copy the following code

Class ListViewNF: System.Windows.Forms.ListView {public ListViewNF () {/ / Activate double buffering this.SetStyle (ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); / / Enable the OnNotifyMessage event so we get a chance to filter out / / Windows messages before they get to the form's WndProc this.SetStyle (ControlStyles.EnableNotifyMessage, true) } protected override void OnNotifyMessage (Message m) {/ / Filter out the WM_ERASEBKGND message if (m.Msg! = 0x14) {base.OnNotifyMessage (m);}

3. Modify the xxxx.Design.cs corresponding to your WinForm and change the default System.Windows.Forms.ListView generated by the system to ListViewNF.

3. Add records dynamically and jump to the last line:

Implementation code:

ListViewItem Item = new ListViewItem (); Item.SubItems.Clear (); Related other codes this.listView1.Items.Add (Item); Item.EnsureVisible (); / / key implementation functions

4. Click the header to sort:

1. Add a custom sort class:

Using System;using System.Collections;using System.Windows.Forms;namespace Whir.Software.Framework.UI {public class ListViewSort: IComparer {private readonly int _ col; private readonly bool _ descK; public ListViewSort () {_ col = 0;} public ListViewSort (int column, object desc) {_ descK = (bool) desc; _ col = column / / at the top of the list, 0SubItems 1 col 2. Parameters are passed by the ColumnClick event of the ListView control} public int Compare (object x, object y) {int tempInt = String.CompareOrdinal (ListViewItem) x). SubItems [_ col] .text, ((ListViewItem) y). SubItems [_ col] .text) If (_ descK) {return-tempInt;} return tempInt;}

two。 Add click header events to ListView:

Private void listView_ColumnClick (object sender, ColumnClickEventArgs e) {if (listView.Columns [e.Column] .tag = = null) {listView.Columns.tag = true;} var tabK = (bool) listView.Columns [e.Column] .tag; listView.Columns [e.Column] .tag =! tabK; listView.ListViewItemSorter = new ListViewSort (e.Column, listView.Columns [e.Column] .tag) / / specify the sorter and send the column index and the ascending and descending keyword listView.Sort (); / / Custom sort a pair of lists} this is how to use listView. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Path sort code event key dynamic more state knowledge article header utility keyword function front column ascending order parameter icon that is work will Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Redmi Linux MySQL