Using ContextMenus in C#

In my last post I was talking about ContextMenus and ContextMenuStrips. In this one, I will try to explain some of the ways you can make use of that control. Before that a quick recap. ContextMenus are obsolete. Now a days we use ContextMenuStrip. It has many cool facilities that makes your application more eye cathy (can’t beat mac though). Lets begin with

Remember: ContextMenu, menu, ContextMenuStrip means ContextMenuStrip in the following explanations

Adding a ContextMenuStrip
ContextMenuStrip can be found under System.Windows.Forms.ContextMenuStrip. So to have a menu control, just create an object of the above

System.Windows.Forms.ContextMenuStrip myContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); //Just creates a usable object. Nothing else

Adding items to the ContextMenuStrip
Adding items to the menu is easy (as usual). Use the Items.Add() method to do so.

myContextMenu.Items.Add("foo"); //Now you can see and click foo from the contextmenu

Actually all the items that we add to the menu will be of type ToolStripMenuItem. So we can also create an object of such a type and wrap it with the additional information.

Associating images with the menu items
You can have images to the left of each and every item. To have such a thing, first we need to enable that functionality. Use the ShowImageMargin to do so. ShowImageMargin is a small margin to the left of the menustrip where the system can render some cool and small icons. Now we need to have some images in our project. Ok. Now things can be done in two ways.

Using ImageLists
Add an ImageList control in to your application and drag some pictures in to it. Don’t know how ?? I will tell you. Now we have a ImageList control ready with some images in it(considering), we can associate this with the menu items using the ImageList property (used mainly with ToolBarStrip control).


myContexMenuStrip.ShowImageMargin = true; //To see the image margin with the items
myContextMenuStrip.ImageList = myImageList; //Associating the image list with the menu. This is actually useless. WIll be usefull only when you deal with toolbastrip
myContextMenuStrip.Items.Add("foo", myImageList.Images[0]); //Will show the first image for the first item
myContextMenuStrip.Items.Add("bar", myImageList.Images[1]); //Will show the second image for the second item and so on...

Using images directly
Lets import some images in to the global resource first. Now to add an image with an item,

myContextMenuStrip.ShowImageMargin = true;
myContextMenuStrip.Items.Add("foo",<your_namespace>.Properties.Resources.<image_name>); //the second parameter may differ based on the place where you have put the image. Use VS's intellisence to figure out your image. I have written the default object hierarcy.

Items and event handling
To associate an event with any item, we will have to subscribe for the event first and then write the handler.

myContextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(myComtextMenuStrip_
ItemClicked);
//Subscribing for an event and initialising the listener (event handler)
void myContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
//EventHandler
{
  MessageBox.Show(e.ClickedItem.Text); //Will show the clicked item
}

You can even associate more events using the ToolStripMenuItem object. Take a look at the following example,

ToolStripMenuItem aItem = new ToolStripMenuItem();
aItem.Text = "spams";
myContextMenuStrip.Items.Add(aItem);  //Adding the ToolStripMenuItem
aItem.Checked = true;  //Enabling checkboxes that can appear to the left of the ContextMenuItems
aItem.CheckedChanged += new EventHandler(aItem_CheckedChanged);
void aItem_CheckedChanged(object sender, EventArgs e)  //EventHandler
{
  ToolStripMenuItem aItem = sender as ToolStripMenuItem;
  MessageBox.Show(aItem.Checked.ToString());
}

Showing the contextmenus

To show, use the Show() method. For example,

private void Form1_Click(object sender, EventArgs e)
{
  MouseEventArgs args = e as MouseEventArgs;
  if (args.Button == MouseButtons.Right)
  {
    myContextMenuStrip.Show(this, new Point(args.X, args.Y));
  }
}

I have talked about just a few of the features provided by ContextMenuStrip. You have many. Comment if you know something else about this control.

Tags: , ,

One comment

  1. I am now learning c# and this tutorial is usefull :)