Csharp/CSharp Tutorial/GUI Windows Forms/ListViewItem

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create a ListViewItem and add it to ListView

using System;
using System.IO;
using System.Windows.Forms;
public class ListViewFileInfoFormDemo{
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new ListViewFileInfoForm());
    }
}
public partial class ListViewFileInfoForm : Form
{
    public ListViewFileInfoForm()
    {
        InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        DirectoryInfo directory = new DirectoryInfo(@"C:\");
        FileInfo[] files = directory.GetFiles();
        foreach (FileInfo file in files)
        {
            ListViewItem item = listView1.Items.Add(file.Name);
            //item.ImageIndex = 0;
            item.Tag = file;
        }
    }
    private void listView1_ItemActivate(object sender, EventArgs e)
    {
        ListViewItem item = ((ListView)sender).SelectedItems[0];
        FileInfo file = (FileInfo)item.Tag;
        string info = file.FullName + " is " + file.Length + " bytes.";
        MessageBox.Show(info, "File Information");
    }
}
partial class ListViewFileInfoForm
{
    private System.ruponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.listView1 = new System.Windows.Forms.ListView();
        this.SuspendLayout();
        this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView1.Location = new System.Drawing.Point(0, 0);
        this.listView1.MultiSelect = false;
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(292, 266);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.listView1);
        this.Name = "ListViewFileInfoForm";
        this.Text = "ListViewFileInfoForm";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.ListView listView1;
}

ListView item activation event

using System;
using System.IO;
using System.Windows.Forms;
public class ListViewItemActivateFormDemo{
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new ListViewItemActivateForm());
    }
}
public partial class ListViewItemActivateForm : Form
{
    public ListViewItemActivateForm()
    {
        InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        DirectoryInfo directory = new DirectoryInfo(@"C:\");
        FileInfo[] files = directory.GetFiles();
        foreach (FileInfo file in files)
        {
            ListViewItem item = listView1.Items.Add(file.Name);
            item.ImageIndex = 0;
            item.Tag = file;
        }
    }
    private void listView1_ItemActivate(object sender, EventArgs e)
    {
        ListViewItem item = ((ListView)sender).SelectedItems[0];
        FileInfo file = (FileInfo)item.Tag;
        string info = file.FullName + " is " + file.Length + " bytes.";
        MessageBox.Show(info, "File Information");
    }
}
partial class ListViewItemActivateForm
{
    private System.ruponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.listView1 = new System.Windows.Forms.ListView();
        this.SuspendLayout();
        this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView1.Location = new System.Drawing.Point(0, 0);
        this.listView1.MultiSelect = false;
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(292, 266);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.listView1);
        this.Name = "ListViewItemActivateForm";
        this.Text = "ListViewItemActivateForm";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.ListView listView1;
}