c# ListView




// Attempt to run the file.
System.Diagnostics.Process.Start(filename);
//folderCol 可以存放一个路径的 栈(用于返回功能的设计)
private System.Collections.Specialized.StringCollection folderCol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace ListView
{
public partial class Form1 : Form
{
private System.Collections.Specialized.StringCollection folderCol; public Form1()
{
InitializeComponent(); // Init ListView and folder collection
folderCol = new System.Collections.Specialized.StringCollection();
CreateHeadersAndFillListView();
PaintListView(@"C:\");
folderCol.Add(@"C:\"); } private void CreateHeadersAndFillListView()
{
ColumnHeader colHead; // First header
colHead = new ColumnHeader();
colHead.Text = "Filename";
listViewFilesAndFolders.Columns.Add(colHead); // Insert the header // Second header
colHead = new ColumnHeader();
colHead.Text = "Size";
listViewFilesAndFolders.Columns.Add(colHead); // Insert the header // Third header
colHead = new ColumnHeader();
colHead.Text = "Last accessed";
listViewFilesAndFolders.Columns.Add(colHead); // Insert the header
} private void PaintListView(string root)
{
try
{
// Two local variables that are used to create the items to insert
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi; // If there’s no root folder, we can’t insert anything.
if (string.IsNullOrEmpty(root))
return; // Get information about the root folder.
DirectoryInfo dir = new DirectoryInfo(root); // Retrieve the files and folders from the root folder.
DirectoryInfo[] dirs = dir.GetDirectories(); // Folders
FileInfo[] files = dir.GetFiles(); // Files // Clear the ListView. Note that we call the Clear method on the
// Items collection rather than on the ListView itself.
// The Clear method of the ListView remove everything, including column
// headers, and we only want to remove the items from the view.
listViewFilesAndFolders.Items.Clear(); // Set the label with the current path.
labelCurrentPath.Text = root; // Lock the ListView for updates.
listViewFilesAndFolders.BeginUpdate(); // Loop through all folders in the root folder and insert them.
foreach (DirectoryInfo di in dirs)
{
// Create the main ListViewItem.
lvi = new ListViewItem();
lvi.Text = di.Name; // Folder name
lvi.ImageIndex = ; // The folder icon has index 0
lvi.Tag = di.FullName; // Set the tag to the qualified path of the
// folder // Create the two ListViewSubItems.
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = ""; // Size—a folder has no size and so this column
// is empty
lvi.SubItems.Add(lvsi); // Add the subitem to the ListViewItem lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = di.LastAccessTime.ToString(); // Last accessed column
lvi.SubItems.Add(lvsi); // Add the subitem to the ListViewItem. // Add the ListViewItem to the Items collection of the ListView.
listViewFilesAndFolders.Items.Add(lvi);
} // Loop through all the files in the root folder.
foreach (FileInfo fi in files)
{
// Create the main ListViewItem.
lvi = new ListViewItem();
lvi.Text = fi.Name; // Filename
lvi.ImageIndex = ; // The icon we use to represent a folder has
// index 1.
lvi.Tag = fi.FullName; // Set the tag to the qualified path of the
// file. // Create the two subitems.
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.Length.ToString(); // Length of the file
lvi.SubItems.Add(lvsi); // Add to the SubItems collection lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.LastAccessTime.ToString(); // Last Accessed Column
lvi.SubItems.Add(lvsi); // Add to the SubItems collection // Add the item to the Items collection of the ListView.
listViewFilesAndFolders.Items.Add(lvi);
} // Unlock the ListView. The items that have been inserted will now
// be displayed.
listViewFilesAndFolders.EndUpdate();
}
catch (System.Exception err)
{
MessageBox.Show("Error: " + err.Message);
}
} private void listViewFilesAndFolders_ItemActivate(object sender, EventArgs e)
{
// Cast the sender to a ListView and get the tag of the first selected
// item.
System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
string filename = lw.SelectedItems[].Tag.ToString(); if (lw.SelectedItems[].ImageIndex != )
{
try
{
// Attempt to run the file.
System.Diagnostics.Process.Start(filename);
}
catch
{
// If the attempt fails we simply exit the method.
return;
}
}
else
{
// Insert the items.
PaintListView(filename);
folderCol.Add(filename);
}
} private void buttonBack_Click(object sender, EventArgs e)
{
if (folderCol.Count > )
{
PaintListView(folderCol[folderCol.Count - ].ToString());
folderCol.RemoveAt(folderCol.Count - );
}
else
PaintListView(folderCol[].ToString());
} private void radioButtonLargeIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.LargeIcon;
} private void radioButtonSmallIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.SmallIcon;
} private void radioButtonList_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.List;
} private void radioButtonDetails_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.Details;
} private void radioButtonTile_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.listViewFilesAndFolders.View = View.Tile;
}
}
}
c# ListView的更多相关文章
- 张高兴的 UWP 开发笔记:横向 ListView
ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...
- Android—万能ListView适配器
ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...
- Android—ListView条目背景为图片时,条目间距问题解决
ListView是android开发中使用最普遍的控件了,可有的listView条目的内容颇为丰富,甚至为了美观,背景用指定图片,如下图:
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- listview下拉刷新和上拉加载更多的多种实现方案
listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局 android系统为listview提供了addfootview ...
- Android listview和gridview以及view的区别
GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...
- mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context
需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...
- 【腾讯Bugly干货分享】跨平台 ListView 性能优化
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...
- android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)
第三节(2):常用控件之ViewPager.日期时间相关.ListView 一.ViewPager 实例:结合PagerAdapter滑动切换图片 二.日期时间相关:AnalogClock\Dig ...
- 父ListView嵌套子ListView时点击事件没有响应
转发请备注出处:http://www.cnblogs.com/LT5505/p/5972999.html 问题: 在ListView中嵌套ListView之后,子ListView会把父ListView ...
随机推荐
- NSBundle 类
NSBundle NSBundle继承于NSObject,NSBundle是一个程序包,其中包含了程序会使用的资源(图像,声音,编辑好的代码,nib文件). 一. 初始化NSBundle + (ins ...
- mongo db 使用方法
1 下载 mogodb http://www.mongodb.org/display/DOCS/Downloads 2 打开服务 我安装在e盘下了 可以指定数据文件位置 到 E:\mongoDB\mo ...
- 二维计算几何基础题目泛做(SYX第一轮)
题目1: POJ 2318 TOYS 题目大意: 给一个有n个挡板的盒子,从左到右空格编号为0...n.有好多玩具,问每个玩具在哪个空格里面. 算法讨论: 直接叉积判断就可以.注意在盒子的边界上面也算 ...
- CISC + RISC = Y86
最近在读深入理解计算机系统,打算把读时的心得放上来 Y86有着CISC和RISC的属性Y86可以看成是CISC(IA32),但用RISC的原理简化了 CISC和RISC的竞争引发了许多争论CISC和R ...
- 豆瓣FM duilib版
最近duilib项目被复制到了github上,仿佛又多了些活力.想要总结以前的项目的同时因为很喜欢豆瓣的FM,所以打算做个duilib版本豆瓣FM. 在网上了看到了很多豆瓣的不同版本,node.js. ...
- 反引号backtick中输入多个命令
如果在反引号backtick中输入多个命令会怎样?比如有如下脚本: #!/bin/bash var=`date;who` echo $var 运行该脚本,会发现输出的是命令date和who的集合,只是 ...
- HDU-简单计算器-1237
这道题我做了一天,把中缀表达式转化为后缀表达式,但遇到了储存的问题,考虑了好久,写出后又调试,弄了一天,下面说一下中缀表达式转换后缀表达式: 算法: 中缀表达式转后缀表达式的方法: 1.遇到操作数:直 ...
- browsersync 浏览器自动刷新神器
官网:http://www.browsersync.cn/ 基于node,具体安装方法和使用方法参见官网,可以结合gulp等构建工具来用,也可以单独使用.不错~
- CentOS 安装redis2.8.13 提醒"libc.so.6: version `GLIBC_2.14' not found"系统的glibc版本太低
以下在系统CentOS 6.3 x86_64上操作 1.试图运行程序,提示"libc.so.6: version `GLIBC_2.14' not found",原因是系统的gli ...
- re模块
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...