| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 555 人关注过本帖
标题:关于listview的代码不理解 求高手
只看楼主 加入收藏
open382000
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2012-6-29
结帖率:0
收藏
 问题点数:0 回复次数:1 
关于listview的代码不理解 求高手
图片附件: 游客没有浏览图片的权限,请 登录注册
2012-08-10 18:07
open382000
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2012-6-29
收藏
得分:0 
代码是 using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using

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 = 0; // 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 = 1; // 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[0].Tag.ToString();

      if (lw.SelectedItems[0].ImageIndex != 0)//这里的判断是什么意思~
      {
        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 > 1)
      {
        PaintListView(folderCol[folderCol.Count - 2].ToString());
        folderCol.RemoveAt(folderCol.Count - 1);
      }
      else
        PaintListView(folderCol[0].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;
    }
  }
}
2012-08-10 18:10
快速回复:关于listview的代码不理解 求高手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023465 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved