從工具欄中拉兩個Imagelist控件,分別命名為ilLarge,ilSmall,分別添加兩個圖片以顯示文件的圖標, 屬性Size分別設置為(16,16),(32,32). ListVIEw屬性
LagerLmageList=ilLagrge,SmallImageList=ilsmall
2. 再拉一個ContextMenuStrip控件,以選擇ListView.VIEw
public partial class ListVIEw : Form
...{
System.Collections.Specialized.StringCollection foldCol;
public ListVIEw()
...{
InitializeComponent();
foreach (string drive in Directory.GetLogicalDrives())
...{
Filelist.Items.Add(drive);
}
foldCol = new System.Collections.Specialized.StringCollection();
createHeadersAndFillListVIEw();
PaintListVIEw(@""+Filelist.Items[0].ToString()+"");
foldCol.Add(@"" + Filelist.Items[0].ToString() + "");
FileView.IteMactivate += new EventHandler(FileVIEw_IteMactivate);
Filelist.DoubleClick += new EventHandler(Filelist_DoubleClick);
}
void Filelist_DoubleClick(object sender, EventArgs e)
...{
ListBox list = (ListBox)sender;
string Filename = (string)list.SelectedItem;
PaintListVIEw(@"" + Filename + "");
foldCol.Add(@"" + Filename + "");
}
void FileVIEw_IteMactivate(object sender, EventArgs e)
...{
System.Windows.Forms.ListView lw=(System.Windows.Forms.ListVIEw)sender;
string filename = lw.SelectedItems[0].Tag.ToString();
if (lw.SelectedItems[0].ImageIndex != 0)
...{
try
System.Diagnostics.Process.Start(filename);
}
catch
...{
return;
}
}
else
...{
PaintListVIEw(filename);
foldCol.Add(filename);
}
}
void createHeadersAndFillListVIEw()
...{
ColumnHeader colHead;
colHead = new ColumnHeader();
&n.Text = "Filename";
FileVIEw.Columns.Add(colHead);
colHead = new ColumnHeader();
colHead.Text = "Size";
FileVIEw.Columns.Add(colHead);
colHead = new ColumnHeader();
colHead.Text = "Last Accessed";
FileVIEw.Columns.Add(colHead);
}
void PaintListVIEw(string root)
...{
try
...{
ListVIEwItem lvi;
ListViewItem.ListVIEwSubItem lvsi;
if (root.CompareTo("") == 0)
return;
DirectoryInfo dir = new DirectoryInfo(root);
DirectoryInfo[] dirs = dir.GetDirectorIEs();
FileInfo[] files = dir.GetFiles();
.Net實例:ListVIEw顯示本機文件夾及文件
[ 來源:.net實例 | 作者:.net實例 | 時間:2008-2-22 | 去論壇]
-
- FileVIEw.Items.Clear();
labPathName.Text = root;
FileVIEw.BeginUpdate();
foreach (DirectoryInfo di in dirs)
...{
lvi = new ListVIEwItem();
lvi.Text = di.Name;
lvi.ImageIndex = 0;
lvi.Tag = di.FullName;
lvsi = new ListViewItem.ListVIEwSubItem();
lvsi.Text = "";
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListVIEwSubItem();
lvsi.Text = di.LastAccessTime.ToString();
lvi.SubItems.Add(lvsi);
FileVIEw.Items.Add(lvi);
}
foreach (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);
FileVIEw.Items.Add(lvi);
}
FileVIEw.EndUpdate();
}
catch (Exception err) ...{ MessageBox.Show("Error:" + err.Message); }
}
private void btnpicture_Click(object sender, EventArgs e)
...{
.Count > 1)
...{
PaintListVIEw(foldCol[foldCol.Count-2].ToString());
foldCol.RemoveAt(foldCol.Count - 1);
}
else ...{ PaintListVIEw(foldCol[0].ToString()); }
}
private void itemLargeIcon_Click(object sender, EventArgs e)
...{
FileView.View = VIEw.LargeIcon;
}
private void itemList_Click(object sender, EventArgs e)
...{
FileView.View = VIEw.List;
}
private void itemSmallIcon_Click(object sender, EventArgs e)
...{
FileView.View = VIEw.SmallIcon;
}
private void itemDetails_Click(object sender, EventArgs e)
...{
FileView.View = VIEw.Details;
}
}
運行效果圖