Simply adding some items to a ListView control. category 'experiment', language C#, created 04-Oct-2009, version V1.0, by Luc Pattyn |
License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author. Disclaimer: This work is provided “as is”, without any express or implied warranties or conditions or guarantees. You, the user, assume all risk in its use. In no event will the author be liable to you on any legal theory for any special, incidental, consequential, punitive or exemplary damages arising out of this license or the use of the work or otherwise. |
I hadn't used the standard ListView
Control yet, so I decided to perform a little test with it.
I often use list views, and when I do I tend to roll my own Control.
The environment used is the Microsoft .NET Framework (version 2.0 or above) and the C# programming language. The items to be displayed are the folders and files in the EXE's folder.
This is the bulk of the code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace ListView1 {
public class Form1 : Form {
public Form1() {
InitializeComponent();
Size=new Size(SystemInformation.PrimaryMonitorSize.Width-150,
SystemInformation.PrimaryMonitorSize.Height-50);
}
private void log(string s) {
lb.Items.Add(s);
lb.TopIndex=lb.Items.Count-1;
}
private void log(Exception exc) {
foreach(string s in exc.ToString().Split('\n','\r')) if (s.Length!=0) log(s);
}
private void Form1_Shown(object sender, EventArgs e) {
string folder=Path.GetDirectoryName(Application.ExecutablePath);
log("Folder = "+folder);
ListViewGroup lvgFolders = lv.Groups.Add("Folders", "Folders");
ListViewGroup lvgFiles = lv.Groups.Add("Files", "Files");
DirectoryInfo di=new DirectoryInfo(folder);
try {
int count=0;
foreach (DirectoryInfo fi in di.GetDirectories()) {
log("folder "+fi.Name);
count++;
ListViewItem item=lv.Items.Add(fi.Name);
item.SubItems.Add(fi.LastWriteTime.ToString("dd-MMM-yyyy"));
item.SubItems.Add("");
item.Group=lvgFolders;
}
foreach (FileInfo fi in di.GetFiles()) {
log("file "+fi.Name);
count++;
ListViewItem item=lv.Items.Add(fi.Name);
item.SubItems.Add(fi.LastWriteTime.ToString("dd-MMM-yyyy"));
long size=(fi.Length+1023)/1024;
item.SubItems.Add(size.ToString()+" KB");
item.Group=lvgFiles;
}
log("========================== Done (created "+count+" items)");
} catch (Exception exc) { log(exc); }
}
private void lv_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e) {
log("lv_ColumnWidthChanging: will be canceled");
e.NewWidth=lv.Columns[e.ColumnIndex].Width; // keep width
e.Cancel=true;
}
}
And here is the designer-generated code which has to be added to the above to set up the GUI; a ListBox is used for logging, as I often do:
private void InitializeComponent() {
this.lb = new System.Windows.Forms.ListBox();
this.lv = new System.Windows.Forms.ListView();
this.colName = new System.Windows.Forms.ColumnHeader();
this.colDate = new System.Windows.Forms.ColumnHeader();
this.colSize = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// lb
//
this.lb.Anchor = ((System.Windows.Forms.AnchorStyles)((((
System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
this.lb.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lb.FormattingEnabled = true;
this.lb.ItemHeight = 15;
this.lb.Location = new System.Drawing.Point(489, 18);
this.lb.Name = "lb";
this.lb.Size = new System.Drawing.Size(361, 334);
this.lb.TabIndex = 1;
//
// lv
//
this.lv.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left)));
this.lv.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colName,
this.colDate,
this.colSize});
this.lv.Location = new System.Drawing.Point(34, 19);
this.lv.Name = "lv";
this.lv.Size = new System.Drawing.Size(430, 341);
this.lv.TabIndex = 2;
this.lv.UseCompatibleStateImageBehavior = false;
this.lv.View = System.Windows.Forms.View.Details;
this.lv.ColumnWidthChanging += new System.Windows.Forms.ColumnWidthChangingEventHandler(
this.lv_ColumnWidthChanging);
//
// colName
//
this.colName.Text = "Name";
this.colName.Width = 200;
//
// colDate
//
this.colDate.Text = "Date";
this.colDate.Width = 100;
//
// colSize
//
this.colSize.Text = "Size";
this.colSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.colSize.Width = 75;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(863, 384);
this.Controls.Add(this.lv);
this.Controls.Add(this.lb);
this.Name = "Form1";
this.Text = "ListView Experiment";
this.Shown += new System.EventHandler(this.Form1_Shown);
this.ResumeLayout(false);
}
private ListView lv;
private ColumnHeader colName;
private ColumnHeader colDate;
private ColumnHeader colSize;
private System.Windows.Forms.ListBox lb;
}
I wanted two groups; adding them with Visual Designer resulted in local variables, which I could not use later to associate new items to the existing groups. So I decided to add Groups by explicit code.
There is a
known bug:
resizing column widths can not be prevented by simply setting Canceled=true;
inside
the ColumnWidthChanging
evemt. Instead one must explicitly set the NewWidth
value to the current width.
It works more or less as expected.
Perceler |
Copyright © 2012, Luc Pattyn |
Last Modified 02-Sep-2013 |