如何使用listView

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

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、大箐山ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(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("耗時(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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

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

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

廣告

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

成都app開(kāi)發(fā)公司