MDI form


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ImpossibleAPI
{
    
    public class Global
    {
        public static Form1 Main1 = new Form1();
        public static Form1 Main2 = new Form1();
        public static Form2 Child = new Form2();                                
                                                                                
        [STAThread]
        public static void Main() 
        {
            Main1.Text = "Parent 2";
            Main2.Text = "Parent 1";
            Main1.Show();
            Main2.Show();
            
            Child.MdiParent = Main2;
            Child.Show();

            System.Windows.Forms.Application.Run();
        }
        
    }
    /// <summary>
    /// Summary description for Form2.
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Button Button3;
        internal System.Windows.Forms.Button Button2;
        internal System.Windows.Forms.Button Button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form2()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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(124, 60);
            this.Button3.Name = "Button3";
            this.Button3.Size = new System.Drawing.Size(88, 32);
            this.Button3.TabIndex = 5;
            this.Button3.Text = "Become Child of Parent2";
            this.Button3.Click += new System.EventHandler(this.Button3_Click);
            // 
            // Button2
            // 
            this.Button2.Location = new System.Drawing.Point(16, 60);
            this.Button2.Name = "Button2";
            this.Button2.Size = new System.Drawing.Size(88, 32);
            this.Button2.TabIndex = 4;
            this.Button2.Text = "Become Child of Parent1";
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            // 
            // Button1
            // 
            this.Button1.Location = new System.Drawing.Point(16, 16);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(88, 32);
            this.Button1.TabIndex = 3;
            this.Button1.Text = "Become Parent";
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(292, 150);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Button3,
                                                                          this.Button2,
                                                                          this.Button1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }
        #endregion

        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 = Global.Main2;
            this.Show();
        }

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

    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(464, 370);
            this.IsMdiContainer = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            this.Load += new System.EventHandler(this.Form1_Load);

        }
        #endregion

        private void Form1_Load(object sender, System.EventArgs e)
        {
        
        }

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>

    }


}



           
          


Maximized MDI window at startup


   


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

namespace MdiBasic
{
  public class frmContainer : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components = null;

    public frmContainer()
    {
      InitializeComponent();
            MdiBasic.frmChild child = new MdiBasic.frmChild(this);

            child.Show();
        }
    private void InitializeComponent()
    {
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(292, 273);
         this.IsMdiContainer = true;
         this.Name = "frmContainer";
         this.Text = "MDI Basic";
         this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

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

  public class frmChild : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components = null;

        public frmChild(MdiBasic.frmContainer parent)
    {
      InitializeComponent();
            this.MdiParent = parent;
        }
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.Size = new System.Drawing.Size(300,300);
      this.Text = "frmChild";
    }
  }
}

           
          


Set MDI parent window


   


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

           
          


Masked TextBox Host

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

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

namespace MaskedTextBoxHost
{
///

/// Summary description for MaskedTextBoxHost.
///

public class MaskedTextBoxHost : System.Windows.Forms.Form
{
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public MaskedTextBoxHost()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///

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

///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// MaskedTextBoxHost
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = “MaskedTextBoxHost”;
this.Text = “MaskedTextBoxHost”;
this.Load += new System.EventHandler(this.MaskedTextBoxHost_Load);

}

///

/// The main entry point for the application.
///

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

private void MaskedTextBoxHost_Load(object sender, System.EventArgs e)
{
MaskedTextBox txtMask = new
MaskedTextBox();
txtMask.Location = new Point(10, 10);
txtMask.Mask = “(###) ###-####”;
this.Controls.Add(txtMask);

}
}
public class MaskedTextBox : TextBox
{
private string mask;
public string Mask
{
get
{
return mask;
}
set
{
mask = value;
this.Text = “”;
}
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
if (Mask != “”)
{
// Suppress the typed character.
e.Handled = true;

string newText = this.Text;

// Loop through the mask, adding fixed characters as needed.
// If the next allowed character matches what the user has
// typed in (a number or letter), that is added to the end.
bool finished = false;
for (int i = this.SelectionStart; i < mask.Length; i++) { switch (mask[i].ToString()) { case "#" : // Allow the keypress as long as it is a number. if (Char.IsDigit(e.KeyChar)) { newText += e.KeyChar.ToString(); finished = true; break; } else { // Invalid entry; exit and don't change the text. return; } case "." : // Allow the keypress as long as it is a letter. if (Char.IsLetter(e.KeyChar)) { newText += e.KeyChar.ToString(); finished = true; break; } else { // Invalid entry; exit and don't change the text. return; } default : // Insert the mask character. newText += mask[i]; break; } if (finished) { break; } } // Update the text. this.Text = newText; this.SelectionStart = this.Text.Length; } } protected override void OnKeyDown(KeyEventArgs e) { // Stop special characters. e.Handled = true; } } } [/csharp]

MaskedTextBox with MaskInputRejected event

   
 

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

public class MainWindow : Form {
    public MainWindow() {
        InitializeComponent();
        CreateLabelControl();
    }

    private void btnDisplayData_Click(object sender, EventArgs e) {
        string textBoxData = "";
        textBoxData += string.Format("MultiLine:  {0}
", txtMultiline.Text);
        textBoxData += string.Format("
Password:  {0}
", txtPassword.Text);
        textBoxData += string.Format("
Uppercase:  {0}
", txtUpperCase.Text);
        textBoxData += string.Format("
Masked:  {0}
", txtMaskedTextBox.Text);

        MessageBox.Show(textBoxData, "Here is the data in your TextBoxes");
    }

    private void CreateLabelControl() {
        Label lblInstructions = new Label();
        lblInstructions.Text = "Please enter values in all the text boxes";
        lblInstructions.Font = new Font("Times New Roman", 9.75F, FontStyle.Bold);
        lblInstructions.AutoSize = true;
        lblInstructions.Location = new System.Drawing.Point(16, 13);
        lblInstructions.Size = new System.Drawing.Size(240, 16);
        this.Controls.Add(lblInstructions);
    }

    private void txtMaskedTextBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) {
        this.Text = string.Format("Error: {0}", e.RejectionHint);
    }

    private void txtMaskedTextBox_KeyDown(object sender, KeyEventArgs e) {
        this.Text = "Labels and TextBoxes";
    }
    private void InitializeComponent() {
        this.lblPassword = new System.Windows.Forms.Label();
        this.txtPassword = new System.Windows.Forms.TextBox();
        this.lblMultiline = new System.Windows.Forms.Label();
        this.txtMultiline = new System.Windows.Forms.TextBox();
        this.lblUpperCaseOnly = new System.Windows.Forms.Label();
        this.txtUpperCase = new System.Windows.Forms.TextBox();
        this.lblMaskedTextBox = new System.Windows.Forms.Label();
        this.txtMaskedTextBox = new System.Windows.Forms.MaskedTextBox();
        this.btnDisplayData = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.lblPassword.AutoSize = true;
        this.lblPassword.Location = new System.Drawing.Point(13, 43);
        this.lblPassword.Name = "lblPassword";
        this.lblPassword.Size = new System.Drawing.Size(94, 13);
        this.lblPassword.TabIndex = 0;
        this.lblPassword.Text = "&amp;Password TextBox:";

        this.txtPassword.Location = new System.Drawing.Point(125, 43);
        this.txtPassword.Name = "txtPassword";
        this.txtPassword.PasswordChar = &#039;*&#039;;
        this.txtPassword.Size = new System.Drawing.Size(220, 20);
        this.txtPassword.TabIndex = 1;

        this.lblMultiline.AutoSize = true;
        this.lblMultiline.Location = new System.Drawing.Point(14, 80);
        this.lblMultiline.Name = "lblMultiline";
        this.lblMultiline.Size = new System.Drawing.Size(86, 13);
        this.lblMultiline.TabIndex = 2;
        this.lblMultiline.Text = "&amp;Multiline TextBox:";

        this.txtMultiline.Location = new System.Drawing.Point(125, 80);
        this.txtMultiline.Multiline = true;
        this.txtMultiline.Name = "txtMultiline";
        this.txtMultiline.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.txtMultiline.Size = new System.Drawing.Size(220, 52);
        this.txtMultiline.TabIndex = 3;

        this.lblUpperCaseOnly.AutoSize = true;
        this.lblUpperCaseOnly.Location = new System.Drawing.Point(15, 157);
        this.lblUpperCaseOnly.Name = "lblUpperCaseOnly";
        this.lblUpperCaseOnly.Size = new System.Drawing.Size(82, 13);
        this.lblUpperCaseOnly.TabIndex = 4;
        this.lblUpperCaseOnly.Text = "&amp;Uppercase Only:";

        this.txtUpperCase.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
        this.txtUpperCase.Location = new System.Drawing.Point(125, 157);
        this.txtUpperCase.Name = "txtUpperCase";
        this.txtUpperCase.Size = new System.Drawing.Size(220, 20);
        this.txtUpperCase.TabIndex = 5;
        // 
        this.lblMaskedTextBox.AutoSize = true;
        this.lblMaskedTextBox.Location = new System.Drawing.Point(15, 199);
        this.lblMaskedTextBox.Name = "lblMaskedTextBox";
        this.lblMaskedTextBox.Size = new System.Drawing.Size(83, 13);
        this.lblMaskedTextBox.TabIndex = 7;
        this.lblMaskedTextBox.Text = "Mas&amp;ked TextBox";
        // 
        this.txtMaskedTextBox.BeepOnError = true;
        this.txtMaskedTextBox.CausesValidation = false;
        this.txtMaskedTextBox.Location = new System.Drawing.Point(125, 199);
        this.txtMaskedTextBox.Mask = "(999) 000-0000";
        this.txtMaskedTextBox.Name = "txtMaskedTextBox";
        this.txtMaskedTextBox.Size = new System.Drawing.Size(220, 20);
        this.txtMaskedTextBox.TabIndex = 8;
        this.txtMaskedTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtMaskedTextBox_KeyDown);
        this.txtMaskedTextBox.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.txtMaskedTextBox_MaskInputRejected);
        // 
        this.btnDisplayData.Location = new System.Drawing.Point(257, 236);
        this.btnDisplayData.Name = "btnDisplayData";
        this.btnDisplayData.Size = new System.Drawing.Size(88, 23);
        this.btnDisplayData.TabIndex = 6;
        this.btnDisplayData.Text = "Display Data";
        this.btnDisplayData.Click += new System.EventHandler(this.btnDisplayData_Click);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(357, 276);
        this.Controls.Add(this.txtMaskedTextBox);
        this.Controls.Add(this.lblMaskedTextBox);
        this.Controls.Add(this.btnDisplayData);
        this.Controls.Add(this.txtUpperCase);
        this.Controls.Add(this.lblUpperCaseOnly);
        this.Controls.Add(this.txtMultiline);
        this.Controls.Add(this.lblMultiline);
        this.Controls.Add(this.txtPassword);
        this.Controls.Add(this.lblPassword);
        this.Text = "Fun with Labels and TextBoxes";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.Label lblPassword;
    private System.Windows.Forms.TextBox txtPassword;
    private System.Windows.Forms.Label lblMultiline;
    private System.Windows.Forms.TextBox txtMultiline;
    private System.Windows.Forms.Label lblUpperCaseOnly;
    private System.Windows.Forms.TextBox txtUpperCase;
    private System.Windows.Forms.Label lblMaskedTextBox;
    private System.Windows.Forms.MaskedTextBox txtMaskedTextBox;
    private System.Windows.Forms.Button btnDisplayData;

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

    


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


   


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 ListView to display file name and double click the name to execute that file


   


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;
      }
    }
  }
}