Tooltips Demo

image_pdfimage_print


   

/*
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

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

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 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 Tooltips for Label

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 Form1 : Form
{
   private System.Windows.Forms.Label firstLabel;
   private System.Windows.Forms.Label secondLabel;
   private System.Windows.Forms.ToolTip labelsToolTip;

   public Form1() {
       InitializeComponent();
   }

   private void InitializeComponent()
   {
      this.firstLabel = new System.Windows.Forms.Label();
      this.secondLabel = new System.Windows.Forms.Label();
      this.labelsToolTip = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
      this.SuspendLayout();
      // 
      // firstLabel
      // 
      this.firstLabel.AutoSize = true;
      this.firstLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      this.firstLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.firstLabel.Location = new System.Drawing.Point(12, 20);
      this.firstLabel.Name = "firstLabel";
      this.firstLabel.Size = new System.Drawing.Size(92, 18);
      this.firstLabel.TabIndex = 0;
      this.firstLabel.Text = "This is a label.";
      this.labelsToolTip.SetToolTip(this.firstLabel, "First Label");
      // 
      // secondLabel
      // 
      this.secondLabel.AutoSize = true;
      this.secondLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      this.secondLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.secondLabel.Location = new System.Drawing.Point(12, 69);
      this.secondLabel.Name = "secondLabel";
      this.secondLabel.Size = new System.Drawing.Size(133, 18);
      this.secondLabel.TabIndex = 1;
      this.secondLabel.Tag = "";
      this.secondLabel.Text = "This is another Label.";
      this.labelsToolTip.SetToolTip(this.secondLabel, "Second Label");
      // 
      // ToolTipExampleForm
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(252, 124);
      this.Controls.Add(this.secondLabel);
      this.Controls.Add(this.firstLabel);
      this.Name = "ToolTipExampleForm";
      this.Text = "ToolTip Demonstration";
      this.ResumeLayout(false);
      this.PerformLayout();

   }

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

}


           
          


Use ToolStripMenuItem to set font size

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;

enum TextFontSize {
    FontSizeHuge = 30,
    FontSizeNormal = 20,
    FontSizeTiny = 8
}

public class MainWindow : Form {
    private TextFontSize currFontSize = TextFontSize.FontSizeNormal;

    private ToolStripMenuItem currentCheckedItem;

    public MainWindow() {
        InitializeComponent();
        currentCheckedItem = normalToolStripMenuItem;
        currentCheckedItem.Checked = true;
        this.toolStripTextBoxColor.LostFocus += new EventHandler(toolStripTextBoxColor_LostFocus);
    }
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
        Application.Exit();
    }

    void toolStripTextBoxColor_LostFocus(object sender, EventArgs e) {
        BackColor = Color.FromName(toolStripTextBoxColor.Text);
    }

    private void ContextMenuItemSelection_Clicked(object sender, EventArgs e) {
        currentCheckedItem.Checked = false;
        ToolStripMenuItem miClicked = miClicked = (ToolStripMenuItem)sender;

        if (miClicked.Name == "hugeToolStripMenuItem") {
            currFontSize = TextFontSize.FontSizeHuge;
            currentCheckedItem = hugeToolStripMenuItem;
        }
        if (miClicked.Name == "normalToolStripMenuItem") {
            currFontSize = TextFontSize.FontSizeNormal;
            currentCheckedItem = normalToolStripMenuItem;

        }
        if (miClicked.Name == "tinyToolStripMenuItem") {
            currFontSize = TextFontSize.FontSizeTiny;
            currentCheckedItem = tinyToolStripMenuItem;
        }
        currentCheckedItem.Checked = true;
        Invalidate();
    }
    private void MainWindow_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.DrawString("Right click on me...", new Font("Times New Roman", (float)currFontSize), new SolidBrush(Color.Black), 50, 50);
    }

    private void InitializeComponent() {
        this.mainMenuStrip = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.changeBackgroundColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripTextBoxColor = new System.Windows.Forms.ToolStripTextBox();
        this.hugeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.normalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.tinyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.mainMenuStrip.SuspendLayout();
        this.fontSizeContextStrip.SuspendLayout();
        this.SuspendLayout();
        // 
        this.mainMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.changeBackgroundColorToolStripMenuItem});
        this.mainMenuStrip.Location = new System.Drawing.Point(0, 0);
        this.mainMenuStrip.Name = "mainMenuStrip";
        this.mainMenuStrip.Size = new System.Drawing.Size(300, 24);
        this.mainMenuStrip.TabIndex = 0;
        this.mainMenuStrip.Text = "menuStrip1";

        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
        this.fileToolStripMenuItem.Text = "&amp;File";

        this.exitToolStripMenuItem.Text = "E&amp;xit";
        this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
        // 
        // changeBackgroundColorToolStripMenuItem
        // 
        this.changeBackgroundColorToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripTextBoxColor});
        this.changeBackgroundColorToolStripMenuItem.Name = "changeBackgroundColorToolStripMenuItem";
        this.changeBackgroundColorToolStripMenuItem.Text = "Change Background Color";
        // 
        // toolStripTextBoxColor
        // 
        this.toolStripTextBoxColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
        this.toolStripTextBoxColor.Name = "toolStripTextBoxColor";
        this.toolStripTextBoxColor.Size = new System.Drawing.Size(100, 21);

        this.fontSizeContextStrip.Enabled = true;
        this.fontSizeContextStrip.GripMargin = new System.Windows.Forms.Padding(2);
        this.fontSizeContextStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.hugeToolStripMenuItem,
            this.normalToolStripMenuItem,
            this.tinyToolStripMenuItem});
        this.fontSizeContextStrip.Location = new System.Drawing.Point(25, 90);
        this.fontSizeContextStrip.Name = "contextMenuStrip1";
        this.fontSizeContextStrip.RightToLeft = System.Windows.Forms.RightToLeft.No;
        this.fontSizeContextStrip.Size = new System.Drawing.Size(97, 70);

        this.hugeToolStripMenuItem.Text = "Huge";
        this.hugeToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);

        this.normalToolStripMenuItem.Text = "Normal";
        this.normalToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);

        this.tinyToolStripMenuItem.Text = "Tiny";
        this.tinyToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(300, 145);
        this.ContextMenuStrip = this.fontSizeContextStrip;
        this.Controls.Add(this.mainMenuStrip);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainWindow_Paint);
        this.mainMenuStrip.ResumeLayout(false);
        this.fontSizeContextStrip.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.MenuStrip mainMenuStrip;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem changeBackgroundColorToolStripMenuItem;
    private System.Windows.Forms.ToolStripTextBox toolStripTextBoxColor;
    private System.Windows.Forms.ContextMenuStrip fontSizeContextStrip;
    private System.Windows.Forms.ToolStripMenuItem hugeToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem normalToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem tinyToolStripMenuItem;

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

    


ToolStripMenuItem in action

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 Form1 : Form
{
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem1;
    private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem formatToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem colorToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem blackToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem blueToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem redToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem greenToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem fontToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem timesToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem courierToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem comicToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem boldToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem italicToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator dashToolStripMenuItem;
    private System.Windows.Forms.Label displayLabel;

  public Form1() {
        InitializeComponent();
  }
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      MessageBox.Show("First Line
Second Line",
         "About", MessageBoxButtons.OK, MessageBoxIcon.Information );
    }

   private void exitToolStripMenuItem_Click(object sender, EventArgs e)
   {
      Application.Exit();
   }

   private void ClearColor()
   {
      blackToolStripMenuItem.Checked = false;
      blueToolStripMenuItem.Checked = false;
      redToolStripMenuItem.Checked = false;
      greenToolStripMenuItem.Checked = false;
   } 
   private void blackToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Black;
      blackToolStripMenuItem.Checked = true;
   }
   private void blueToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Blue;
      blueToolStripMenuItem.Checked = true;
   }
   private void redToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();

      displayLabel.ForeColor = Color.Red;
      redToolStripMenuItem.Checked = true;
   }
   private void greenToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Green;
      greenToolStripMenuItem.Checked = true;
   }
   private void ClearFont()
   {
      timesToolStripMenuItem.Checked = false;
      courierToolStripMenuItem.Checked = false;
      comicToolStripMenuItem.Checked = false;
   }
   private void timesToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearFont();

      timesToolStripMenuItem.Checked = true;
      displayLabel.Font = new Font( 
         "Times New Roman", 14, displayLabel.Font.Style );
   }
   private void courierToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearFont();

      courierToolStripMenuItem.Checked = true;
      displayLabel.Font = new Font(
         "Courier", 14, displayLabel.Font.Style );
   }
   private void comicToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearFont();
      comicToolStripMenuItem.Checked = true;
      displayLabel.Font = new Font(
         "Comic Sans MS", 14, displayLabel.Font.Style );
   }
   private void boldToolStripMenuItem_Click(object sender, EventArgs e)
   {
      boldToolStripMenuItem.Checked = !boldToolStripMenuItem.Checked;
      displayLabel.Font = new Font(
         displayLabel.Font.FontFamily, 14,
         displayLabel.Font.Style ^ FontStyle.Bold );
   }
   private void italicToolStripMenuItem_Click(object sender, EventArgs e)
   {
      italicToolStripMenuItem.Checked = !italicToolStripMenuItem.Checked;

      displayLabel.Font = new Font(
         displayLabel.Font.FontFamily, 14,
         displayLabel.Font.Style ^ FontStyle.Italic );
   } 
   private void InitializeComponent()
   {
     this.menuStrip1 = new System.Windows.Forms.MenuStrip();
     this.fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
     this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.formatToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.colorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.blackToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.blueToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.redToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.greenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.fontToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.timesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.courierToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.comicToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.dashToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator();
     this.boldToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.italicToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.displayLabel = new System.Windows.Forms.Label();
     this.menuStrip1.SuspendLayout();
     this.SuspendLayout();
     // 
     // menuStrip1
     // 
     this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.fileToolStripMenuItem1,
        this.formatToolStripMenuItem});
     this.menuStrip1.Location = new System.Drawing.Point(0, 0);
     this.menuStrip1.Name = "menuStrip1";
     this.menuStrip1.Size = new System.Drawing.Size(326, 24);
     this.menuStrip1.TabIndex = 4;
     this.menuStrip1.Text = "menuStrip1";
     // 
     // fileToolStripMenuItem1
     // 
     this.fileToolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.aboutToolStripMenuItem,
        this.exitToolStripMenuItem});
     this.fileToolStripMenuItem1.Name = "fileToolStripMenuItem1";
     this.fileToolStripMenuItem1.Text = "File";
     // 
     // aboutToolStripMenuItem
     // 
     this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
     this.aboutToolStripMenuItem.Text = "About";
     this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
     // 
     // exitToolStripMenuItem
     // 
     this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
     this.exitToolStripMenuItem.Text = "Exit";
     this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
     // 
     // formatToolStripMenuItem
     // 
     this.formatToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.colorToolStripMenuItem,
        this.fontToolStripMenuItem});
     this.formatToolStripMenuItem.Name = "formatToolStripMenuItem";
     this.formatToolStripMenuItem.Text = "Format";
     // 
     // colorToolStripMenuItem
     // 
     this.colorToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.blackToolStripMenuItem,
        this.blueToolStripMenuItem,
        this.redToolStripMenuItem,
        this.greenToolStripMenuItem});
     this.colorToolStripMenuItem.Name = "colorToolStripMenuItem";
     this.colorToolStripMenuItem.Text = "Color";
     // 
     // blackToolStripMenuItem
     // 
     this.blackToolStripMenuItem.Name = "blackToolStripMenuItem";
     this.blackToolStripMenuItem.Text = "Black";
     this.blackToolStripMenuItem.Click += new System.EventHandler(this.blackToolStripMenuItem_Click);
     // 
     // blueToolStripMenuItem
     // 
     this.blueToolStripMenuItem.Name = "blueToolStripMenuItem";
     this.blueToolStripMenuItem.Text = "Blue";
     this.blueToolStripMenuItem.Click += new System.EventHandler(this.blueToolStripMenuItem_Click);
     // 
     // redToolStripMenuItem
     // 
     this.redToolStripMenuItem.Name = "redToolStripMenuItem";
     this.redToolStripMenuItem.Text = "Red";
     this.redToolStripMenuItem.Click += new System.EventHandler(this.redToolStripMenuItem_Click);
     // 
     // greenToolStripMenuItem
     // 
     this.greenToolStripMenuItem.Name = "greenToolStripMenuItem";
     this.greenToolStripMenuItem.Text = "Green";
     this.greenToolStripMenuItem.Click += new System.EventHandler(this.greenToolStripMenuItem_Click);
     // 
     // fontToolStripMenuItem
     // 
     this.fontToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.timesToolStripMenuItem,
        this.courierToolStripMenuItem,
        this.comicToolStripMenuItem,
        this.dashToolStripMenuItem,
        this.boldToolStripMenuItem,
        this.italicToolStripMenuItem});
     this.fontToolStripMenuItem.Name = "fontToolStripMenuItem";
     this.fontToolStripMenuItem.Text = "Font";
     // 
     // timesToolStripMenuItem
     // 
     this.timesToolStripMenuItem.Name = "timesToolStripMenuItem";
     this.timesToolStripMenuItem.Text = "Times New Roman";
     this.timesToolStripMenuItem.Click += new System.EventHandler(this.timesToolStripMenuItem_Click);
     // 
     // courierToolStripMenuItem
     // 
     this.courierToolStripMenuItem.Name = "courierToolStripMenuItem";
     this.courierToolStripMenuItem.Text = "Courier";
     this.courierToolStripMenuItem.Click += new System.EventHandler(this.courierToolStripMenuItem_Click);
     // 
     // comicToolStripMenuItem
     // 
     this.comicToolStripMenuItem.Name = "comicToolStripMenuItem";
     this.comicToolStripMenuItem.Text = "Comic Sans";
     this.comicToolStripMenuItem.Click += new System.EventHandler(this.comicToolStripMenuItem_Click);
     // 
     // dashToolStripMenuItem
     // 
     this.dashToolStripMenuItem.Name = "dashToolStripMenuItem";
     // 
     // boldToolStripMenuItem
     // 
     this.boldToolStripMenuItem.Name = "boldToolStripMenuItem";
     this.boldToolStripMenuItem.Text = "Bold";
     this.boldToolStripMenuItem.Click += new System.EventHandler(this.boldToolStripMenuItem_Click);
     // 
     // italicToolStripMenuItem
     // 
     this.italicToolStripMenuItem.Name = "italicToolStripMenuItem";
     this.italicToolStripMenuItem.Text = "Italic";
     this.italicToolStripMenuItem.Click += new System.EventHandler(this.italicToolStripMenuItem_Click);
     // 
     // displayLabel
     // 
     this.displayLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.displayLabel.Location = new System.Drawing.Point(12, 39);
     this.displayLabel.Name = "displayLabel";
     this.displayLabel.Size = new System.Drawing.Size(293, 89);
     this.displayLabel.TabIndex = 7;
     this.displayLabel.Text = "Text";
     // 
     // MenuTest
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(326, 169);
     this.Controls.Add(this.menuStrip1);
     this.Controls.Add(this.displayLabel);
     this.Name = "MenuTest";
     this.Text = "MenuTest";
     this.menuStrip1.ResumeLayout(false);
     this.ResumeLayout(false);
     this.PerformLayout();

  }

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

}


           
          


Floating Toolbar

image_pdfimage_print


/*
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 FloatingToolbar
{
///

/// Summary description for FloatingToolbar.
///

public class FloatingToolbar : System.Windows.Forms.Form
{
internal System.Windows.Forms.ImageList imgButtons;
internal System.Windows.Forms.ToolBar toolBar1;
internal System.Windows.Forms.ToolBarButton cmdNew;
internal System.Windows.Forms.ToolBarButton cmdOpen;
internal System.Windows.Forms.ToolBarButton cmdClose;
internal System.Windows.Forms.ToolBarButton cmdSave;
internal System.Windows.Forms.ToolBarButton ToolBarButton1;
internal System.Windows.Forms.ToolBarButton cmdPreview;
private System.ComponentModel.IContainer components;

public FloatingToolbar()
{
//
// 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 );
}

#region Windows Form Designer generated code
///

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

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FloatingToolbar));
this.imgButtons = new System.Windows.Forms.ImageList(this.components);
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.cmdNew = new System.Windows.Forms.ToolBarButton();
this.cmdOpen = new System.Windows.Forms.ToolBarButton();
this.cmdClose = new System.Windows.Forms.ToolBarButton();
this.cmdSave = new System.Windows.Forms.ToolBarButton();
this.ToolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.cmdPreview = new System.Windows.Forms.ToolBarButton();
this.SuspendLayout();
//
// imgButtons
//
this.imgButtons.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imgButtons.ImageSize = new System.Drawing.Size(16, 16);
this.imgButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject(“imgButtons.ImageStream”)));
this.imgButtons.TransparentColor = System.Drawing.Color.Transparent;
//
// toolBar1
//
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.cmdNew,
this.cmdOpen,
this.cmdClose,
this.cmdSave,
this.ToolBarButton1,
this.cmdPreview});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imgButtons;
this.toolBar1.Name = “toolBar1”;
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(292, 41);
this.toolBar1.TabIndex = 5;
this.toolBar1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseUp);
this.toolBar1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseMove);
this.toolBar1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseDown);
//
// cmdNew
//
this.cmdNew.ImageIndex = 3;
this.cmdNew.Text = “New”;
//
// cmdOpen
//
this.cmdOpen.ImageIndex = 0;
this.cmdOpen.Text = “Open”;
//
// cmdClose
//
this.cmdClose.ImageIndex = 1;
this.cmdClose.Text = “Close”;
//
// cmdSave
//
this.cmdSave.ImageIndex = 2;
this.cmdSave.Text = “Save”;
//
// ToolBarButton1
//
this.ToolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// cmdPreview
//
this.cmdPreview.ImageIndex = 4;
this.cmdPreview.Text = “Preview”;
//
// FloatingToolbar
//
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.toolBar1});
this.IsMdiContainer = true;
this.Name = “FloatingToolbar”;
this.Text = “Floating Toolbar”;
this.ResumeLayout(false);

}
#endregion

///

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

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

private bool draggingToolbar;
private Point draggedFrom;

private void toolBar1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (draggingToolbar)
{
//if (toolBar1.Dock == DockStyle.Top || toolBar1.Dock == DockStyle.Left)
if (toolBar1.Dock == DockStyle.Top)
{
// Check it the dragging has reached the threshold.
if (draggedFrom.X < (e.X - 20) || draggedFrom.Y < (e.Y - 20)) { draggingToolbar = false; // Disconnect the toolbar. toolBar1.Dock = DockStyle.None; toolBar1.Location = new Point(10, 10); toolBar1.Size = new Size(200, 100); toolBar1.BorderStyle = BorderStyle.FixedSingle; } } else if (toolBar1.Dock == DockStyle.None) { toolBar1.Left = e.X + toolBar1.Left - draggedFrom.X; toolBar1.Top = e.Y + toolBar1.Top - draggedFrom.Y; if (toolBar1.Top < 5) { draggingToolbar = false; // Re-dock the control. toolBar1.Dock = DockStyle.Top; toolBar1.BorderStyle = BorderStyle.None; } else if (toolBar1.Left < 5) { draggingToolbar = false; // Re-dock the control. toolBar1.Dock = DockStyle.Left; toolBar1.BorderStyle = BorderStyle.None; } } } } private void toolBar1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { draggingToolbar = true; draggedFrom = new Point(e.X, e.Y); toolBar1.Capture = true; } private void toolBar1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { draggingToolbar = false; toolBar1.Capture = false; } } } FloatingToolbar.zip( 30 k)[/csharp]