ToolStripMenuItem in action : ToolStripMenuItem « GUI Windows Form « C# / C Sharp






ToolStripMenuItem in action

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

}


           
       








Related examples in the same category

1.Use ToolStripMenuItem to set font size