Create Context Menu in Windows Forms application using C # Z

Source: Internet
Author: User

In this article let us see how to create Context Menu in Windows Forms application using C #

Introduction

In this article we'll see what to create Context menu or Popup menu or Shortcut menu in Windows Forms application with Co Ntextmenustrip Control using C #

Context Menu

Context menu is a group of commands or menu items that can accessed by right click on the control surface. It usually contains some frequently used commands for example Cut, Copy and Paste in a text editor. In the Windows Forms, context menu is created using ContextMenuStrip control and it command or menu items are Toolstripmenuit EM objects.

Creating Context Menu in Design view

    • Create a new Windows Forms application and drag a ContextMenuStrip control on the Form
    • Type Name of menu item in the ComboBox labeled with "Type here" and press Enter. For example, type "Exit" and press Enter
    • Double Click on the menu item (Exit) to write code for its Click event. Write following in its Click event
?
1 2 3 4 private void exitToolStripMenuItem_Click(object sender, EventArgs e) {      Application.Exit();}
    • Set ContextMenuStrip property of the Form to ContextMenuStrip1

In this article let us see how to create Context Menu in Windows Forms application using C #

Introduction

In this article we'll see how to create Context menu or Popup menu or Shortcut menu in Windows Forms applicatio N with ContextMenuStrip control using C #

Context Menu

Context menu is a group of commands or menu items that can accessed by right click on the control surface. It usually contains some frequently used commands for example Cut, Copy and Paste in a text editor. In the Windows Forms, context menu is created using ContextMenuStrip control and it command or menu items are Toolstripmenuit EM objects.

Creating Context Menu in Design view

    • Create a new Windows Forms application and drag a ContextMenuStrip control on the Form
    • Type Name of menu item in the ComboBox labeled with "Type here" and press Enter. For example, type "Exit" and press Enter
    • Double Click on the menu item (Exit) to write code for its Click event. Write following in its Click event
?
1 2 3 4 private void exitToolStripMenuItem_Click(object sender, EventArgs e) {      Application.Exit();}
    • Set ContextMenuStrip property of the Form to ContextMenuStrip1

Creating Context Menu in code behind

    • Create a new Windows Forms application
    • Write code for Createcontextmenu () method and call it after Initializecomponet () method
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public Form1() {      InitializeComponent();      CreateContextMenu(); }private void CreateContextMenu() {      ContextMenuStrip menuStrip = new ContextMenuStrip();      ToolStripMenuItem menuItem = new ToolStripMenuItem("Exit");      menuItem.Click += new EventHandler(menuItem_Click);      menuItem.Name = "Exit";      menuStrip.Items.Add(menuItem);      this.ContextMenuStrip = menuStrip; }
    • Write code for MenuItem Click event
?
1 2 3 4 5 6 7 8 void menuItem_Click(object sender, EventArgs e) {      ToolStripItem menuItem = (ToolStripItem)sender;      if (menuItem.Name == "Exit")      {           Application.Exit();      } }

ADD image to Context Menu items

Image property of ToolStripMenuItem was used to add image to a menu item. In Design view, select menu item and go to it property and click Ellipsis (...) next to the Image property and select Image From Local Resource or Project Resource File.

To add an image to a menu item first add a folder named "Icons" with the project and add images to that folder. I have the added "Close.png" to that folder. Toolstripmenuitem.image is set with a image so can any image to it. You can also give any absolute path in Image.FromFile () method

?
1 menuItem.Image=Image.FromFile("../../icons/Close.png");

Add shortcut keys to Context Menu items

In Design view, use Shorcutkeys property to set shortcuts to the menu items. Select your prefered combination of keys by using checkboxes for Modifiers (Ctrl, Alt and Shift) and selecting Key from Co Mbobox. For example, to-bind shortcut of "alt+x" to Exit, select Alt-CheckBox from Modifiers and select X from Key ComboBox

In code behind, write following to does the same, to add multiple keys separate them using Pipe character

?
1 menuItem.ShortcutKeys = Keys.Alt|Keys.X;

By default, shortcut keys is shown with the menu items. It can be set to hidden by setting Showshortcutkeys property of the menu item to False

?
1 menuItem.ShowShortcutKeys = false;

Show CheckBox to Context Menu items

To show CheckBox in a menu item, set Checked property of ToolStripMenuItem to True. The CheckBox is shown only if Image is not displayed in the menu item. CheckState of CheckBox can is set after setting Checked property to CheckState.Checked, CheckState.Indeterminate or CheckS Tate. Unchecked

?
1 2 menuItem.Checked = true;menuItem.CheckState = CheckState.Checked

To add a toggle CheckBox to the menu item Set CheckOnClick property to True. It toggles checked state of the CheckBox when clicked

?
1 menuItem.CheckOnClick = true;

Set Display Style of Context Menu items

Display style of a toolstripmenuitem can be changed using the Displaystyle property. It can is set to one of the Toolstripitemdisplaystyle enumerators, Toolstripitemdisplaystyle.image, toolstripitemdisplays Tyle. ImageAndText, Toolstripitemdisplaystyle.none or Toolstripitemdisplaystyle.text

?
1 menuItem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;

Type of items in ContextMenuStrip

There is four types of items the can is added to a ContextMenuStrip

    1. MenuItem (ToolStripMenuItem): It's used to give the menu item like "Exit" in above example
    2. ComboBox (Toolstripcombobox): It is used to insert a ComboBox in the context menu where user can select or type an item I N ComboBox
    3. Separator (toolstripseparator): It's used to give a horizontal line (ruler) between menu items
    4. TextBox (Toolstriptextbox): It is used to give a TextBox in context menu where user can enter an item

Type of item can be selected by clicking on the labeled of the ComboBox and "Type here"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.