Set MDI parent window

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

  public class Form2 : Form
  {
    private System.Windows.Forms.Button Button3;
    private System.Windows.Forms.Button Button2;
    private System.Windows.Forms.Button Button1;
      
      
    public Form2()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.Button3 = new System.Windows.Forms.Button();
      this.Button2 = new System.Windows.Forms.Button();
      this.Button1 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // Button3
      // 
      this.Button3.Location = new System.Drawing.Point(120, 56);
      this.Button3.Name = "Button3";
      this.Button3.Size = new System.Drawing.Size(88, 32);
      this.Button3.TabIndex = 8;
      this.Button3.Text = "Become Child of Parent2";
      this.Button3.Click += new System.EventHandler(this.Button3_Click);
      // 
      // Button2
      // 
      this.Button2.Location = new System.Drawing.Point(12, 56);
      this.Button2.Name = "Button2";
      this.Button2.Size = new System.Drawing.Size(88, 32);
      this.Button2.TabIndex = 7;
      this.Button2.Text = "Become Child of Parent1";
      this.Button2.Click += new System.EventHandler(this.Button2_Click);
      // 
      // Button1
      // 
      this.Button1.Location = new System.Drawing.Point(12, 12);
      this.Button1.Name = "Button1";
      this.Button1.Size = new System.Drawing.Size(88, 32);
      this.Button1.TabIndex = 6;
      this.Button1.Text = "Become Parent";
      this.Button1.Click += new System.EventHandler(this.Button1_Click);
      // 
      // Form2
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(292, 154);
      this.Controls.Add(this.Button3);
      this.Controls.Add(this.Button2);
      this.Controls.Add(this.Button1);
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.Name = "Form2";
      this.Text = "Form2";
      this.ResumeLayout(false);

    }

    private void Button1_Click(object sender, System.EventArgs e)
    {
      this.Hide();
      this.MdiParent = null;
      this.IsMdiContainer = true;
      this.Show();
    }

    private void Button2_Click(object sender, System.EventArgs e)
    {
      this.Hide();
      this.IsMdiContainer = false;
      this.MdiParent = Program.Main2;
      this.Show();
    }

    private void Button3_Click(object sender, System.EventArgs e)
    {
      this.Hide();
      this.IsMdiContainer = false;
      this.MdiParent = Program.Main1;
      this.Show();
    }
  }

  public class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
      Application.Exit();
    }
    private void InitializeComponent()
    {
      this.SuspendLayout();
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(422, 351);
      this.IsMdiContainer = true;
      this.Name = "Form1";
      this.Text = "Form1";
      this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
      this.ResumeLayout(false);

    }
  }

  static class Program
  {
    public static Form1 Main1 = new Form1();
    public static Form1 Main2 = new Form1();
    public static Form2 Child = new Form2();

    [STAThread]
    public static void Main()
    {
      Application.EnableVisualStyles();
      Main1.Text = "Parent 2";
      Main2.Text = "Parent 1";
      Main1.Show();
      Main2.Show();

      Child.MdiParent = Main2;
      Child.Show();

      System.Windows.Forms.Application.Run();
    }
  }

           
          


Use ListView to display file name and double click the name to execute that file

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.BeginUpdate();

        foreach (System.IO.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); 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = fi.LastAccessTime.ToString(); 
          lvi.SubItems.Add(lvsi); 

             this.lwFilesAndFolders.Items.Add(lvi);
        }        
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
      try
      {
        System.Diagnostics.Process.Start(filename);
      }
      catch
      {
        return;
      }
    }
  }
}

           
          


Use ListView to diaplay folder info and double click to enter that directory

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.BeginUpdate();

        foreach (System.IO.DirectoryInfo di in dirs)
        {
          lvi = new ListViewItem();
          lvi.Text = di.Name; 
          //lvi.ImageIndex = 0;
          lvi.Tag = di.FullName; 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = "sub item"; 
          lvi.SubItems.Add(lvsi); 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = di.LastAccessTime.ToString(); 
          lvi.SubItems.Add(lvsi); 

          this.lwFilesAndFolders.Items.Add(lvi);
        }
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
      PaintListView(filename);
      folderCol.Add(filename);
    }
  }
}

           
          


Use RadioButton to control the ListView display style

image_pdfimage_print


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.RadioButton rdoLarge;
    private System.Windows.Forms.RadioButton rdoSmall;
    private System.Windows.Forms.RadioButton rdoList;
    private System.Windows.Forms.RadioButton rdoDetails;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();
      CreateHeadersAndFillListView();
      PaintListView();
    }

    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.groupBox1 = new System.Windows.Forms.GroupBox();
      this.rdoDetails = new System.Windows.Forms.RadioButton();
      this.rdoList = new System.Windows.Forms.RadioButton();
      this.rdoSmall = new System.Windows.Forms.RadioButton();
      this.rdoLarge = new System.Windows.Forms.RadioButton();
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.groupBox1.SuspendLayout();
      this.SuspendLayout();

      this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {this.rdoDetails,
                                                                            this.rdoList,
                                                                            this.rdoSmall,
                                                                            this.rdoLarge});
      this.groupBox1.Location = new System.Drawing.Point(424, 32);
      this.groupBox1.Name = "groupBox1";
      this.groupBox1.Size = new System.Drawing.Size(120, 128);
      this.groupBox1.TabIndex = 2;
      this.groupBox1.TabStop = false;
      this.groupBox1.Text = "View mode";
      // 
      // rdoDetails
      // 
      this.rdoDetails.Checked = true;
      this.rdoDetails.Location = new System.Drawing.Point(8, 96);
      this.rdoDetails.Name = "rdoDetails";
      this.rdoDetails.Size = new System.Drawing.Size(104, 16);
      this.rdoDetails.TabIndex = 3;
      this.rdoDetails.TabStop = true;
      this.rdoDetails.Text = "Details";
      this.rdoDetails.CheckedChanged += new System.EventHandler(this.rdoDetails_CheckedChanged);
      // 
      // rdoList
      // 
      this.rdoList.Location = new System.Drawing.Point(8, 72);
      this.rdoList.Name = "rdoList";
      this.rdoList.Size = new System.Drawing.Size(104, 16);
      this.rdoList.TabIndex = 2;
      this.rdoList.Text = "List";
      this.rdoList.CheckedChanged += new System.EventHandler(this.rdoList_CheckedChanged);
      // 
      // rdoSmall
      // 
      this.rdoSmall.Location = new System.Drawing.Point(8, 48);
      this.rdoSmall.Name = "rdoSmall";
      this.rdoSmall.Size = new System.Drawing.Size(104, 16);
      this.rdoSmall.TabIndex = 1;
      this.rdoSmall.Text = "SmallIcon";
      this.rdoSmall.CheckedChanged += new System.EventHandler(this.rdoSmall_CheckedChanged);
      // 
      // rdoLarge
      // 
      this.rdoLarge.Location = new System.Drawing.Point(8, 24);
      this.rdoLarge.Name = "rdoLarge";
      this.rdoLarge.Size = new System.Drawing.Size(96, 16);
      this.rdoLarge.TabIndex = 0;
      this.rdoLarge.Text = "LargeIcon";
      this.rdoLarge.CheckedChanged += new System.EventHandler(this.rdoLarge_CheckedChanged);
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.groupBox1, this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.groupBox1.ResumeLayout(false);
      this.ResumeLayout(false);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView()
    {
      ListViewItem lvi;
      ListViewItem.ListViewSubItem lvsi;

      this.lwFilesAndFolders.Items.Clear();
      this.lwFilesAndFolders.BeginUpdate();

      lvi = new ListViewItem();
      lvi.Text = "A"; 
      lvi.ImageIndex = 0; 
      lvi.Tag = "tag";

      lvsi = new ListViewItem.ListViewSubItem();
      lvsi.Text = "4"; 
      lvi.SubItems.Add(lvsi); 
          
      lvsi = new ListViewItem.ListViewSubItem();
      lvsi.Text = "C"; 
      lvi.SubItems.Add(lvsi); 
      this.lwFilesAndFolders.Items.Add(lvi);

      this.lwFilesAndFolders.EndUpdate();
      this.lwFilesAndFolders.View = View.Details;
    }

    private void rdoLarge_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.LargeIcon;
    }

    private void rdoList_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.List;
    }

    private void rdoSmall_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.SmallIcon;
    }

    private void rdoDetails_CheckedChanged(object sender, System.EventArgs e)
    {
      RadioButton rdb = (RadioButton)sender;
      if (rdb.Checked)
        this.lwFilesAndFolders.View = View.Details;
    }
  }
}


           
          


Display Directory info in a ListView

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.BeginUpdate();

        foreach (System.IO.DirectoryInfo di in dirs)
        {
          lvi = new ListViewItem();
          lvi.Text = di.Name; 
          //lvi.ImageIndex = 0;
          lvi.Tag = di.FullName; 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = "sub item"; 
          lvi.SubItems.Add(lvsi); 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = di.LastAccessTime.ToString(); 
          lvi.SubItems.Add(lvsi); 

          this.lwFilesAndFolders.Items.Add(lvi);
        }
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
            Console.WriteLine(filename);
    }
  }
}

           
          


Add ListView column and insert ListView rows

image_pdfimage_print


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListView
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Collections.Specialized.StringCollection folderCol;    
    
    private System.Windows.Forms.ImageList ilLarge;
    private System.Windows.Forms.ImageList ilSmall;
    private System.Windows.Forms.ListView lwFilesAndFolders;
    private System.Windows.Forms.Label lblCurrentPath;

    public Form1()
    {
      InitializeComponent();

      // Init ListView and folder collection
      folderCol = new System.Collections.Specialized.StringCollection();
      CreateHeadersAndFillListView();
      PaintListView(@"C:");
      folderCol.Add(@"C:");

      this.lwFilesAndFolders.ItemActivate += new System.EventHandler(this.lwFilesAndFolders_ItemActivate);
    }
    private void InitializeComponent()
    {
//      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
      this.lblCurrentPath = new System.Windows.Forms.Label();
      this.ilLarge = new System.Windows.Forms.ImageList();
      this.ilSmall = new System.Windows.Forms.ImageList();
      this.lwFilesAndFolders = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.lblCurrentPath.Location = new System.Drawing.Point(16, 8);
      this.lblCurrentPath.Name = "lblCurrentPath";
      this.lblCurrentPath.Size = new System.Drawing.Size(528, 16);
      this.lblCurrentPath.TabIndex = 3;
      // 
      // ilLarge
      // 
      this.ilLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilLarge.ImageSize = new System.Drawing.Size(32, 32);
//      this.ilLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilLarge.ImageStream")));
      this.ilLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ilSmall
      // 
      this.ilSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.ilSmall.ImageSize = new System.Drawing.Size(16, 16);
//      this.ilSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilSmall.ImageStream")));
      this.ilSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // lwFilesAndFolders
      // 
      this.lwFilesAndFolders.LargeImageList = this.ilLarge;
      this.lwFilesAndFolders.Location = new System.Drawing.Point(16, 32);
      this.lwFilesAndFolders.MultiSelect = false;
      this.lwFilesAndFolders.Name = "lwFilesAndFolders";
      this.lwFilesAndFolders.Size = new System.Drawing.Size(400, 216);
      this.lwFilesAndFolders.SmallImageList = this.ilSmall;
      this.lwFilesAndFolders.TabIndex = 0;
      this.lwFilesAndFolders.View = System.Windows.Forms.View.List;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(552, 293);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCurrentPath,  this.lwFilesAndFolders});
      this.Name = "Form1";
      this.Text = "ListView";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void CreateHeadersAndFillListView()
    {
      ColumnHeader colHead;

      colHead = new ColumnHeader();
      colHead.Text = "Filename";
      this.lwFilesAndFolders.Columns.Add(colHead);
      
      colHead = new ColumnHeader();
      colHead.Text = "Size";
      this.lwFilesAndFolders.Columns.Add(colHead); 
      
      colHead = new ColumnHeader();
      colHead.Text = "Last accessed";
      this.lwFilesAndFolders.Columns.Add(colHead); 
    }

    private void PaintListView(string root)
    {
      try
      {
        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        this.lblCurrentPath.Text = root + "    (Double click to display the path name)";

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
      
        DirectoryInfo[] dirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();

        this.lwFilesAndFolders.Items.Clear();

        this.lwFilesAndFolders.BeginUpdate();

        foreach (System.IO.DirectoryInfo di in dirs)
        {
          lvi = new ListViewItem();
          lvi.Text = di.Name; 
          //lvi.ImageIndex = 0;
          lvi.Tag = di.FullName; 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = "sub item"; 
          lvi.SubItems.Add(lvsi); 

          lvsi = new ListViewItem.ListViewSubItem();
          lvsi.Text = di.LastAccessTime.ToString(); 
          lvi.SubItems.Add(lvsi); 

          this.lwFilesAndFolders.Items.Add(lvi);
        }
        this.lwFilesAndFolders.EndUpdate();
      }
      catch (System.Exception err)
      {
        MessageBox.Show("Error: " + err.Message);
      }
      
      this.lwFilesAndFolders.View = View.Details;
    }

    private void lwFilesAndFolders_ItemActivate(object sender, System.EventArgs e)
    {
      System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
      string filename = lw.SelectedItems[0].Tag.ToString();
            
            Console.WriteLine(filename);
    }
  }
}


           
          


Use ListViewItem to display file information

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;


class Form1 : Form {
    private System.Collections.Specialized.StringCollection folderCol;

    public Form1() {
        InitializeComponent();

        folderCol = new System.Collections.Specialized.StringCollection();
        CreateHeadersAndFillListView();
        PaintListView(@"C:");
        folderCol.Add(@"C:");
    }

    private void CreateHeadersAndFillListView() {
        ColumnHeader colHead;

        colHead = new ColumnHeader();
        colHead.Text = "Filename";
        this.listViewFilesAndFolders.Columns.Add(colHead); 

        colHead = new ColumnHeader();
        colHead.Text = "Size";
        this.listViewFilesAndFolders.Columns.Add(colHead); 

        colHead = new ColumnHeader();
        colHead.Text = "Last accessed";
        this.listViewFilesAndFolders.Columns.Add(colHead); 
    }

    private 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();           

            this.listViewFilesAndFolders.Items.Clear();

            this.labelCurrentPath.Text = root;

            this.listViewFilesAndFolders.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);

                this.listViewFilesAndFolders.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);

                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Text = fi.LastAccessTime.ToString();
                lvi.SubItems.Add(lvsi);

                this.listViewFilesAndFolders.Items.Add(lvi);
            }
            this.listViewFilesAndFolders.EndUpdate();
        } catch (System.Exception err) {
            MessageBox.Show("Error: " + err.Message);
        }
    }

    private void listViewFilesAndFoldes_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);
            folderCol.Add(filename);
        }
    }

    private void buttonBack_Click(object sender, EventArgs e) {
        if (folderCol.Count > 1) {
            PaintListView(folderCol[folderCol.Count - 2].ToString());
            folderCol.RemoveAt(folderCol.Count - 1);
        } else {
            PaintListView(folderCol[0].ToString());
        }
    }

    private void radioButtonLargeIcon_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.LargeIcon;
    }

    private void radioButtonList_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.List;
    }

    private void radioButtonSmallIcon_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.SmallIcon;
    }

    private void radioButtonDetails_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.Details;
    }

    private void radioButtonTile_CheckedChanged(object sender, EventArgs e) {
        RadioButton rdb = (RadioButton)sender;
        if (rdb.Checked)
            this.listViewFilesAndFolders.View = View.Tile;
    }
    private void InitializeComponent() {
        System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("listViewFilesAndFoldes");
        this.labelCurrentPath = new System.Windows.Forms.Label();
        this.listViewFilesAndFolders = new System.Windows.Forms.ListView();
        this.buttonBack = new System.Windows.Forms.Button();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.radioButtonSmallIcon = new System.Windows.Forms.RadioButton();
        this.radioButtonList = new System.Windows.Forms.RadioButton();
        this.radioButtonTile = new System.Windows.Forms.RadioButton();
        this.radioButtonDetails = new System.Windows.Forms.RadioButton();
        this.radioButtonLargeIcon = new System.Windows.Forms.RadioButton();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();

        this.labelCurrentPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.labelCurrentPath.Location = new System.Drawing.Point(13, 13);
        this.labelCurrentPath.Name = "labelCurrentPath";
        this.labelCurrentPath.Size = new System.Drawing.Size(502, 15);
        this.labelCurrentPath.TabIndex = 0;

        this.listViewFilesAndFolders.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.listViewFilesAndFolders.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
        listViewItem2
      });
        this.listViewFilesAndFolders.Location = new System.Drawing.Point(13, 35);
        this.listViewFilesAndFolders.Name = "listViewFilesAndFoldes";
        this.listViewFilesAndFolders.Size = new System.Drawing.Size(423, 220);
        this.listViewFilesAndFolders.TabIndex = 1;
        this.listViewFilesAndFolders.View = System.Windows.Forms.View.Details;
        this.listViewFilesAndFolders.ItemActivate += new System.EventHandler(this.listViewFilesAndFoldes_ItemActivate);

        this.buttonBack.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
        this.buttonBack.Location = new System.Drawing.Point(222, 262);
        this.buttonBack.Name = "buttonBack";
        this.buttonBack.TabIndex = 2;
        this.buttonBack.Text = "Back";
        this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);

        this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.groupBox1.Controls.Add(this.radioButtonSmallIcon);
        this.groupBox1.Controls.Add(this.radioButtonList);
        this.groupBox1.Controls.Add(this.radioButtonTile);
        this.groupBox1.Controls.Add(this.radioButtonDetails);
        this.groupBox1.Controls.Add(this.radioButtonLargeIcon);
        this.groupBox1.Location = new System.Drawing.Point(443, 35);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(97, 220);
        this.groupBox1.TabIndex = 3;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "View Mode";

        this.radioButtonSmallIcon.AutoSize = true;
        this.radioButtonSmallIcon.Location = new System.Drawing.Point(7, 44);
        this.radioButtonSmallIcon.Name = "radioButtonSmallIcon";
        this.radioButtonSmallIcon.Size = new System.Drawing.Size(67, 17);
        this.radioButtonSmallIcon.TabIndex = 4;
        this.radioButtonSmallIcon.Text = "SmallIcon";
        this.radioButtonSmallIcon.CheckedChanged += new System.EventHandler(this.radioButtonSmallIcon_CheckedChanged);

        this.radioButtonList.AutoSize = true;
        this.radioButtonList.Location = new System.Drawing.Point(7, 68);
        this.radioButtonList.Name = "radioButtonList";
        this.radioButtonList.Size = new System.Drawing.Size(37, 17);
        this.radioButtonList.TabIndex = 3;
        this.radioButtonList.Text = "List";
        this.radioButtonList.CheckedChanged += new System.EventHandler(this.radioButtonList_CheckedChanged);

        this.radioButtonTile.AutoSize = true;
        this.radioButtonTile.Location = new System.Drawing.Point(7, 116);
        this.radioButtonTile.Name = "radioButtonTile";
        this.radioButtonTile.Size = new System.Drawing.Size(38, 17);
        this.radioButtonTile.TabIndex = 2;
        this.radioButtonTile.Text = "Tile";
        this.radioButtonTile.CheckedChanged += new System.EventHandler(this.radioButtonTile_CheckedChanged);

        this.radioButtonDetails.AutoSize = true;
        this.radioButtonDetails.Checked = true;
        this.radioButtonDetails.Location = new System.Drawing.Point(7, 92);
        this.radioButtonDetails.Name = "radioButtonDetails";
        this.radioButtonDetails.Size = new System.Drawing.Size(53, 17);
        this.radioButtonDetails.TabIndex = 1;
        this.radioButtonDetails.TabStop = true;
        this.radioButtonDetails.Text = "Details";
        this.radioButtonDetails.CheckedChanged += new System.EventHandler(this.radioButtonDetails_CheckedChanged);

        this.radioButtonLargeIcon.AutoSize = true;
        this.radioButtonLargeIcon.Location = new System.Drawing.Point(7, 20);
        this.radioButtonLargeIcon.Name = "radioButtonLargeIcon";
        this.radioButtonLargeIcon.Size = new System.Drawing.Size(69, 17);
        this.radioButtonLargeIcon.TabIndex = 0;
        this.radioButtonLargeIcon.Text = "LargeIcon";
        this.radioButtonLargeIcon.CheckedChanged += new System.EventHandler(this.radioButtonLargeIcon_CheckedChanged);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(550, 291);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.buttonBack);
        this.Controls.Add(this.listViewFilesAndFolders);
        this.Controls.Add(this.labelCurrentPath);
        this.Name = "Form1";
        this.Text = "ListView";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    private System.Windows.Forms.Label labelCurrentPath;
    private System.Windows.Forms.ListView listViewFilesAndFolders;
    private System.Windows.Forms.Button buttonBack;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.RadioButton radioButtonLargeIcon;
    private System.Windows.Forms.RadioButton radioButtonDetails;
    private System.Windows.Forms.RadioButton radioButtonTile;
    private System.Windows.Forms.RadioButton radioButtonList;
    private System.Windows.Forms.RadioButton radioButtonSmallIcon;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}