如何使用listView

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

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、大箐山ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的大箐山網(wǎng)站制作公司

一、基本使用: 

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("耗時", 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"); //耗時統(tǒng)計
 
listView.BeginUpdate();
listView.Items.Add(item);
listView.Items[listView.Items.Count - 1].EnsureVisible();//滾動到最后
listView.EndUpdate();

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

1.新建一個C# 類,命名為ListViewNF(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對應(yīng)的xxxx.Design.cs,將系統(tǒng)默認(rèn)生成的System.Windows.Forms.ListView改為ListViewNF即可。

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

實現(xiàn)代碼:

ListViewItem Item = new ListViewItem();
Item.SubItems.Clear();
.....相關(guān)其他代碼
this.listView1.Items.Add(Item);
Item.EnsureVisible(); //關(guān)鍵的實現(xià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增加點擊表頭事件:

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();//對列表進(jìn)行自定義排序 
}

以上就是如何使用listView,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞名稱:如何使用listView
網(wǎng)頁鏈接:http://muchs.cn/article42/pphoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站服務(wù)器托管、品牌網(wǎng)站制作網(wǎng)站制作、微信公眾號關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司