Custom TreeView

   

/*
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 CustomTreeView
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class CustomTreeView : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ImageList imagesTree;
        private ProjectTree tree;
        private System.ComponentModel.IContainer components;

        public CustomTreeView()
        {
            //
            // 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.components = new System.ComponentModel.Container();
//            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CustomTreeView));
            this.imagesTree = new System.Windows.Forms.ImageList(this.components);
            this.tree = new ProjectTree();
            this.SuspendLayout();
            // 
            // imagesTree
            // 
            this.imagesTree.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imagesTree.ImageSize = new System.Drawing.Size(16, 16);
//            this.imagesTree.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesTree.ImageStream")));
            this.imagesTree.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // tree
            // 
            this.tree.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.tree.ImageList = this.imagesTree;
            this.tree.Location = new System.Drawing.Point(8, 4);
            this.tree.Name = "tree";
            this.tree.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                                                                             new System.Windows.Forms.TreeNode("Unassigned", 0, 0),
                                                                             new System.Windows.Forms.TreeNode("In Progress", 1, 1),
                                                                             new System.Windows.Forms.TreeNode("Closed", 2, 2)});
            this.tree.Scrollable = false;
            this.tree.Size = new System.Drawing.Size(320, 296);
            this.tree.TabIndex = 0;
            // 
            // CustomTreeView
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(336, 310);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.tree});
            this.Name = "CustomTreeView";
            this.Text = "ProjectUserTree";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

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

        private void Form1_Load(object sender, System.EventArgs e)
        {
            tree.AddProject("Migration to .NET", ProjectTree.StatusType.InProgress);
            tree.AddProject("Revamp pricing site", ProjectTree.StatusType.Unassigned);
            tree.AddProject("Prepare L-DAP feasibility report", ProjectTree.StatusType.Unassigned);
            tree.AddProject("Update E201-G to Windows XP", ProjectTree.StatusType.Closed);
            tree.AddProject("Annual meeting", ProjectTree.StatusType.Closed);
        }
    }




        public class ProjectTree : TreeView
        {
            // Use an enumeration to represent the three types of nodes.
            // Specific numbers correspond to the database field code.
            public enum StatusType
            {
                Unassigned = 101,
                InProgress = 102,
                Closed = 103
            }

            // Store references to the three main node branches.
            private TreeNode nodeUnassigned = new TreeNode("Unassigned", 0, 0);
            private TreeNode nodeInProgress = new TreeNode("In Progress", 1, 1);
            private TreeNode nodeClosed = new TreeNode("Closed", 2, 2);

            // Add the main level of nodes when the control is instantiated.
            public ProjectTree() : base()
            {
                base.Nodes.Add(nodeUnassigned);
                base.Nodes.Add(nodeInProgress);
                base.Nodes.Add(nodeClosed);
            }

            // Provide a specialized method the client can use to add nodes.
            public void AddProject(string name, StatusType status)
            {
                TreeNode nodeNew = new TreeNode(name, 3, 4);
                nodeNew.Tag = status;

                switch (status)
                {
                    case StatusType.Unassigned:
                        nodeUnassigned.Nodes.Add(nodeNew);
                        break;
                    case StatusType.InProgress:
                        nodeInProgress.Nodes.Add(nodeNew);
                        break;
                    case StatusType.Closed:
                        nodeClosed.Nodes.Add(nodeNew);
                        break;
                }
            }
        }

    public class ProjectUserTree : TreeView
    {
        // Use an enumeration to represent the three types of nodes.
        public enum NodeType
        {
            Project,
            User
        }

        // Define a new type of higher-level event for node selection.
        public delegate void ItemSelectEventHandler(object sender,
            ItemSelectEventArgs e);

            public class ItemSelectEventArgs : EventArgs
            {
                public NodeType Type;
                public DataRow ItemData;
            }

        // Define the events that use this signature and event arguments.
        public event ItemSelectEventHandler UserSelect;
        public event ItemSelectEventHandler ProjectSelect;
        
        // Store references to the two main node branches.
        private TreeNode nodeProjects = new TreeNode("Projects", 0, 0);
        private TreeNode nodeUsers = new TreeNode("Users", 1, 1);

        // Add the main level of nodes when the control is instantiated.
        public ProjectUserTree() : base()
        {
            base.Nodes.Add(nodeProjects);
            base.Nodes.Add(nodeUsers);
        }

        // Provide a specialized method the client can use to add projects.
        // Store the corresponding DataRow.
        public void AddProject(DataRow project)
        {
            TreeNode nodeNew = new TreeNode(project["Name"].ToString(), 2, 3);
            nodeNew.Tag = project;
            nodeProjects.Nodes.Add(nodeNew);
        }

        // Provide a specialized method the client can use to add users.
        // Store the correspnding DataRow.
        public void AddUser(DataRow user)
        {
            TreeNode nodeNew = new TreeNode(user["Name"].ToString(), 2, 3);
            nodeNew.Tag = user;
            nodeUsers.Nodes.Add(nodeNew);
        }

        // When a node is selected, retrieve the DataRow and raise the event.
        protected override void OnAfterSelect(TreeViewEventArgs e)
         {
            base.OnAfterSelect(e);

                 ItemSelectEventArgs arg = new ItemSelectEventArgs();
                 arg.ItemData = (DataRow)e.Node.Tag;

                 if (e.Node.Parent == nodeProjects)
                 {
                     arg.Type = NodeType.Project;
                     ProjectSelect(this, arg);
                 }
                 else if (e.Node.Parent == nodeUsers)
                 {
                     arg.Type = NodeType.User;
                     UserSelect(this, arg);
                 }

             }
    }
}


           
          


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

           
          


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( &#039;&#039; ) + 1,
                  directory.Length - directory.LastIndexOf( &#039;&#039; ) - 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());
  }

}


           
          


Drag and drop Tree Node


   


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.SplitContainer splitContainer1;
  private System.Windows.Forms.TreeView treeOne;
  private System.Windows.Forms.TreeView treeTwo;
  public Form1() {
        InitializeComponent();
    TreeNode node = treeOne.Nodes.Add("A");
    node.Nodes.Add("A1");
    node.Nodes.Add("A2");
    node.Expand();

    node = treeTwo.Nodes.Add("B");
    node.Nodes.Add("B1");
    node.Nodes.Add("B2");
    node.Expand();

    treeTwo.AllowDrop = true;
    treeOne.AllowDrop = true;

  }
  private void tree_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
    TreeView tree = (TreeView)sender;
    TreeNode node = tree.GetNodeAt(e.X, e.Y);
    tree.SelectedNode = node;

    if (node != null)
    {
      tree.DoDragDrop(node, DragDropEffects.Copy);
    }
  }
  private void tree_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
  {
    TreeView tree = (TreeView)sender;

    e.Effect = DragDropEffects.None;

    TreeNode nodeSource = (TreeNode)e.Data.GetData(typeof(TreeNode));
    if (nodeSource != null)
    {
      if (nodeSource.TreeView != tree)
      {
        Point pt = new Point(e.X, e.Y);
        pt = tree.PointToClient(pt);
        TreeNode nodeTarget = tree.GetNodeAt(pt);
        if (nodeTarget != null)
        {
          e.Effect = DragDropEffects.Copy;
          tree.SelectedNode = nodeTarget;
        }
      }
    }
  }
  private void tree_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  {
    TreeView tree = (TreeView)sender;
    Point pt = new Point(e.X, e.Y);
    pt = tree.PointToClient(pt);
    TreeNode nodeTarget = tree.GetNodeAt(pt);
    TreeNode nodeSource = (TreeNode)e.Data.GetData(typeof(TreeNode));
    nodeTarget.Nodes.Add((TreeNode)nodeSource.Clone());
    nodeTarget.Expand();
  }

  private void InitializeComponent()
  {
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.treeOne = new System.Windows.Forms.TreeView();
        this.treeTwo = new System.Windows.Forms.TreeView();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.Location = new System.Drawing.Point(0, 0);
        this.splitContainer1.Name = "splitContainer1";
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.treeOne);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.treeTwo);
        this.splitContainer1.Size = new System.Drawing.Size(456, 391);
        this.splitContainer1.SplitterDistance = 238;
        this.splitContainer1.TabIndex = 0;
        this.splitContainer1.Text = "splitContainer1";
        // 
        // treeOne
        // 
        this.treeOne.Dock = System.Windows.Forms.DockStyle.Left;
        this.treeOne.HideSelection = false;
        this.treeOne.Location = new System.Drawing.Point(0, 0);
        this.treeOne.Name = "treeOne";
        this.treeOne.Size = new System.Drawing.Size(236, 391);
        this.treeOne.TabIndex = 5;
        this.treeOne.DragDrop += new System.Windows.Forms.DragEventHandler(this.tree_DragDrop);
        this.treeOne.DragOver += new System.Windows.Forms.DragEventHandler(this.tree_DragOver);
        this.treeOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tree_MouseDown);
        // 
        // treeTwo
        // 
        this.treeTwo.Dock = System.Windows.Forms.DockStyle.Fill;
        this.treeTwo.Location = new System.Drawing.Point(0, 0);
        this.treeTwo.Name = "treeTwo";
        this.treeTwo.Size = new System.Drawing.Size(214, 391);
        this.treeTwo.TabIndex = 7;
        this.treeTwo.DragDrop += new System.Windows.Forms.DragEventHandler(this.tree_DragDrop);
        this.treeTwo.DragOver += new System.Windows.Forms.DragEventHandler(this.tree_DragOver);
        this.treeTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tree_MouseDown);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(456, 391);
        this.Controls.Add(this.splitContainer1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "TreeView Drag-And-Drop";
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel2.ResumeLayout(false);
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);

  }

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

}


           
          


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