Geekpedia Programming Tutorials






Making a Context Menu

Shows you how to make a context menu (right click / shortcut menu) in C# using Visual C# .NET and how to attach it to a control.

On Tuesday, July 6th 2004 at 07:17 AM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 54 votes)
Contextual Ads
More C# Resources
Advertisement
You can hardly find an application that doesn't use any context menu. Adding a context menu to your application isn't hard at all, thanks to Visual C# .NET.

Start a new Windows Application project. Look in the Toolbox for ContextMenu:







Add it to the form by double clicking or dragging. After the context menu is added to the application project, when you click on it at the top of the form, below the Title Bar the menu appears waiting to be edited. Of course, that's not where the menu appears when you run the application. The menu appears when you right click the control.











Let's add a few items to the menu. You can add new items just by clicking on the Type Here area. In the following screenshot you can see we added 3 items (Help -> About and Exit):







You can add code to the click event of an item the same way you would with any control. Select the item and switch to Events in the properties window and double click the Click event. This will take you to where you need to insert the code for the event. Here is the code for the event of the 'Exit' item:





private void menuItem3_Click(object sender, System.EventArgs e)

{

   Application.Exit();

}



Ok, we have created the context menu now we need to attach it to the control we need to have a context menu when it is right clicked.

You can attach a context menu to any control that has a ContextMenu property. To a Form, to a ListView, to a DataGrid, just use the Properties window to select a context menu:







And here's the result to attaching the context menu to a form:



Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Predrag Pesic - Peca on Thursday, October 7th 2004 at 01:25 PM

That is O.K.
But, try to make context menu in runtime using Add method, wire up the same handler for different items of that menu and try to achieve different actions.

by Ravi on Saturday, September 17th 2005 at 08:49 AM

Nice and simple tutorial, thanks 8)

by vj on Monday, November 21st 2005 at 09:08 AM

Can you help me create such a context menu in an asp.net application on a Web Form???

by Azzam on Sunday, March 26th 2006 at 04:14 AM

Hi thank you very much . but in my project i fill the menuItem by data taking from a datatable and i add them by code. so i cliked to one item of the menu how can i get this item .
any explanation please mail me.
thank you again

by derpahl on Saturday, August 5th 2006 at 05:47 AM

@azzam

either with the 'sender' object or you just index your menuitems or write something in the tags..

by jabeer on Saturday, April 7th 2007 at 04:46 AM

This Code can be useful only to the beginner.

For the Beginner we have so many like MSDN which is automatically provided by the microsoft.

Please publish your web page with some innotative concepts with the code. Will make this page a world of good.

by Glen McDonald on Thursday, February 14th 2008 at 04:50 AM

As a beginner to C# I found this to be a great reference. It quickly shows me exactly what I need to know.

by ishan on Tuesday, March 4th 2008 at 04:43 AM

this is great. i was unable to create context menu but this helped me a lot. GREAT REFERENCE............................HURAYYYYYYYYY

by Maen on Wednesday, April 30th 2008 at 03:15 AM

thanks you it is very nice and simple to follow

by Sabareesh on Saturday, June 21st 2008 at 04:54 AM

Nice And Simple Toutorial.
Thanks.............

by Lucky on Thursday, September 4th 2008 at 01:17 AM

I want to add a submenu at runtime to existing context menu like shown above. Can anybone help me.

by cnu on Monday, September 8th 2008 at 11:51 PM

how can we assign click event for context menu

by jp on Saturday, May 30th 2009 at 12:12 PM

thanks mate.
good tutorial

by Madhu on Tuesday, June 2nd 2009 at 12:02 AM

Very nice.

by francis on Sunday, January 3rd 2010 at 08:58 AM

here is the simple tutorial

http://www.ceveni.com/2009/10/sample-code-for-context-menu-in-c.html

by AMIT KUMAR SINGH on Friday, January 15th 2010 at 07:59 AM

i want C# code for
finding text i.e FIND ctrl F
find next i.e Findnext F3
in notepade application

by AMIT KUMAR SINGH on Friday, January 15th 2010 at 08:01 AM

void MenuFileFindOnClick(object obj, EventArgs ea)
{
MessageBox.Show("File FIND item clicked!", Text);
}


void MenuFileFindNextOnClick(object obj, EventArgs ea)
{

}

void MenuFileReplaceOnClick(object obj, EventArgs ea)
{

}


void MenuFileFGoToOnClick(object obj, EventArgs ea)
{

}


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons