Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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 72 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)
{

}

by aisterum on Tuesday, September 14th 2010 at 11:26 PM

Thank you! Very simple and helpful :)

by Johno on Tuesday, October 26th 2010 at 11:17 AM

I bet this don't work for WPF?

by Replica watches on Monday, November 21st 2011 at 12:34 AM

in comparison to spending twenty to thirty thousand on an original Swiss-designed model. In addition, your watch will have the same look and feel of a real watch, and you will have thousands of dollars in savings. This situation is ideal in the event that you need an impressive and stylish luxury timepiece from a world-class brand name, but are not in a position to afford to buy one.

by buy louboutin on Friday, November 25th 2011 at 08:03 PM

I'm fifty several years old user. My living adjustments after i lastly turned to e-cigarettes. It had been correct consider to try and do and that I designed it. Now I really feel healthier along with check out gymnasium following operate.

by long sleeve gowns on Friday, November 25th 2011 at 08:05 PM

Business is never so healthy as when, like a chicken, it must do a certain amount of scratching around for what it gets

by magazine subscriptions to Switzerland on Monday, December 12th 2011 at 04:52 AM

thank u for providing me information on List View.
I want to know how i can display data in datalist in Newsletter layout and Thank you so much, this is easy to understand and fun to continue along with. I am a migrating VB.NET/C# Developer looking for an insight into VC .NET. I've found the VC tutorials on many sites to be confusing, and the navigation around the sites to be near impossible to use! Thanks once again.

by Miami Criminal Defense Attorney on Tuesday, January 3rd 2012 at 07:06 AM

I am happy to find so many useful information here in the post, we need develop more strategies in this regard, thanks for sharing.

by Addison on Monday, January 9th 2012 at 05:26 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by Elizabethton Real Estate on Sunday, February 5th 2012 at 10:22 PM

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 .

by Marco Island FL Real Estate on Monday, February 6th 2012 at 12:31 AM

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

by pagina web crear on Monday, March 5th 2012 at 02:58 PM

I was just browsing through the internet looking for some information and came across. I am impressed by the information. It shows how well you understand this subject. Bookmarked this page, will come back for more.

by michael kors bags on Sunday, March 18th 2012 at 08:55 PM

Thank you for sharing, I will often come to patronize your site

by Milgard Vinyl Windows on Wednesday, March 28th 2012 at 12:44 PM

I always wondered how they built these, its pretty standard, and I would never guess it takes this much effort, but then again I'm still new to the programming crowd. Thanks for the TUT...!

by ShelleyFlynn21 on Saturday, April 7th 2012 at 03:16 PM

If you're in the corner and have got no cash to get out from that, you would require to receive the <a href="http://goodfinance-blog.com/topics/credit-loans">credit loans</a>. Just because it would help you emphatically. I take sba loan every year and feel great just because of it.

by 8 person tents for camping on Thursday, April 12th 2012 at 05:42 AM

I have been absent for a while, but now I remember why I used to love this blog. Thank you


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 >>
Sponsors
Discover Geekpedia
Other Resources
Bargain EZ Tech Coupons & Deals