关于Listview应用问题
新手仿书示例,用ListView控件显示:C:\WINDOWS 下文件及文件夹,并根据视图通过单选按钮切换不同模式,但不能实现,请高手赐教,原程序如下: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 Example_ListViewTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
if (radSmallIco.Checked)
this.lvwFile.View = View.SmallIcon;
}
private void CreateColumnHeader()
{
System.Windows.Forms.ColumnHeader header;
header = new ColumnHeader();
header.Text = "名称";
this.lvwFile.Columns.Add(header );
header = new ColumnHeader();
header.Text = "大小";
this.lvwFile.Columns.Add(header);
header = new ColumnHeader();
header.Text = "修改日期";
this.lvwFile.Columns.Add(header);
}
private void Form1_Load(object sender, EventArgs e)
{
CreateColumnHeader();
try {
ListViewItem lstItem;
ListViewItem.ListViewSubItem lstSubItem;
DirectoryInfo dir = new DirectoryInfo("c:\\windows");
DirectoryInfo[] dirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
this.lblCurrentPath.Text = "c:\\WINDOWS";
foreach (DirectoryInfo di in dirs )
{
this .lvwFile .BeginUpdate();
lstItem = new ListViewItem();
lstItem.Text = di.Name;
lstItem.Tag = di.FullName;
lstItem.ImageIndex = 0;
lstSubItem = new ListViewItem.ListViewSubItem();
lstSubItem.Text = "";
lstItem.SubItems.Add(lstSubItem);
lstSubItem = new ListViewItem.ListViewSubItem();
lstSubItem.Text = di.LastAccessTime.ToString();
lstItem.SubItems.Add(lstSubItem);
this.lvwFile.Items.Add(lstItem);
foreach (FileInfo fi in files )
{
lstItem = new ListViewItem();
lstItem.Text = fi.Name;
lstItem.Tag = fi.FullName;
lstItem.ImageIndex = 1;
lstSubItem = new ListViewItem.ListViewSubItem();
lstSubItem.Text = fi.Length.ToString();
lstItem.SubItems.Add(lstSubItem);
lstSubItem = new ListViewItem.ListViewSubItem();
lstSubItem.Text = fi.LastAccessTime.ToString();
lstItem.SubItems.Add(lstSubItem);
this.lvwFile.Items.Add(lstItem );
}
}
this.lvwFile.EndUpdate();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
}
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void radDetail_CheckedChanged(object sender, EventArgs e)
{
if (radDetail.Checked)
this.lvwFile.View = View.Details;
}
private void radlist_CheckedChanged(object sender, EventArgs e)
{
if (radlist.Checked)
this.lvwFile.View = View.List;
}
private void radLargeIcon_CheckedChanged(object sender, EventArgs e)
{
if (radLargeIcon.Checked)
{
this.lvwFile.View = View.LargeIcon;
}
}
private void radTitle_CheckedChanged(object sender, EventArgs e)
{
if (radTitle.Checked)
this.lvwFile.View = View.Tile;
}
}
}