create Context Menu in Windows Forms application using C# z

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

Introduction

In this article we will see how to create Context Menu or Popup Menu or Shortcut menu in Windows Forms application with ContextMenuStrip control using C#

Context Menu

Context menu is group of commands or menu items that can be 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 Windows Forms, context menu is created using ContextMenuStrip control and its command or menu items are ToolStripMenuItem 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
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 will see how to create Context Menu or Popup Menu or Shortcut menu in Windows Forms application with ContextMenuStrip control using C#

Context Menu

Context menu is group of commands or menu items that can be 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 Windows Forms, context menu is created using ContextMenuStrip control and its command or menu items are ToolStripMenuItem 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
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
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
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 is used to add image to a menu item. In design view, select menu item and go to its property and click ellipsis(…) next to the Image property and select image from Local Resource or Project Resource File.

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

     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 ComboBox. 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 do the same, to add multiple keys separate them using Pipe character

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

By default, shortcut keys are shown with the menu items. It can be set to hidden by setting ShowShortcutKeys property of the menu item to false

     menuItem.ShowShortcutKeys = false;

Show CheckBox to Context Menu items

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

     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 CheckBox when clicked

     menuItem.CheckOnClick = true;

Set Display Style of Context Menu items

Display style of a ToolStripMenuItem can be changed using DisplayStyle property. It can be set to one of the ToolStripItemDisplayStyle enumerators, ToolStripItemDisplayStyle.Image, ToolStripItemDisplayStyle.ImageAndText, ToolStripItemDisplayStyle.None or ToolStripItemDisplayStyle.Text

     menuItem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;

Type of items in ContextMenuStrip

There are four types of items that can be added to a ContextMenuStrip

  1. MenuItem (ToolStripMenuItem) : It is used to give simple 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 in ComboBox
  3. Separator (ToolStripSeparator) : It is 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 arrow of the ComboBox labeled with “Type here”

转载于:https://www.cnblogs.com/zeroone/p/3749955.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值