Add shortcut key to a menu item : Menu « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » Menu 




Add shortcut key to a menu item
Add shortcut key to a menu item

   using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  
  internal struct TheFontSize
  {
    public static int Huge = 30;
    public static int Normal = 20;
    public static int Tiny = 8;
  }
  
  public class mainForm : System.Windows.Forms.Form
  {
    Color currColor = Color.MistyRose;
    private int currFontSize = TheFontSize.Normal;
    private StatusBarPanel sbPnlPrompt = new StatusBarPanel();
    private StatusBarPanel sbPnlTime = new StatusBarPanel();
    private MainMenu mainMenu = new MainMenu();
    
    private MenuItem currentCheckedItem;
    private MenuItem checkedHuge;
    private MenuItem checkedNormal;
    private MenuItem checkedTiny;


    public mainForm()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292273);

      this.MenuComplete += new EventHandler(StatusForm_MenuDone);

      BuildMenuSystem();
      BuildStatBar();
    }

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

    private void FileExit_Clicked(object sender, EventArgs e
    {
      Console.WriteLine("File | Exit Menu item handler");
      this.Close();
    }

    private void FileSave_Clicked(object sender, EventArgs e
    {
            Console.WriteLine("File | Save Menu item handler");
    }

    private void ColorItem_Clicked(object sender, EventArgs e
    {
      MenuItem miClicked = (MenuItem)sender;
      string color = miClicked.Text.Remove(0,1);
      
      this.BackColor = Color.FromName(color);
      currColor = this.BackColor;
    }

    private void PopUp_Clicked(object sender, EventArgs e
    {
      currentCheckedItem.Checked = false;

      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text;
      
      if(item == "Huge") {
        currFontSize = TheFontSize.Huge;
        currentCheckedItem = checkedHuge;
      }else if(item == "Normal") {
        currFontSize = TheFontSize.Normal;
        currentCheckedItem = checkedNormal;
      }else if(item == "Tiny") {
        currFontSize = TheFontSize.Tiny;
        currentCheckedItem = checkedTiny;
      }
      currentCheckedItem.Checked = true;
      Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics g = e.Graphics;

      g.DrawString("www.java2s.com"
        new Font("Times New Roman"(float)currFontSize)
        new SolidBrush(Color.Black)
        this.DisplayRectangle);
    }
    
    protected override void OnResize(EventArgs e)
    {
      base.OnResize(e);
      Invalidate();
    }

    private void HelpAbout_Clicked(object sender, EventArgs e
    {
      Console.WriteLine("The amazing final app...""About...");
    }
        
    private void FileMenuItem_Selected(object sender, EventArgs e
    {
      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text.Remove(0,1);
      
      if(item == "Save..."){
        sbPnlPrompt.Text = "Save current settings.";     
      }else{
        sbPnlPrompt.Text = "Terminates this app.";     
        
    }

    private void ColorMenuItem_Selected(object sender, EventArgs e
    {
      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text.Remove(0,1);
      sbPnlPrompt.Text = "Select " + item;            
    }

    private void HelpAbout_Selected(object sender, EventArgs e
    {
      sbPnlPrompt.Text = "Displays app info";
    }

    private void StatusForm_MenuDone(object sender, EventArgs e
    {
      sbPnlPrompt.Text = "Ready";
    }

    private void timer1_Tick(object sender, EventArgs e
    {
      DateTime t = DateTime.Now;
      string s = t.ToLongTimeString() ;
      sbPnlTime.Text = s ;    
    }

    private void BuildMenuSystem()
    {
      MenuItem miFile = mainMenu.MenuItems.Add("&File");           
      miFile.MenuItems.Add(new MenuItem("&Save..."new EventHandler(this.FileSave_Clicked), Shortcut.CtrlS));     
      miFile.MenuItems.Add(new MenuItem("E&xit"new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));

      miFile.MenuItems[0].Select += new EventHandler(FileMenuItem_Selected);
      miFile.MenuItems[1].Select += new EventHandler(FileMenuItem_Selected);

      MenuItem miColor = mainMenu.MenuItems.Add("&Background Color");
      miColor.MenuItems.Add("&DarkGoldenrod"new EventHandler(ColorItem_Clicked));
      miColor.MenuItems.Add("&GreenYellow"new EventHandler(ColorItem_Clicked));
      
      for(int i = 0; i < miColor.MenuItems.Count; i++){
        miColor.MenuItems[i].Select += new EventHandler(ColorMenuItem_Selected);
            }

      MenuItem miHelp = mainMenu.MenuItems.Add("Help");  
      miHelp.MenuItems.Add(new MenuItem("&About"new EventHandler(this.HelpAbout_Clicked), Shortcut.CtrlA));
      miHelp.MenuItems[0].Select += new EventHandler(HelpAbout_Selected);

      this.Menu = mainMenu;  

            ContextMenu popUpMenu = new ContextMenu();

      popUpMenu.MenuItems.Add("Huge"new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Normal"new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Tiny"new EventHandler(PopUp_Clicked));

      this.ContextMenu = popUpMenu;

      checkedHuge = this.ContextMenu.MenuItems[0];
      checkedNormal = this.ContextMenu.MenuItems[1];        
      checkedTiny = this.ContextMenu.MenuItems[2];
      
      if(currFontSize == TheFontSize.Huge)
        currentCheckedItem = checkedHuge;
      else if(currFontSize == TheFontSize.Normal)
        currentCheckedItem = checkedNormal;
      else
        currentCheckedItem = checkedTiny;

      currentCheckedItem.Checked = true;
    }

    private void BuildStatBar()
    {
        Timer timer1 = new Timer();
      timer1.Interval = 1000;
      timer1.Enabled = true;
      timer1.Tick += new EventHandler(timer1_Tick);

        StatusBar statusBar = new StatusBar();
        
      statusBar.ShowPanels = true;
      statusBar.Panels.AddRange((StatusBarPanel[])new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});

      sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
      sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
      sbPnlPrompt.Width = 62;
      sbPnlPrompt.Text = "Ready";

      sbPnlTime.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
      sbPnlTime.Width = 76;

      try
      {
        Icon i = new Icon("icon1.ico");
        sbPnlPrompt.Icon = i;
      catch(Exception e) {
        MessageBox.Show(e.Message);
      }

      this.Controls.Add(statusBar);  
    }

  }


           
       














Related examples in the same category
1.Add context menu to a form windowAdd context menu to a form window
2.Build Menu and context sensitive menu on your ownBuild Menu and context sensitive menu on your own
3.Form MenuForm Menu
4.Add a Main MenuAdd a Main Menu
5.Menu Text Provider HostMenu Text Provider Host
6.Dynamic Menu
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.