C# ListView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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();
            this.lwFileAndFolders.ItemActivate += new System.EventHandler(this.lwFileAndFolders_ItemActivate);

            folderCol = new System.Collections.Specialized.StringCollection();
            CreateHeaderAndFillListView();
            PaintListView(@"D:\");
            folderCol.Add(@"D:\");
        }

        private void lwFileAndFolders_ItemActivate(object sender, System.EventArgs e)
        {
            System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
            string fileName = lwFileAndFolders.SelectedItems[0].Tag.ToString();
            if (lw.SelectedItems[0].ImageIndex != 0)
            {
                try
                {
                    System.Diagnostics.Process.Start(fileName);
                }
                catch
                {
                    return;
                }
            }
            else
            {
                PaintListView(fileName);
                folderCol.Add(fileName);
            }
        }
        // 创建列标题;
        private void CreateHeaderAndFillListView()
        {
            ColumnHeader colHead;
            // 第一个标题;
            colHead = new ColumnHeader();
            colHead.Text = "FileName";
            this.lwFileAndFolders.Columns.Add(colHead); // 将标题插入到listView中

            // 第二个标题;
            colHead = new ColumnHeader();
            colHead.Text = "Size";
            this.lwFileAndFolders.Columns.Add(colHead);

            // 第三个标题
            colHead = new ColumnHeader();
            colHead.Text = "Last accessed";
            this.lwFileAndFolders.Columns.Add(colHead);
        }
        // 用硬盘山的文件盒文件夹填充列表;
        private void PaintListView(string root)
        {
            try
            {
                // 用来存储列表对象, 然后将其插入到视图列表框中;
                ListViewItem lvi;
                ListViewItem.ListViewSubItem lvsi;

                if (root.CompareTo("") == 0)
                    return;

                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root); // 获取要显示的文件的目录信息;
                DirectoryInfo[] dirs = dir.GetDirectories();// Folders;  返回当前目录的所以子目录;
                FileInfo[] files = dir.GetFiles(); // Files; 返回当前目录下的所有文件,为每个文件返回一个FileInfor对象;

                this.lwFileAndFolders.Items.Clear();// 清空ListView里面的所有内容,包括列标题;
                this.label1.Text = root; // 标签显示当前的目录;

                this.lwFileAndFolders.BeginUpdate(); // 告诉ListView停止更新其可见域;

                // 添加所有文件夹,并插入到ListView中;
                foreach (System.IO.DirectoryInfo di in dirs)
                {
                    lvi = new ListViewItem();
                    lvi.Name = di.Name;
                    lvi.ImageIndex = 0; // 获取和设置当树节点处于未选定状态时所显示图像的图像列表索引值;。从0开始索引,表示图像在所分配的ImageList中的位置;
                    lvi.Tag = di.FullName;
                    
                    lvsi = new ListViewItem.ListViewSubItem();//创建ListView的子目录
                    lvsi.Text = "";
                    lvi.SubItems.Add(lvsi); // 将子目录添加到lvi中;

                    lvsi = new ListViewItem.ListViewSubItem();
                    lvsi.Text = di.LastAccessTime.ToString();
                    lvi.SubItems.Add(lvsi);

                    this.lwFileAndFolders.Items.Add(lvi);
                }
                foreach (System.IO.FileInfo fi in files)
                {
                    lvi = new ListViewItem();
                    lvi.Text = fi.Name;
                    lvi.ImageIndex = 1;
                    lvi.Tag = fi.FullName;

                    lvsi = new ListViewItem.ListViewSubItem();
                    lvsi.Text = fi.Length.ToString();
                    lvi.SubItems.Add(lvsi);

                    this.lwFileAndFolders.Items.Add(lvi);
                }
                this.lwFileAndFolders.EndUpdate();
            }
            catch (System.Exception err)
            {
                MessageBox.Show("Error:"+err.Message);
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (folderCol.Count > 1)
            {
                PaintListView(folderCol[folderCol.Count - 2].ToString());
                folderCol.RemoveAt(folderCol.Count - 1);
            }
            else
            {
                PaintListView(folderCol[0].ToString());
            }
        }

        private void rdoLargeIcon_CheckedChanged(object sender, EventArgs e)
        {
            /* 因为事件使用它们的方法传递事件的时候,sender都指向了引起这个事件的控件的对象(对象的类型很多),
             * 但是无论哪个控件调用了方法,其结果是一样的。这里我们制定了这个对象为RadioButton,因为此时此刻我们
             * 是由这个单选按钮(RadioButton)引发的事件处理,我们这样转化后,就可以调用RadioButton里面的一些属性和方法
             * 来帮助我们做出更加方面的判断,因为object是最顶层的基类,不一定具有RadioButton所有的属性和方法,但是RadioButton
             * 继承object,则object所有的非private属性和方法我们这里都可以使用;
            */
            RadioButton rdb = (RadioButton)sender;
            if (rdb.Checked)
                this.lwFileAndFolders.View = View.LargeIcon;
        }

        private void rdoSmallIcon_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = (RadioButton)sender;
            if (rdb.Checked)
                this.lwFileAndFolders.View = View.SmallIcon;
        }

        private void rdoList_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = (RadioButton)sender;
            if (rdb.Checked)
                this.lwFileAndFolders.View = View.List;
        }

        private void rdoDetails_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = (RadioButton)sender;
            if (rdb.Checked)
                this.lwFileAndFolders.View = View.Details;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值