Add Tooltips for Label


   


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

   
 

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 = "&File";

        this.exitToolStripMenuItem.Text = "E&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


   


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


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

ToolBar Example

   

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

  /// <summary>
  /// Summary description for ToolBarExample.
  /// </summary>
  public class ToolBarExample : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.ContextMenu ContextMenu1;
    internal System.Windows.Forms.MenuItem MenuItem1;
    internal System.Windows.Forms.MenuItem MenuItem2;
    internal System.Windows.Forms.MenuItem MenuItem3;
    internal System.Windows.Forms.ImageList imagesToolBar;
    internal System.Windows.Forms.ImageList imagesLarge;
    internal System.Windows.Forms.ToolBar ToolBar4;
    internal System.Windows.Forms.ToolBarButton ToolBarButton13;
    internal System.Windows.Forms.ToolBarButton ToolBarButton16;
    internal System.Windows.Forms.ToolBar ToolBar3;
    internal System.Windows.Forms.ToolBarButton ToolBarButton8;
    internal System.Windows.Forms.ToolBarButton ToolBarButton9;
    internal System.Windows.Forms.ToolBarButton ToolBarButton12;
    internal System.Windows.Forms.ToolBar ToolBar2;
    internal System.Windows.Forms.ToolBarButton ToolBarButton4;
    internal System.Windows.Forms.ToolBarButton ToolBarButton5;
    internal System.Windows.Forms.ToolBarButton ToolBarButton6;
    internal System.Windows.Forms.ToolBarButton ToolBarButton7;
    internal System.Windows.Forms.ToolBar ToolBar1;
    internal System.Windows.Forms.ToolBarButton ToolBarButton1;
    internal System.Windows.Forms.ToolBarButton ToolBarButton2;
    internal System.Windows.Forms.ToolBarButton ToolBarButton3;
    internal System.Windows.Forms.ToolBarButton ToolBarButton10;
    internal System.Windows.Forms.Label Label4;
    internal System.Windows.Forms.Label Label3;
    internal System.Windows.Forms.Label Label2;
    internal System.Windows.Forms.Label Label1;
    private System.ComponentModel.IContainer components;

    public ToolBarExample()
    {
      //
      // 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(ToolBarExample));
      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.imagesToolBar = new System.Windows.Forms.ImageList(this.components);
      this.imagesLarge = new System.Windows.Forms.ImageList(this.components);
      this.ToolBar4 = new System.Windows.Forms.ToolBar();
      this.ToolBarButton13 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton16 = new System.Windows.Forms.ToolBarButton();
      this.ToolBar3 = new System.Windows.Forms.ToolBar();
      this.ToolBarButton8 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton9 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton12 = new System.Windows.Forms.ToolBarButton();
      this.ToolBar2 = new System.Windows.Forms.ToolBar();
      this.ToolBarButton4 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton5 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton6 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton7 = new System.Windows.Forms.ToolBarButton();
      this.ToolBar1 = new System.Windows.Forms.ToolBar();
      this.ToolBarButton1 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton2 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton3 = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton10 = new System.Windows.Forms.ToolBarButton();
      this.Label4 = new System.Windows.Forms.Label();
      this.Label3 = new System.Windows.Forms.Label();
      this.Label2 = new System.Windows.Forms.Label();
      this.Label1 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // ContextMenu1
      // 
      this.ContextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                             this.MenuItem1,
                                             this.MenuItem2,
                                             this.MenuItem3});
      // 
      // MenuItem1
      // 
      this.MenuItem1.Index = 0;
      this.MenuItem1.Text = "Menu 1";
      // 
      // MenuItem2
      // 
      this.MenuItem2.Index = 1;
      this.MenuItem2.Text = "Menu 2";
      // 
      // MenuItem3
      // 
      this.MenuItem3.Index = 2;
      this.MenuItem3.Text = "Menu 3";
      // 
      // imagesToolBar
      // 
      this.imagesToolBar.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imagesToolBar.ImageSize = new System.Drawing.Size(16, 16);
      this.imagesToolBar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesToolBar.ImageStream")));
      this.imagesToolBar.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // imagesLarge
      // 
      this.imagesLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imagesLarge.ImageSize = new System.Drawing.Size(32, 32);
      this.imagesLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesLarge.ImageStream")));
      this.imagesLarge.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ToolBar4
      // 
      this.ToolBar4.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
      this.ToolBar4.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                            this.ToolBarButton13,
                                            this.ToolBarButton16});
      this.ToolBar4.DropDownArrows = true;
      this.ToolBar4.ImageList = this.imagesToolBar;
      this.ToolBar4.Location = new System.Drawing.Point(0, 89);
      this.ToolBar4.Name = "ToolBar4";
      this.ToolBar4.ShowToolTips = true;
      this.ToolBar4.Size = new System.Drawing.Size(344, 25);
      this.ToolBar4.TabIndex = 9;
      this.ToolBar4.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
      // 
      // ToolBarButton13
      // 
      this.ToolBarButton13.DropDownMenu = this.ContextMenu1;
      this.ToolBarButton13.ImageIndex = 0;
      this.ToolBarButton13.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
      this.ToolBarButton13.Text = "One";
      // 
      // ToolBarButton16
      // 
      this.ToolBarButton16.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
      // 
      // ToolBar3
      // 
      this.ToolBar3.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
      this.ToolBar3.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                            this.ToolBarButton8,
                                            this.ToolBarButton9,
                                            this.ToolBarButton12});
      this.ToolBar3.DropDownArrows = true;
      this.ToolBar3.ImageList = this.imagesToolBar;
      this.ToolBar3.Location = new System.Drawing.Point(0, 64);
      this.ToolBar3.Name = "ToolBar3";
      this.ToolBar3.ShowToolTips = true;
      this.ToolBar3.Size = new System.Drawing.Size(344, 25);
      this.ToolBar3.TabIndex = 8;
      this.ToolBar3.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
      // 
      // ToolBarButton8
      // 
      this.ToolBarButton8.ImageIndex = 0;
      this.ToolBarButton8.Pushed = true;
      this.ToolBarButton8.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
      this.ToolBarButton8.Text = "One";
      // 
      // ToolBarButton9
      // 
      this.ToolBarButton9.ImageIndex = 1;
      this.ToolBarButton9.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
      this.ToolBarButton9.Text = "Two";
      // 
      // ToolBarButton12
      // 
      this.ToolBarButton12.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
      // 
      // ToolBar2
      // 
      this.ToolBar2.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
      this.ToolBar2.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                            this.ToolBarButton4,
                                            this.ToolBarButton5,
                                            this.ToolBarButton6,
                                            this.ToolBarButton7});
      this.ToolBar2.DropDownArrows = true;
      this.ToolBar2.ImageList = this.imagesToolBar;
      this.ToolBar2.Location = new System.Drawing.Point(0, 39);
      this.ToolBar2.Name = "ToolBar2";
      this.ToolBar2.ShowToolTips = true;
      this.ToolBar2.Size = new System.Drawing.Size(344, 25);
      this.ToolBar2.TabIndex = 7;
      this.ToolBar2.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
      // 
      // ToolBarButton4
      // 
      this.ToolBarButton4.ImageIndex = 0;
      this.ToolBarButton4.Text = "One";
      // 
      // ToolBarButton5
      // 
      this.ToolBarButton5.ImageIndex = 1;
      this.ToolBarButton5.Text = "Two";
      // 
      // ToolBarButton6
      // 
      this.ToolBarButton6.ImageIndex = 2;
      this.ToolBarButton6.Text = "Three";
      // 
      // ToolBarButton7
      // 
      this.ToolBarButton7.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
      // 
      // ToolBar1
      // 
      this.ToolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
      this.ToolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                            this.ToolBarButton1,
                                            this.ToolBarButton2,
                                            this.ToolBarButton3,
                                            this.ToolBarButton10});
      this.ToolBar1.DropDownArrows = true;
      this.ToolBar1.ImageList = this.imagesToolBar;
      this.ToolBar1.Name = "ToolBar1";
      this.ToolBar1.ShowToolTips = true;
      this.ToolBar1.Size = new System.Drawing.Size(344, 39);
      this.ToolBar1.TabIndex = 6;
      // 
      // ToolBarButton1
      // 
      this.ToolBarButton1.ImageIndex = 0;
      this.ToolBarButton1.Text = "One";
      // 
      // ToolBarButton2
      // 
      this.ToolBarButton2.ImageIndex = 1;
      this.ToolBarButton2.Text = "Two";
      // 
      // ToolBarButton3
      // 
      this.ToolBarButton3.ImageIndex = 2;
      this.ToolBarButton3.Text = "Three";
      // 
      // ToolBarButton10
      // 
      this.ToolBarButton10.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
      // 
      // Label4
      // 
      this.Label4.ForeColor = System.Drawing.Color.Navy;
      this.Label4.Location = new System.Drawing.Point(104, 92);
      this.Label4.Name = "Label4";
      this.Label4.Size = new System.Drawing.Size(112, 16);
      this.Label4.TabIndex = 13;
      this.Label4.Text = "Drop-Down Button";
      // 
      // Label3
      // 
      this.Label3.ForeColor = System.Drawing.Color.Navy;
      this.Label3.Location = new System.Drawing.Point(136, 68);
      this.Label3.Name = "Label3";
      this.Label3.Size = new System.Drawing.Size(88, 16);
      this.Label3.TabIndex = 12;
      this.Label3.Text = "Toggle Buttons";
      // 
      // Label2
      // 
      this.Label2.ForeColor = System.Drawing.Color.Navy;
      this.Label2.Location = new System.Drawing.Point(192, 40);
      this.Label2.Name = "Label2";
      this.Label2.Size = new System.Drawing.Size(72, 16);
      this.Label2.TabIndex = 11;
      this.Label2.Text = "Text on Right";
      // 
      // Label1
      // 
      this.Label1.ForeColor = System.Drawing.Color.Navy;
      this.Label1.Location = new System.Drawing.Point(128, 12);
      this.Label1.Name = "Label1";
      this.Label1.Size = new System.Drawing.Size(64, 16);
      this.Label1.TabIndex = 10;
      this.Label1.Text = "Flat Buttons";
      // 
      // ToolBarExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(344, 266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.Label4,
                                      this.Label3,
                                      this.Label2,
                                      this.Label1,
                                      this.ToolBar4,
                                      this.ToolBar3,
                                      this.ToolBar2,
                                      this.ToolBar1});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.Name = "ToolBarExample";
      this.Text = "ToolBarExample";
      this.ResumeLayout(false);

    }
    #endregion

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




           
          


ToolBarExample.zip( 17 k)

ToolBar float 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 Palette : Form
{
  private System.Windows.Forms.Label lblPictureThree;
  private System.Windows.Forms.Label lblPictureTwo;
  private System.Windows.Forms.Label lblPictureOne;

  public Palette() {
        InitializeComponent();
  }

  private void lbl_MouseDown(object sender, MouseEventArgs e)
  {
    Label lbl = (Label)sender;
    lbl.DoDragDrop(lbl.Image, DragDropEffects.Copy);

  }
  private void InitializeComponent()
  {
    this.lblPictureThree = new System.Windows.Forms.Label();
    this.lblPictureTwo = new System.Windows.Forms.Label();
    this.lblPictureOne = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblPictureThree
    // 
    this.lblPictureThree.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureThree.Image = new Bitmap("winter.jpg");
    this.lblPictureThree.Location = new System.Drawing.Point(12, 113);
    this.lblPictureThree.Name = "lblPictureThree";
    this.lblPictureThree.Size = new System.Drawing.Size(56, 48);
    this.lblPictureThree.TabIndex = 6;
    this.lblPictureThree.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // lblPictureTwo
    // 
    this.lblPictureTwo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureTwo.Image = new Bitmap("winter.jpg");
    this.lblPictureTwo.Location = new System.Drawing.Point(12, 61);
    this.lblPictureTwo.Name = "lblPictureTwo";
    this.lblPictureTwo.Size = new System.Drawing.Size(56, 48);
    this.lblPictureTwo.TabIndex = 5;
    this.lblPictureTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // lblPictureOne
    // 
    this.lblPictureOne.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureOne.Image = new Bitmap("winter.jpg");
    this.lblPictureOne.Location = new System.Drawing.Point(12, 9);
    this.lblPictureOne.Name = "lblPictureOne";
    this.lblPictureOne.Size = new System.Drawing.Size(56, 48);
    this.lblPictureOne.TabIndex = 4;
    this.lblPictureOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // Palette
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(83, 173);
    this.Controls.Add(this.lblPictureTwo);
    this.Controls.Add(this.lblPictureOne);
    this.Controls.Add(this.lblPictureThree);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
    this.Name = "Palette";
    this.ShowInTaskbar = false;
    this.Text = "Palette";
    this.ResumeLayout(false);

  }

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

}

  public class DrawingArea : Form
  {
    private System.Windows.Forms.PictureBox picDrawingArea;

    public DrawingArea()
    {
      InitializeComponent();
    }

    private void DrawingArea_Load(object sender, EventArgs e)
    {
      Palette frmTool = new Palette();
      this.AddOwnedForm(frmTool);
      frmTool.Show();
      picDrawingArea.AllowDrop = true;
    }

    private void picDrawingArea_DragEnter(object sender, DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.Bitmap))
      {
        e.Effect = DragDropEffects.Copy;
      }
      else
      {
        e.Effect = DragDropEffects.None;
      }
    }

    private void picDrawingArea_DragDrop(object sender, DragEventArgs e)
    {
      Graphics g = picDrawingArea.CreateGraphics();
      g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap),
        new Point(e.X - this.Left, e.Y - this.Top));

    }
     private void InitializeComponent()
    {
      this.picDrawingArea = new System.Windows.Forms.PictureBox();
      ((System.ComponentModel.ISupportInitialize)(this.picDrawingArea)).BeginInit();
      this.SuspendLayout();
      // 
      // picDrawingArea
      // 
      this.picDrawingArea.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
      this.picDrawingArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.picDrawingArea.Location = new System.Drawing.Point(1, 2);
      this.picDrawingArea.Name = "picDrawingArea";
      this.picDrawingArea.Size = new System.Drawing.Size(377, 270);
      this.picDrawingArea.TabIndex = 2;
      this.picDrawingArea.TabStop = false;
      this.picDrawingArea.DragDrop += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragDrop);
      this.picDrawingArea.DragEnter += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragEnter);
      // 
      // DrawingArea
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(379, 274);
      this.Controls.Add(this.picDrawingArea);
      this.Name = "DrawingArea";
      this.Text = "Drawing Area";
      this.Load += new System.EventHandler(this.DrawingArea_Load);
      ((System.ComponentModel.ISupportInitialize)(this.picDrawingArea)).EndInit();
      this.ResumeLayout(false);
    }
  }

           
          


Using ToolBarButton

using System;
using System.Drawing;
using System.Windows.Forms;

class ToggleButtons: Form
{
protected Panel panel = new Panel();
protected ToolBar tbar;
protected string strText = “Toggle”;
protected Color clrText = SystemColors.WindowText;
FontStyle fontstyle = FontStyle.Regular;

public static void Main()
{
Application.Run(new ToggleButtons());
}
public ToggleButtons()
{
panel.Parent = this;
panel.Dock = DockStyle.Fill;
panel.BackColor = SystemColors.Window;
panel.ForeColor = SystemColors.WindowText;
panel.Resize += new EventHandler(PanelOnResize);
panel.Paint += new PaintEventHandler(PanelOnPaint);

Bitmap bm = new Bitmap(GetType(), “ToggleButtons.bmp”);

ImageList imglst = new ImageList();
imglst.ImageSize = new Size(bm.Width / 4, bm.Height);
imglst.Images.AddStrip(bm);
imglst.TransparentColor = Color.White;

tbar = new ToolBar();
tbar.ImageList = imglst;
tbar.Parent = this;
tbar.ShowToolTips = true;
tbar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBarOnClick);

FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
FontStyle.Underline, FontStyle.Strikeout };

for (int i = 0; i < 4; i++) { ToolBarButton tbarbtn = new ToolBarButton(); tbarbtn.ImageIndex = i; tbarbtn.Style = ToolBarButtonStyle.ToggleButton; tbarbtn.ToolTipText = afs[i].ToString(); tbarbtn.Tag = afs[i]; tbar.Buttons.Add(tbarbtn); } } void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea) { ToolBarButton tbarbtn = tbbcea.Button; if (tbarbtn.Tag == null || tbarbtn.Tag.GetType() != typeof(FontStyle)) return; if (tbarbtn.Pushed) fontstyle |= (FontStyle) tbarbtn.Tag; else fontstyle &= ~(FontStyle) tbarbtn.Tag; panel.Invalidate(); } void PanelOnResize(object obj, EventArgs ea) { Panel panel = (Panel) obj; panel.Invalidate(); } void PanelOnPaint(object obj, PaintEventArgs pea) { Panel panel = (Panel) obj; Graphics grfx = pea.Graphics; Font font = new Font("Times New Roman", 72, fontstyle); SizeF sizef = grfx.MeasureString(strText, font); grfx.DrawString(strText, font, new SolidBrush(clrText), (panel.Width - sizef.Width) / 2, (panel.Height - sizef.Height) / 2); } } [/csharp]