ContextMenuStrip Control
The ContextMenuStrip control provides functionality of context menus in Visual Studio 2010 and .NET 4.0. A context menu is also known as a popup menu. A context menu appears when you right click on a Form or on a control.
In the previous versions of .NET, the context menu functionality was provided by the ContextMenu control. In .NET 4.0, the ContextMenu control is replaced with the ContextMenuStrip control.
In this article, we will discuss how to build context menu enabled Windows applications using ContextMenuStrip control in Visual Studio 2010.
The ContextMenuStrip control provides functionality of context menus in Visual Studio 2010 and .NET 4.0. A context menu is also known as a popup menu. A context menu appears when you right click on a Form or on a control.
In the previous versions of .NET, the context menu functionality was provided by the ContextMenu control. In .NET 4.0, the ContextMenu control is replaced with the ContextMenuStrip control.
In this article, we will discuss how to build context menu enabled Windows applications using ContextMenuStrip control in Visual Studio 2010.
Creating a Context Menu
To create a ContextMenuStrip control at design-time, you simply drag and drop a ContextMenuStrip control from Toolbox onto a Form in Visual Studio. After you drag and drop a ContextMenuStrip on a Form, the ContextMenuStrip1 is added to the Form and looks like Figure 1. Once a ContextMenuStrip is on the Form, you can add menu items and set its properties and events. If you noticed in Figure 1, first item of the ContextMenuStrip has text Type Here. You can actually start typing here.
Figure 1
If you notice in Figure 2, I type couple of menu items. As soon as you select a menu item, you will see automatically sub menu items areas are editable and you can keep going as many levels you like. I add two menu items and two sub menu items.
Figure 2
We can also create context menus at run-time. Even though you can create a ContextMenuStrip at run-time, it is recommended you create at design-time and then set the properties and methods at run-time.
First step to create a dynamic ContextMenuStrip is to create an instance of ContextMenuStrip class. The following code snippet creates a ContextMenuStrip control object.
C# Code:
ContextMenuStrip PopupMenu = new ContextMenuStrip();
VB.NET Code:
Dim PopupMenu As New ContextMenuStrip()
In the next step, you may set properties of a ContextMenuStrip control. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a ContextMenuStrip.
C# Code:
PopupMenu.BackColor = Color.OrangeRed;
PopupMenu.ForeColor = Color.Black;
PopupMenu.Text = "File Menu";
PopupMenu.Font = new Font("Georgia", 16);
VB.NET Code:
PopupMenu.BackColor = Color.OrangeRed
PopupMenu.ForeColor = Color.Black
PopupMenu.Text = "File Menu"
PopupMenu.Font = New Font("Georgia", 16)
Once the ContextMenuStrip control is ready with its properties, the next step is to add the ContextMenuStrip to a Form. To do so, first we set ContextMenuStrip property and then use call the Show method to display the ContextMenuStrip.
The following code snippet adds a ContextMenuStrip control to the current Form and displays it when you right click on the Form.
C# Code:
this.ContextMenuStrip = PopupMenu;
PopupMenu.Show();
VB.NET Code:
Me.ContextMenuStrip = PopupMenu
PopupMenu.Show()
Setting ContextMenuStrip Properties
After you place a ContextMenuStrip control on a Form, the next step is to set properties.
The C# Menu Controls for Windows Forms offers much more amazing navigation menus than the classic Office style menus and can fully customize item appearance, text, orientation, image, checkbox, and any other controls at design time
ReplyDelete