詳解C# winform ListView的基本操作
組圖的加載與導(dǎo)入
圖片存放的相對(duì)路徑/ 與exe存放在一個(gè)文件夾

為界面添加圖片組–組件

圖片下載路徑
鏈接:img_jb51.rar
// 組圖的加載與導(dǎo)入
Dictionary<int, string> dic = new Dictionary<int, string>(); // 保存 序號(hào)--文件路徑
private void LoadImgList()
{
string imgPath = @".\img"; // 文件夾路徑
if (Directory.Exists(imgPath)==false)
{
Debug.WriteLine("圖片文件夾不存在");
return;
}
// 獲取圖片路徑列表
string[] files = Directory.GetFiles(imgPath);
if (files.Length <= 0)
{
Debug.WriteLine("圖片不存在");
return;
}
this.largeList.Images.Clear();
this.smallList.Images.Clear();
int index = 0;
// 判斷文件后綴,讀取圖片,設(shè)置圖片
string[] fileType = { ".jpg", ".png" };
foreach (string fpath in files)
{
// 判斷文件后綴
if (fileType.Contains(Path.GetExtension(fpath)))
{
Image img = Image.FromFile(fpath);
string keyName = Path.GetFileNameWithoutExtension(fpath);
// 設(shè)置圖片
this.largeList.Images.Add(keyName, img);
this.smallList.Images.Add(keyName, img);
// 保存索引與圖片路徑
dic.Add(index, fpath);
index++;
}
}
// 設(shè)置圖片大小
this.largeList.ImageSize = new Size(50, 50);
this.smallList.ImageSize = new Size(20, 20);
}
為ListView添加內(nèi)容
private void SetDetailsData()
{
// 清空列表
lvList.Columns.Clear();
lvList.Items.Clear();
// 設(shè)置模式
lvList.View = View.Details; // 設(shè)置詳細(xì)視圖
// 列的添加
lvList.Columns.Add("文件名", 100, HorizontalAlignment.Left);
lvList.Columns.Add("創(chuàng)建日期", 150, HorizontalAlignment.Left);
lvList.Columns.Add("類型", 80, HorizontalAlignment.Left);
lvList.Columns.Add("大小", 60, HorizontalAlignment.Left);
// item 的添加
for (int i = 0; i < dic.Count; i++)
{
ListViewItem li = new ListViewItem();
li.ImageIndex = i; //設(shè)置圖片序號(hào)
// 設(shè)置文件名
li.Text = smallList.Images.Keys[i];
li.SubItems.Add(File.GetCreationTime(dic[i]).ToString()); // 創(chuàng)建時(shí)間
li.SubItems.Add(Path.GetExtension(dic[i]).ToString()); // 類型
long length = new FileInfo(dic[i]).Length; // 大小
li.SubItems.Add((length / 1024).ToString());
lvList.Items.Add(li);
}
}
ListView 初始化設(shè)置與加載
private void Form1_Load(object sender, EventArgs e)
{
// 顯示是否分組顯示
this.lvList.ShowGroups = false;
if (this.largeList == null || this.largeList.Images.Count <= 0)
{
return;
}
this.SetDetailsData(); // 設(shè)置數(shù)據(jù)
this.SetListGroup(); // 設(shè)置列表分組
this.SetItemGroup(); // 設(shè)置item所屬組
lvList.GridLines = true;
lvList.View = View.LargeIcon; // 設(shè)置顯示模式--為大圖模式
lvList.LargeImageList = this.largeList;//關(guān)聯(lián)大圖標(biāo)列表
lvList.SmallImageList = this.smallList;// 關(guān)聯(lián)小圖標(biāo)列表
}
分組設(shè)置
// 為列表設(shè)置分組設(shè)置,
Dictionary<string, string> dict = new Dictionary<string, string>(); // 記錄分組名與包含字符串
private void SetListGroup()
{
// 添加組
lvList.Groups.Clear();
lvList.Groups.Add(new ListViewGroup("花", HorizontalAlignment.Center));
lvList.Groups.Add(new ListViewGroup("動(dòng)物", HorizontalAlignment.Center));
lvList.Groups.Add(new ListViewGroup("人物", HorizontalAlignment.Center));
lvList.Groups.Add(new ListViewGroup("風(fēng)景", HorizontalAlignment.Center));
dict.Add("花", "04");
dict.Add("動(dòng)物", "01");
dict.Add("人物", "02");
dict.Add("風(fēng)景", "03");
}
// 設(shè)置item 所屬分組
private void SetItemGroup()
{
// 判斷每一項(xiàng)進(jìn)行分組
for (int i = 0; i < lvList.Items.Count; i++)
{
foreach (ListViewGroup lvg in lvList.Groups)
{
if (lvList.Items[i].Text.Substring(0, 2) == dict[lvg.Header])
{
lvList.Items[i].Group = lvg;
break;
}
}
}
}
完整代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace ListViewSt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.LoadImgList();
}
// 組圖的加載與導(dǎo)入
Dictionary<int, string> dic = new Dictionary<int, string>(); // 保存 序號(hào)--文件路徑
private void LoadImgList()
{
string imgPath = @".\img"; // 文件夾路徑
if (Directory.Exists(imgPath) == false)
{
Debug.WriteLine("圖片文件夾不存在");
return;
}
// 獲取圖片路徑列表
string[] files = Directory.GetFiles(imgPath);
if (files.Length <= 0)
{
Debug.WriteLine("圖片不存在");
return;
}
this.largeList.Images.Clear();
this.smallList.Images.Clear();
int index = 0;
// 判斷文件后綴,讀取圖片,設(shè)置圖片
string[] fileType = { ".jpg", ".png" };
foreach (string fpath in files)
{
// 判斷文件后綴
if (fileType.Contains(Path.GetExtension(fpath)))
{
Image img = Image.FromFile(fpath);
string keyName = Path.GetFileNameWithoutExtension(fpath);
// 設(shè)置圖片
this.largeList.Images.Add(keyName, img);
this.smallList.Images.Add(keyName, img);
// 保存索引與圖片路徑
dic.Add(index, fpath);
index++;
}
}
// 設(shè)置圖片大小
this.largeList.ImageSize = new Size(50, 50);
this.smallList.ImageSize = new Size(20, 20);
}
private void Form1_Load(object sender, EventArgs e)
{
// 顯示是否分組顯示
this.lvList.ShowGroups = false;
if (this.largeList == null || this.largeList.Images.Count <= 0)
{
return;
}
this.SetDetailsData(); // 設(shè)置數(shù)據(jù)
this.SetListGroup(); // 設(shè)置列表分組
this.SetItemGroup(); // 設(shè)置item所屬組
lvList.GridLines = true;
lvList.View = View.LargeIcon; // 設(shè)置顯示模式--為大圖模式
lvList.LargeImageList = this.largeList;//關(guān)聯(lián)大圖標(biāo)列表
lvList.SmallImageList = this.smallList;// 關(guān)聯(lián)小圖標(biāo)列表
}
// 大圖標(biāo)視圖
private void btnLarge_Click(object sender, EventArgs e)
{
this.lvList.View = View.LargeIcon;
}
// 小圖標(biāo)視圖
private void btnSmall_Click(object sender, EventArgs e)
{
this.lvList.View = View.SmallIcon;
}
// 列表視圖
private void btnList_Click(object sender, EventArgs e)
{
this.lvList.View = View.List;
}
// Tile 視圖 圖標(biāo)使用大圖標(biāo)
private void btnTile_Click(object sender, EventArgs e)
{
this.lvList.View = View.Tile;
}
// 詳細(xì)信息視圖 列
private void btnDetail_Click(object sender, EventArgs e)
{
lvList.View = View.Details; // 設(shè)置詳細(xì)視圖
}
// 添加item列表
private void SetDetailsData()
{
// 清空列表
lvList.Columns.Clear();
lvList.Items.Clear();
// 設(shè)置模式
lvList.View = View.Details; // 設(shè)置詳細(xì)視圖
// 列的添加
lvList.Columns.Add("文件名", 100, HorizontalAlignment.Left);
lvList.Columns.Add("創(chuàng)建日期", 150, HorizontalAlignment.Left);
lvList.Columns.Add("類型", 80, HorizontalAlignment.Left);
lvList.Columns.Add("大小", 60, HorizontalAlignment.Left);
// item 的添加
for (int i = 0; i < dic.Count; i++)
{
ListViewItem li = new ListViewItem();
li.ImageIndex = i; //設(shè)置圖片序號(hào)
// 設(shè)置文件名
li.Text = smallList.Images.Keys[i];
li.SubItems.Add(File.GetCreationTime(dic[i]).ToString()); // 創(chuàng)建時(shí)間
li.SubItems.Add(Path.GetExtension(dic[i]).ToString()); // 類型
long length = new FileInfo(dic[i]).Length; // 大小
li.SubItems.Add((length / 1024).ToString());
lvList.Items.Add(li);
}
}
// 為列表設(shè)置分組設(shè)置,
Dictionary<string, string> dict = new Dictionary<string, string>(); // 記錄分組名與包含字符串
private void SetListGroup()
{
// 添加組
lvList.Groups.Clear();
lvList.Groups.Add(new ListViewGroup("花", HorizontalAlignment.Center));
lvList.Groups.Add(new ListViewGroup("動(dòng)物", HorizontalAlignment.Center));
lvList.Groups.Add(new ListViewGroup("人物", HorizontalAlignment.Center));
lvList.Groups.Add(new ListViewGroup("風(fēng)景", HorizontalAlignment.Center));
dict.Add("花", "04");
dict.Add("動(dòng)物", "01");
dict.Add("人物", "02");
dict.Add("風(fēng)景", "03");
}
// 設(shè)置item 所屬分組
private void SetItemGroup()
{
// 判斷每一項(xiàng)進(jìn)行分組
for (int i = 0; i < lvList.Items.Count; i++)
{
foreach (ListViewGroup lvg in lvList.Groups)
{
if (lvList.Items[i].Text.Substring(0, 2) == dict[lvg.Header])
{
lvList.Items[i].Group = lvg;
break;
}
}
}
}
// 以分組形式顯示
private void btnGroupShow_Click(object sender, EventArgs e)
{
lvList.ShowGroups = true;
this.SetDetailsData();
this.SetItemGroup();
}
}
}
大圖標(biāo)模式顯示

Tile模式 顯示

分組顯示

到此這篇關(guān)于詳解C# winform ListView的基本操作的文章就介紹到這了,更多相關(guān)C# winform ListView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Jquery zTree實(shí)現(xiàn)樹狀結(jié)構(gòu)顯示 異步數(shù)據(jù)加載
這篇文章主要為大家詳細(xì)介紹了C#使用Jquery zTree實(shí)現(xiàn)樹狀結(jié)構(gòu)顯示和異步數(shù)據(jù)加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
C#結(jié)束Excel進(jìn)程的步驟教學(xué)
在本篇文章里小編給大家分享了關(guān)于C#結(jié)束Excel進(jìn)程的步驟教學(xué)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-01-01
C#8.0 中開啟默認(rèn)接口實(shí)現(xiàn)方法
這篇文章主要介紹了C#8.0 中開啟默認(rèn)接口實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧的相關(guān)資料2019-05-05
C# 通過反射初探ORM框架的實(shí)現(xiàn)原理(詳解)
下面小編就為大家分享一篇C# 通過反射初探ORM框架的實(shí)現(xiàn)原理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
C#實(shí)現(xiàn)下載網(wǎng)頁HTML源碼的方法
這篇文章主要介紹了C#實(shí)現(xiàn)下載網(wǎng)頁HTML源碼的方法,是一個(gè)非常實(shí)用的技巧,還包含了對(duì)于下載失敗的判斷等邏輯處理,需要的朋友可以參考下2014-09-09

