溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用listView

發(fā)布時(shí)間:2021-09-13 11:33:15 來(lái)源:億速云 閱讀:257 作者:柒染 欄目:編程語(yǔ)言

本篇文章給大家分享的是有關(guān)如何使用listView,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

一、基本使用: 

listView.View = View.Details;//設(shè)置視圖
listView.SmallImageList = imageList;//設(shè)置圖標(biāo)

//添加列
listView.Columns.Add("本地路徑", 150, HorizontalAlignment.Left);
listView.Columns.Add("遠(yuǎn)程路徑", 150, HorizontalAlignment.Left);
listView.Columns.Add("上傳狀態(tài)", 80, HorizontalAlignment.Left);
listView.Columns.Add("耗時(shí)", 80, HorizontalAlignment.Left);

//添加行
var item = new ListViewItem();
item.ImageIndex = 1;
item.Text = name; //本地路徑
item.SubItems.Add(path); //遠(yuǎn)程路徑
item.SubItems.Add("ok"); //執(zhí)行狀態(tài)
item.SubItems.Add("0.5"); //耗時(shí)統(tǒng)計(jì)
 
listView.BeginUpdate();
listView.Items.Add(item);
listView.Items[listView.Items.Count - 1].EnsureVisible();//滾動(dòng)到最后
listView.EndUpdate();

二、動(dòng)態(tài)添加記錄,ListView不閃爍:

1.新建一個(gè)C# 類,命名為L(zhǎng)istViewNF(NF=Never/No Flickering)
2.復(fù)制如下代碼

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.修改你的WinForm對(duì)應(yīng)的xxxx.Design.cs,將系統(tǒng)默認(rèn)生成的System.Windows.Forms.ListView改為L(zhǎng)istViewNF即可。

三、動(dòng)態(tài)添加記錄,跳轉(zhuǎn)到最后行:

實(shí)現(xiàn)代碼:

ListViewItem Item = new ListViewItem();
Item.SubItems.Clear();
.....相關(guān)其他代碼
this.listView1.Items.Add(Item);
Item.EnsureVisible(); //關(guān)鍵的實(shí)現(xiàn)函數(shù)

四、點(diǎn)擊表頭實(shí)現(xiàn)排序:

1.增加自定義排序類:

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; //當(dāng)前列,0,1,2...,參數(shù)由ListView控件的ColumnClick事件傳遞  
        }

        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;
        }
    }
}

2.給ListView增加點(diǎn)擊表頭事件:

private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
    if (listView.Columns[e.Column].Tag == null)
    {
        listView.Columns[e.Column].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);
    //指定排序器并傳送列索引與升序降序關(guān)鍵字  
    listView.Sort();//對(duì)列表進(jìn)行自定義排序 
}

以上就是如何使用listView,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI