ListView item activation event : ListViewItem « GUI Windows Forms « C# / CSharp Tutorial






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.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.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;
}








23.30.ListViewItem
23.30.1.Create a ListViewItem and add it to ListViewCreate a ListViewItem and add it to ListView
23.30.2.ListView item activation event