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”

create Context Menu in Windows Forms application using C# z的更多相关文章

  1. Windows Forms Application Creation and Initialization

    Windows Forms Application Creation and Initialization This topic details the steps performed after a ...

  2. Catch Application Exceptions in a Windows Forms Application

    You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. Thi ...

  3. 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别

    原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...

  4. System.Windows.Forms.Application.DoEvents();

    关于Application.DoEvents()的小研究 在MSDN中的备注是: 当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件.该窗体在每次处理事件时,均将处理与该事件关联的 ...

  5. Tools (StExBar vs Cmder)which can switch to command line window on context menu in windows OS

    https://tools.stefankueng.com/StExBar.html https://github.com/cmderdev/cmder

  6. Windows Forms (一)

    导读 1.什么是 Windows Forms 2.需要学Windows Forms 么? 3.如何手写一个简单的Windows Forms 程序 4.对上面程序的说明 5.Form 类与Control ...

  7. 如何为Windows Forms应用程序添加启动参数(Start-Up Parameters)

    很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数. 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件. 特别是对那些需要高度自定义配置的大程 ...

  8. windows安装anaconda 报错failed to create anacoda menu ?

    windows安装anaconda 报错failed to create anacoda menu ? 装了无数次,每次都是 failed to create anacoda menu然后无选择忽略, ...

  9. Easy steps to create a System Tray Application with C# z

    Hiding the C# application to the system tray is quiet straight forward. With a few line of codes in ...

随机推荐

  1. vim中编写python代码使用python-mode和syntastic插件时警告(Warning)的消除

    问题: 在Vim使用了syntastic后,编写代码时,可以对代码错误和警告进行相对实时的了解,对编写代码有很大的帮助.同时这个插件和python-mode一起工作时,可以对python代码的编写提供 ...

  2. XSS动态检测

    0x00 起 前一段时间,因为工作原因接触到XSS漏洞检测.前人留下的锅,是采用pyqt webkit来解析网页内容.作为Python webkit框架,相比于PhantomJS,pyqt在捕获错误, ...

  3. mybatis随意sql语句

    mybatis的mapper.xml随意sql语句, 不管表之间存不存在关系, 都可以使用, 但注意resultMap中一定要指定查询数据返回的列 或 对象(其实就是多列封装到一个对象中) <? ...

  4. HighCharts 根据spline-plot-bands图,定制自己的图(区间里显示多个数据)

    公司项目里有这样一个需求,根据数据绘图,但是数据很多,不可能每个点每个点的去画,这样显示的数据太密集非常的难看(更显得技术不专业),如图: 所以我和项目经理商量如何显示这个图形,按照他的意思是,按照范 ...

  5. Xcode 向6.0以后版本添加iOS开发空白模板

    打开finder,找到应用程序,找到xcode 右键显示包内容.按照如下目录进行查找:Contents ▸ Developer ▸ Platforms ▸ iPhoneOS.platform ▸ De ...

  6. 1060: [ZJOI2007]时态同步 - BZOJ

    Description小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的 ...

  7. tar 解压缩命令

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  8. protobuf 向前兼容向后兼容

    http://blog.163.com/jiang_tao_2010/blog/static/12112689020114305013458/ 不错的protobuf.. protobuf的编码方式: ...

  9. AxureRP制作Tab标签

    1.添加动态Panel 2.双击进入编辑动态Panel 3.点击一个面板状态,编辑全部状态 4.选择虚线框,画出两个矩形图,一大一小:

  10. 中国首个 SaaS 模式的云告警平台安卓版 APP 上线

    今年一月底,国内首个 SaaS 模式的云告警平台 OneAlert 正式发布了 iOS 版 App 客户端,今天上午,安卓版 App 客户端也正式上线了!每个安卓用户,无需电脑,都可以通过手机全程跟踪 ...