Home
Username:
Password:
Microsoft Visual Basic 2005 Tutorials

Menus / Creating Context Menus




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

A specialized type of menu is called a Context Menu. You could add a context menu to your application by creating a shortcut menu that pops up when you right click. Generally the items in a context menu are specific to the component to which you are pointing or the options available for that specific component or that situation. Creating a context menu is similar to creating a menu. You add a context menu strip component from the toolbox simply by double clicking and that creates the context menu strip in the components tray. You also see the context menu strip at the top of the form. A context menu does not have a top-level menu like the menu items do. You simply click on the words, type here and type what you want the text of your context menu item to be. For example, I will say exit the project. So now I've created a context menu that will allow the user to right click somewhere on the form and to give them the option of exiting the project. You assign the context menu to the form or control by setting it's context menu strip property. For example, a form has a context menu strip property. By going to the form, find the ContextMenuStrip property. As you can see, by default it is set to none. By clicking the drop-down box, you will see that the context menu strip one is now available. Select that. You've now activated the context menu strip. You can have multiple context menu strips in the same form. If you have only one context menu, you attach it to the form. It will pop up if the user right clicks anywhere on the form, including on a control. However, some controls will have an automatic context menu built into their control. For example, a text box has a built-in context menu that allows the user to cut, Copy and Paste text. If you set the context menu strip property of a text box to your own context menu, your context menu will appear instead of the original that is the default context menu. So let's run our project and at run time I'm going to right click anywhere on the form. As you can see, the option Exit the Project pops up. I click on it. Nothing happens at this point because even though I have activated the context menu, I have not written code for it. So the last step is to activate the context menu strip in the components tray, double click on the Exit the Project item. That opens up the Code Window for me, which I can now write code, which will close the project. Notice I have two me.close lines of code in my program now. I have one for the menu item that's under File and now I have the context menu as well. So if I test the program, right click, exit the project and you can see the program does indeed stop. Of course, this does not affect the File and Exit menu item, which will also cause the program to stop. After you have your program working, experiment with adding more controls and context controls and setting the context menu strip property of those controls. One of the things that you will keep in mind is that you can share the procedures. Sharing an event procedure will be discussed in a separate movie.

Tutorial Information

Course: Microsoft Visual Basic 2005
Author: Arthur Lee
SKU: 33940
ISBN: 1-935320-10-6
Release Date: 2008-11-19
Duration: 7.5 hrs / 97 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available