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
- MenuItem (ToolStripMenuItem) : It is used to give simple menu item like “Exit” in above example
- ComboBox (ToolStripComboBox) : It is used to insert a ComboBox in the context menu where user can select or type an item in ComboBox
- Separator (ToolStripSeparator) : It is used to give a horizontal line (ruler) between menu items
- 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的更多相关文章
- Windows Forms Application Creation and Initialization
Windows Forms Application Creation and Initialization This topic details the steps performed after a ...
- Catch Application Exceptions in a Windows Forms Application
You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. Thi ...
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...
- System.Windows.Forms.Application.DoEvents();
关于Application.DoEvents()的小研究 在MSDN中的备注是: 当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件.该窗体在每次处理事件时,均将处理与该事件关联的 ...
- 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
- Windows Forms (一)
导读 1.什么是 Windows Forms 2.需要学Windows Forms 么? 3.如何手写一个简单的Windows Forms 程序 4.对上面程序的说明 5.Form 类与Control ...
- 如何为Windows Forms应用程序添加启动参数(Start-Up Parameters)
很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数. 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件. 特别是对那些需要高度自定义配置的大程 ...
- windows安装anaconda 报错failed to create anacoda menu ?
windows安装anaconda 报错failed to create anacoda menu ? 装了无数次,每次都是 failed to create anacoda menu然后无选择忽略, ...
- 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 ...
随机推荐
- 微软职位内部推荐-SDE II-MODC-Beijing
微软近期Open的职位: JOB TITLE: Software Design Engineer IIDEPARTMENT: Microsoft Office Division ChinaIMMEDI ...
- Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...
- C# - 高级方法参数
可选参数 -必须有个默认值,默认值必须是字面值,常量值,新对象实例或者默认值类型值. public List<string> GetWords( string sentence, bool ...
- 2733: [HNOI2012]永无乡 - BZOJ
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- @Autowired获取配置文件中被注入实例的两种方式
一.说明 二.那么在JavaBean中如何通过@Autowired获取该实例呢?有两种方式: 1.直接获取 @RunWith(SpringJUnit4ClassRunner.class) @Conte ...
- 原创新闻 11 个最佳 jQuery 滚动条插件
通过jQuery滚动条插件,你可以换掉千篇一律的默认浏览器滚动条,让你的网站或web项目更具特色,更有吸引力.本文收集了11款非常漂亮.实用的jQuery滚动条插件,你可以轻松将它们应用在自己的网站中 ...
- 如何使用PHP实现一个WebService
WSDL WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问.这种文 ...
- cannot find w3wp.exe in VS
Same thing was happening to me, when i remembered that IIS has an idle timeout! As above the proc w3 ...
- VC中不同类型DLL及区别
1. DLL的概念可以向程序提供一些函数.变量或类. 静态链接库与动态链接库的区别:(1)静态链接库与动态链接库都是共享代码的方式.静态链接库把最后的指令都包含在最终生成的EXE文件中了:动态链接库不 ...
- CF 369 B. Valera and Contest
http://codeforces.com/contest/369/problem/B 题意 :n, k, l, r, sall, sk,n代表的是n个人,这n个人的总分是sall,每个人的得分大于 ...