CONTEXT MENUS


A second type of menu system that you might want to add to your Visual C++ applications is a context menu. Context menus are hidden from view, only appearing when the user right-clicks on the form or control with which the context menu is associated.

Hint 

A context menu is a menu system that you can attach to a form or control to provide the form or control with additional options or functionality.

The following steps outline the process involved in setting up a context menu:

  1. Double-click on the ContextMenuStrip control located in the Toolbox to add it to the Component Tray.

  2. Add menu items to it by keying in text on top of the Type Here text, as demonstrated in Figure 4.24.

  3. Double-click on each of the menu items in the context list and add your program code, as demonstrated next. For example, you might add the following statement to the click event for the first context menu:

     private: System::Void executeCommandlToolStripMenuItem_Click( System::Object^  sender. System::EventArgs^  e) {   MessageBox::Show( "Command 1 Has Executed" ); } 

    Similarly, you might add the following code to the click event for the second context menu item:

     private: System::Void executeCommand2ToolStripMenuItem_Click( System::Object^  sender, System::EventArgs^  e)         {           MessageBox::Show( "Command 2 Has Executed" );         } 

    Finally, you can assign the following code to the click event for the third context menu item, as shown here:

     private: System::Void executeCommand3ToolStripMenuItem_Click( System::Object^  sender, System::EventArgs^  e)         {           MessageBox::Show( "Command 3 Has Executed" );         } 

  4. Select the control for which you created the context menu and set its ContextMenuStrip property equal to the name of the context menu that you just created.

image from book
Figure 4.24: Here the menu items for a context menu are configured.

After you have finished configuring the form or control's ContextMenuStrip property, you can run your application and access the context menu by right-clicking on the form or control, as demonstrated in Figure 4.25.

image from book
Figure 4.25: This button has been given an associated context menu.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net