Recursively load Directory info into TreeView


   


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

public class Form1 : Form
{
      private System.Windows.Forms.TreeView directoryTreeView;
     
      string substringDirectory;
       
      public Form1() {
        InitializeComponent();
        directoryTreeView.Nodes.Clear();
        
        String path = "c:Temp";

        directoryTreeView.Nodes.Add( path );
        PopulateTreeView(path, directoryTreeView.Nodes[ 0 ] );
      }  

      public void PopulateTreeView(string directoryValue, TreeNode parentNode )
      {
          string[] directoryArray = 
           Directory.GetDirectories( directoryValue );

          try
          {
             if ( directoryArray.Length != 0 )
             {
                foreach ( string directory in directoryArray )
                {
                  substringDirectory = directory.Substring(
                  directory.LastIndexOf( '' ) + 1,
                  directory.Length - directory.LastIndexOf( '' ) - 1 );

                  TreeNode myNode = new TreeNode( substringDirectory );

                  parentNode.Nodes.Add( myNode );

                  PopulateTreeView( directory, myNode );
               }
             }
          } catch ( UnauthorizedAccessException ) {
            parentNode.Nodes.Add( "Access denied" );
          } // end catch
      }

      private void InitializeComponent()
      {
         this.directoryTreeView = new System.Windows.Forms.TreeView();
         this.SuspendLayout();
         // 
         // directoryTreeView
         // 
         this.directoryTreeView.Location = new System.Drawing.Point(12, 77);
         this.directoryTreeView.Name = "directoryTreeView";
         this.directoryTreeView.Size = new System.Drawing.Size(284, 265);
         this.directoryTreeView.TabIndex = 0;
         // 
         // TreeViewDirectoryStructureForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(304, 354);
         this.Controls.Add(this.directoryTreeView);
         this.Name = "TreeViewDirectoryStructureForm";
         this.Text = "TreeViewDirectoryStructure";
         this.ResumeLayout(false);
         this.PerformLayout();

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

}


           
          


Creating a system tray icon for your application


   


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

  public class Form1 : System.Windows.Forms.Form
  {
   private System.Windows.Forms.NotifyIcon notifyIcon1;
   private System.Windows.Forms.ContextMenu contextMenu1;
   private System.Windows.Forms.MenuItem menuItem1;
   private System.Windows.Forms.MenuItem menuItem2;
   private System.Windows.Forms.MenuItem menuItem3;

   public Form1() {
     InitializeComponent();
   }
   private void InitializeComponent()
   {
     this.notifyIcon1 = new NotifyIcon(new System.ComponentModel.Container());
     this.contextMenu1 = new System.Windows.Forms.ContextMenu();
     this.menuItem1 = new System.Windows.Forms.MenuItem();
     this.menuItem2 = new System.Windows.Forms.MenuItem();
     this.menuItem3 = new System.Windows.Forms.MenuItem();
     this.SuspendLayout();
     // This line associates the context menu with the icon
     this.notifyIcon1.ContextMenu = this.contextMenu1;
     this.notifyIcon1.Icon = new System.Drawing.Icon("icon1.ico");
     this.notifyIcon1.Text = "Tray Icon";
     this.notifyIcon1.Visible = true;
     this.contextMenu1.MenuItems.AddRange(new
         System.Windows.Forms.MenuItem[] {
             this.menuItem1,
             this.menuItem2,
             this.menuItem3});
     this.menuItem1.Index = 0;
     this.menuItem1.Text = "Exit";
     this.menuItem1.Click += new
          System.EventHandler(this.menuItem1_Click);
     this.menuItem2.Index = 1;
     this.menuItem2.Text = "Hide";
     this.menuItem2.Click += new
          System.EventHandler(this.menuItem2_Click);
     this.menuItem3.Index = 2;
     this.menuItem3.Text = "Show";
     this.menuItem3.Click += new
          System.EventHandler(this.menuItem3_Click);
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(504, 365);
     this.Name = "Form1";
     this.Text = "Form1";
     this.ResumeLayout(false);

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

   private void menuItem1_Click(object sender, System.EventArgs e)
   {
     Close();
   }

   private void menuItem2_Click(object sender, System.EventArgs e)
   {
     this.Visible = false;
   }

   private void menuItem3_Click(object sender, System.EventArgs e)
   {
     this.Visible = true;
   }
  }

           
          


Use TrackBar to control label color


   


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

  public class TrackForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TrackBar blueTrackBar;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TrackBar greenTrackBar;
    private System.Windows.Forms.TrackBar redTrackBar;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label lblCurrColor;
    private System.Windows.Forms.PictureBox colorBox;

    public TrackForm()
    {
      InitializeComponent();
      CenterToScreen();         
  
      redTrackBar.Value = 0;
      greenTrackBar.Value = 0;
      blueTrackBar.Value = 0;
      UpdateColor();
    }
    private void InitializeComponent()
    {
      this.label4 = new System.Windows.Forms.Label ();
      this.label1 = new System.Windows.Forms.Label ();
      this.label3 = new System.Windows.Forms.Label ();
      this.label2 = new System.Windows.Forms.Label ();
      this.panel1 = new System.Windows.Forms.Panel ();
      this.redTrackBar = new System.Windows.Forms.TrackBar ();
      this.greenTrackBar = new System.Windows.Forms.TrackBar ();
      this.colorBox = new System.Windows.Forms.PictureBox ();
      this.lblCurrColor = new System.Windows.Forms.Label ();
      this.blueTrackBar = new System.Windows.Forms.TrackBar ();
      redTrackBar.BeginInit ();
      greenTrackBar.BeginInit ();
      blueTrackBar.BeginInit ();

      label4.Location = new System.Drawing.Point (16, 88);
      label4.Text = "Pick your slider here:";
      label4.Size = new System.Drawing.Size (240, 32);
      label4.Font = new System.Drawing.Font ("Microsoft Sans Serif", 15);
      label4.TabIndex = 9;
      label1.Location = new System.Drawing.Point (24, 16);
      label1.Text = "Red:";
      label1.Size = new System.Drawing.Size (88, 32);
      label1.Font = new System.Drawing.Font ("Arial", 15);
      label1.TabIndex = 4;
      label3.Location = new System.Drawing.Point (24, 104);
      label3.Text = "Blue:";
      label3.Size = new System.Drawing.Size (88, 32);
      label3.Font = new System.Drawing.Font ("Arial", 15);
      label3.TabIndex = 6;
      label2.Location = new System.Drawing.Point (24, 64);
      label2.Text = "Green:";
      label2.Size = new System.Drawing.Size (88, 32);
      label2.Font = new System.Drawing.Font ("Arial", 15);
      label2.TabIndex = 5;
      panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      panel1.Location = new System.Drawing.Point (16, 120);
      panel1.Size = new System.Drawing.Size (384, 188);
      panel1.TabIndex = 8;
      panel1.AutoScroll = true;
      redTrackBar.TickFrequency = 5;
      redTrackBar.Location = new System.Drawing.Point (120, 16);
      redTrackBar.TabIndex = 2;
      redTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
      redTrackBar.Maximum = 255;
      redTrackBar.Size = new System.Drawing.Size (232, 42);
      redTrackBar.Scroll += new System.EventHandler (this.redTrackBar_Scroll);
      greenTrackBar.TickFrequency = 5;
      greenTrackBar.Location = new System.Drawing.Point (120, 56);
      greenTrackBar.TabIndex = 3;
      greenTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
      greenTrackBar.Maximum = 255;
      greenTrackBar.Size = new System.Drawing.Size (240, 42);
      greenTrackBar.Scroll += new System.EventHandler (this.greenTrackBar_Scroll);
      colorBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      colorBox.BackColor = System.Drawing.Color.Blue;
      colorBox.Location = new System.Drawing.Point (16, 16);
      colorBox.Size = new System.Drawing.Size (384, 56);
      colorBox.TabIndex = 0;
      colorBox.TabStop = false;
      lblCurrColor.Location = new System.Drawing.Point (16, 324);
      lblCurrColor.Text = "label4";
      lblCurrColor.Size = new System.Drawing.Size (392, 40);
      lblCurrColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      lblCurrColor.Font = new System.Drawing.Font ("Microsoft Sans Serif", 14);
      lblCurrColor.TabIndex = 7;
      blueTrackBar.TickFrequency = 5;
      blueTrackBar.Location = new System.Drawing.Point (120, 96);
      blueTrackBar.TabIndex = 1;
      blueTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
      blueTrackBar.Maximum = 255;
      blueTrackBar.Size = new System.Drawing.Size (240, 42);
      blueTrackBar.Scroll += new System.EventHandler (this.blueTrackBar_Scroll);
      this.Text = "Color Form";
      this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
      this.ClientSize = new System.Drawing.Size (424, 385);
      panel1.Controls.Add (this.label2);
      panel1.Controls.Add (this.blueTrackBar);
      panel1.Controls.Add (this.label3);
      panel1.Controls.Add (this.greenTrackBar);
      panel1.Controls.Add (this.redTrackBar);
      panel1.Controls.Add (this.label1);
      this.Controls.Add (this.label4);
      this.Controls.Add (this.panel1);
      this.Controls.Add (this.lblCurrColor);
      this.Controls.Add (this.colorBox);
      redTrackBar.EndInit ();
      greenTrackBar.EndInit ();
      blueTrackBar.EndInit ();
    }
    static void Main() 
    {
      Application.Run(new TrackForm());
    }

    protected void greenTrackBar_Scroll (object sender, System.EventArgs e)
    {
      UpdateColor();
    }

    protected void redTrackBar_Scroll (object sender, System.EventArgs e)
    {
      UpdateColor();
    }

    protected void blueTrackBar_Scroll (object sender, System.EventArgs e)
    {
      UpdateColor();
    }

    private void UpdateColor()
    {
      Color c = Color.FromArgb(redTrackBar.Value, greenTrackBar.Value, blueTrackBar.Value);
      colorBox.BackColor = c;

      lblCurrColor.Text = "Current color is: " + "(" + 
        redTrackBar.Value + ", " + 
        greenTrackBar.Value + " ," +  
        blueTrackBar.Value + ")";
    }
  }


           
          


Tooltips for Button


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

public class Tooltips : System.Windows.Forms.Form {
    private System.Windows.Forms.Button btnTooltips;
    private System.Windows.Forms.TextBox txtTooltip;
    public Tooltips() {
        ToolTip forButton = new ToolTip();
        forButton.SetToolTip(btnTooltips, "You are now over the button!!");
        forButton.SetToolTip(txtTooltip, "You are now over the textbox!!");
        forButton.AutomaticDelay = 2000;
        this.btnTooltips = new System.Windows.Forms.Button();
        this.txtTooltip = new System.Windows.Forms.TextBox();
        this.SuspendLayout();

        this.btnTooltips.Location = new System.Drawing.Point(0, 152);
        this.btnTooltips.Size = new System.Drawing.Size(288, 104);
        this.btnTooltips.Text = "Press Me";

        this.txtTooltip.Location = new System.Drawing.Point(0, 8);
        this.txtTooltip.Multiline = true;
        this.txtTooltip.Size = new System.Drawing.Size(288, 136);
        this.txtTooltip.Text = "";

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.txtTooltip,
                                      this.btnTooltips});
        this.ResumeLayout(false);

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

    


Tooltips Demo


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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

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

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            ToolTip forButton = new ToolTip();
            forButton.SetToolTip(btnTooltips, "You are now over the button!!");
            forButton.SetToolTip(txtTooltip, "You are now over the textbox!!");
            forButton.AutomaticDelay = 2000;
        }

        /// <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.btnTooltips = new System.Windows.Forms.Button();
            this.txtTooltip = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btnTooltips
            // 
            this.btnTooltips.Location = new System.Drawing.Point(0, 152);
            this.btnTooltips.Name = "btnTooltips";
            this.btnTooltips.Size = new System.Drawing.Size(288, 104);
            this.btnTooltips.TabIndex = 0;
            this.btnTooltips.Text = "Press Me";
            // 
            // txtTooltip
            // 
            this.txtTooltip.Location = new System.Drawing.Point(0, 8);
            this.txtTooltip.Multiline = true;
            this.txtTooltip.Name = "txtTooltip";
            this.txtTooltip.Size = new System.Drawing.Size(288, 136);
            this.txtTooltip.TabIndex = 1;
            this.txtTooltip.Text = "";
            // 
            // Tooltips
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.txtTooltip,
                                                                          this.btnTooltips});
            this.Name = "Tooltips";
            this.Text = "Tooltips";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Tooltips());
        }
    }
}


           
          


Add icon to Tooltip


   


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

public class Form1 : Form
{
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.ToolTip toolTip1;
  private System.Windows.Forms.ToolTip toolTip2;
  private System.Windows.Forms.PictureBox pictureBox2;

  public Form1() {
        InitializeComponent();
  }

  private void InitializeComponent()
  {
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.toolTip1 = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
        this.toolTip2 = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
        this.pictureBox2 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.pictureBox1.Location = new System.Drawing.Point(12, 24);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(100, 50);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.toolTip1.SetToolTip(this.pictureBox1, "This is a tooltip.");
        // 
        // toolTip1
        // 
        this.toolTip1.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
        this.toolTip1.ToolTipTitle = "Titled ToolTip";
        // 
        // pictureBox2
        // 
        this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.pictureBox2.Location = new System.Drawing.Point(148, 24);
        this.pictureBox2.Name = "pictureBox2";
        this.pictureBox2.Size = new System.Drawing.Size(100, 50);
        this.pictureBox2.TabIndex = 1;
        this.pictureBox2.TabStop = false;
        this.toolTip2.SetToolTip(this.pictureBox2, "This is a tooltip.");
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(279, 107);
        this.Controls.Add(this.pictureBox2);
        this.Controls.Add(this.pictureBox1);
        this.Name = "Form1";
        this.Text = "ToolTip Test";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
        this.ResumeLayout(false);

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

}


           
          


Add Tooltip to a component


   

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

public class Form1 : Form
{
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.ToolTip toolTip1;
  private System.Windows.Forms.ToolTip toolTip2;
  private System.Windows.Forms.PictureBox pictureBox2;

  public Form1() {
        InitializeComponent();
  }

  private void InitializeComponent()
  {
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.toolTip1 = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
        this.toolTip2 = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
        this.pictureBox2 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.pictureBox1.Location = new System.Drawing.Point(12, 24);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(100, 50);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.toolTip1.SetToolTip(this.pictureBox1, "This is a tooltip.");
        // 
        // toolTip1
        // 
        this.toolTip1.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
        this.toolTip1.ToolTipTitle = "Titled ToolTip";
        // 
        // pictureBox2
        // 
        this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.pictureBox2.Location = new System.Drawing.Point(148, 24);
        this.pictureBox2.Name = "pictureBox2";
        this.pictureBox2.Size = new System.Drawing.Size(100, 50);
        this.pictureBox2.TabIndex = 1;
        this.pictureBox2.TabStop = false;
        this.toolTip2.SetToolTip(this.pictureBox2, "This is a tooltip.");
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(279, 107);
        this.Controls.Add(this.pictureBox2);
        this.Controls.Add(this.pictureBox1);
        this.Name = "Form1";
        this.Text = "ToolTip Test";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
        this.ResumeLayout(false);

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

}