This tutorial will show how to add a Main Menu Strip and Context Menus to your Windows Forms, as well as how to add code to them to make them functional. This was created with Visual Studio .NET 2008 and C#.

This tutorial and exmaple was written in Visual Studio .NET 2008. Although the same principles apply in 2005, the steps taken may be slightly different.
We are going to explore using Menu Controls in this tutorial for Windows Forms. We will see how we can add a Main Menu Strip at the top of our form (File, Help, etc.) We will also see how we can add Context Menus (Right-click menus) and how we can add code to both these items to make them more functional. We will start by creating a New Project in VS, and choosing Windows Forms, C#.

This is what our finished form will look like:

We are going to create two TextBoxes and allow the user to right-click on them where there will be an option to Perform Calculation. However, this option will only be available if the two TextBoxes are not empty. We can start by adding the Menu Strip – Drag onto the form a MenuStrip Control from the Menus & Toolbars toolbox, or simply double-click it. Once in place, we will be able to edit it. You will see a Type Here box, which will allow us to do just that if we click on it to highlight it. Go ahead and add a File and a Help menu item, then under File, add Exit, and under Help, add About.

Now if you run this program, we will be able to see the MenuStrip, but it doesn’t do anything yet. It has no functionality; it just looks good. To add logic to it, we can click on it once (to select; not highlight) and then either goto the Events window in the Properties and double-click on the Click event, or we can simply double-click on the MenuItem and it will create the handler for us and take us to the code-behind.

First we will add the functionality to the Exit menu item, so double-click on the Exit menu item to generate an Event handler for the Click. Once we are at the code-behind, we can add the following code:

Now when our sub-menu items are clicked, we will have some functionality. The About menu item will display a Message Box to the user with information about the program.
Next, go ahead and add a couple textboxes. Position them where you want, and then add a ContextMenuStrip Control from the toolbox. The positioning of this does not matter as it will be hidden and appear whereever the user right-clicks. Once we have added the Context Menu, we need to specify which Controls will use it. So go to the Properties of each of the text boxes and modify the ContextMenuStrip attribute of both text boxes to the one we just created. Now when the user right-clicks on either of those text boxes, they will get the Context Menu we just created. Although it doesn’t have any menu items as yet.

To select the Context Menu Strip again, click on it in the gray bar at the bottom of our design view. It should then show up on your form. Now we can add Menu Items the same way as we did with the MenuStrip earlier. Let’s add just the one Menu Item – Perform Calculation, and set its Enabled attribute to false. We will enable it again when the text boxes are filled.

Next, add a label to the form. This label will display the result of the calculation. Then create a handler for the menu item’s click event the same way we did the last one. Now we are going to perform the calculation with this:

This program is now fully functional. But we can add some validation to it. It would be wise to disable the Perform Calculation menu item until both text boxes have data (been typed into). We can do this by adding the following code to the text box TextChanged or KeyUp events:

The entire code-behind will look something like this:

Download Source Files