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

?





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 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

?





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 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

?





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 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

?





1

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

?





1

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

?





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

?





1

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

?





1

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

时间: 2024-07-29 01:09:08

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 an end-user has run an XAF Windows Forms application, until the moment the main XAF objects, like the WinApplication, are created and initialized. In t

Catch Application Exceptions in a Windows Forms Application

You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. This article really helped me: http://bytes.com/forum/thread236199.html. Application.ThreadException += new ThreadExceptionEventHandler(MyCommonException

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

原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别 .Net Framework中,System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录 如果使用此方法获得应用程序所在的目录,应该注意:System.IO.Directory.GetCurrentDirectory()方法获得的

Windows Forms (一)

导读 1.什么是 Windows Forms 2.需要学Windows Forms 么? 3.如何手写一个简单的Windows Forms 程序 4.对上面程序的说明 5.Form 类与Control类 6.Windows Forms 中的事件处理及手写一个带鼠标移动事件的窗体 什么是Windows Forms         通常我们的说的Windows Forms 指的是一类GUI(图形用户界面)程序的统称,Windows Forms 是随着 .NET 1.0 一起发布的,但市面上的 Win

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

很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数. 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件. 特别是对那些需要高度自定义配置的大程序,经常需要调整运行参数来帮助使用者获得不同的运行结果. 通常,我们可以通过以下两种方式来实现这个需求: 重载入口点函数(Main) 利用Environment类 重载入口点函数(Main) 我们在Visual Studio中创建Windows Forms程序时, VS会自动帮我们创建一个默认的入口

create a C# context menu from code

I try the one of your approach, it works well in my computer. Below is my code: public void AddContextMenu() { ContextMenu mnuContextMenu = new ContextMenu(); mnuContextMenu.MenuItems.Add("&Red LED", new EventHandler(SetDisplayRed)); mnuCont

Show Notepad++ in Windows Explorer context menu

Keywords: Register, Explorer context menu, Edit with Notepad++ Need to show "Edit with Notepad++" in Windows Explorer? If you have a copy (not installed) of Notepad++ and want this command in Explorer, register the following dll. regsvr32  D:\No

Windows context menu shortcut key

Besides the context menu key, you have another option to trigger the context menu: Shift+F10. This is especially useful when your computer/laptop (Lenovo W530) hasn't a context menu key. Reference: Lenovo W530 and the Context Menu Key

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 C# and you can accomplish this. First you will need to create the context menu for the system tray, then after that you need to create the notify icon