If you visually create your main menu, the form designer takes care of most details behind the scenes. For example, each menu item is automatically added to its parent menu category. If you programmatically create your main menu, you must associate each menu item to its parent menu category. To support menu categories, ToolStripMenuItem, the class used to create menu categories, is derived from a class named ToolStripDropDownItem. The ToolStripDropDownItem class is abstract, which means you cannot instantiate it. Instead, it provides functionality to other classes derived from it. The ToolStripDropDownItem class is based on the ToolStripItem class. To support menu items, the ToolStripDropDownItem class is equipped with a property named DropDownItems. This property is of type ToolStripItemCollection, which a collection-based class. The ToolStripItemCollection class implements the IList and the ICollection interfaces. To specify that a menu item will be part of a menu category, call the Add() method of the ToolStripItemCollection class. This method is overloaded with various versions. One of the versions uses the following syntax: public: int Add(ToolStripItem^ value); This version allows you to pass a ToolStripItem-type of item class, such as a ToolStripMenuItem object. Here is an example: public ref class CExercise : public Form { private: ToolStripMenuItem ^ mnuEdit; ToolStripMenuItem ^ mnuCopy; public: CExercise() { InitializeComponent(); } void InitializeComponent() { mnuEdit = gcnew ToolStripMenuItem(L"Edit"); mnuCopy = gcnew ToolStripMenuItem(L"Copy"); mnuEdit->DropDownItems->Add(mnuCopy); Controls->Add(mnuMain); } }; The ToolStripItemCollection class also allows you to create a menu item without going through ToolStripItem-type of object. To support this, its provides the following version of its Add() method: public: ToolStripItem ^ Add(String^ text); This method takes as argument the text that the menu item would display and it returns the ToolStripItem item that was created. Here is an example: public ref class CExercise : public Form { private: ToolStripMenuItem ^ mnuEdit; ToolStripMenuItem ^ mnuCopy; ToolStripMenuItem ^ mnuPaste; public: CExercise() { InitializeComponent(); } void InitializeComponent() { mnuEdit = gcnew ToolStripMenuItem(L"Edit"); mnuCopy = gcnew ToolStripMenuItem(L"Copy"); mnuEdit->DropDownItems->Add(mnuCopy); mnuPaste = dynamic_cast<ToolStripMenuItem ^>(mnuEdit->DropDownItems->Add(L"Paste")); Controls->Add(mnuMain); } }; Instead of adding one menu item at a time, you can create an array of menu items and then add it to a category in one row. To support this, the ToolStripItemCollection class implements the AddRange() method. This method is overloaded with two versions. One of the versions uses the following syntax: public: void AddRange(array<ToolStripItem^>^ toolStripItems); When calling this method, you must pass it an array of ToolStripItem-type of objects. Here are two examples: public ref class CExercise : public Form { private: MenuStrip ^ mnuMain; ToolStripMenuItem ^ mnuFile; ToolStripMenuItem ^ mnuNew; ToolStripMenuItem ^ mnuOpen; ToolStripMenuItem ^ mnuExit; ToolStripMenuItem ^ mnuEdit; ToolStripMenuItem ^ mnuCopy; ToolStripMenuItem ^ mnuPaste; ToolStripMenuItem ^ mnuHelp; public: CExercise() { InitializeComponent(); } void InitializeComponent() { mnuMain = gcnew MenuStrip; mnuFile = gcnew ToolStripMenuItem(L"File"); mnuNew = gcnew ToolStripMenuItem(L"New"); mnuOpen = gcnew ToolStripMenuItem(L"Open"); mnuExit = gcnew ToolStripMenuItem(L"Exit"); array<ToolStripMenuItem ^> ^ mnuFileItems = { mnuNew, mnuOpen, mnuExit}; mnuFile->DropDownItems->AddRange(mnuFileItems); mnuEdit = gcnew ToolStripMenuItem(L"Edit"); mnuCopy = gcnew ToolStripMenuItem(L"Copy"); mnuEdit->DropDownItems->Add(mnuCopy); mnuPaste = dynamic_cast<ToolStripMenuItem ^>(mnuEdit->DropDownItems->Add(L"Paste")); mnuHelp = gcnew ToolStripMenuItem(L"Help"); array<ToolStripMenuItem ^> ^ mnuHelpItems = { gcnew ToolStripMenuItem(L"Search"), gcnew ToolStripMenuItem(L"Contents"), gcnew ToolStripMenuItem(L"Index"), gcnew ToolStripMenuItem(L"Support Web Site"), gcnew ToolStripMenuItem(L"About this application") }; mnuHelp->DropDownItems->AddRange(mnuHelpItems); Controls->Add(mnuMain); } };
If you visually create your main menu, each menu category is automatically assigned to the menu strip. If you programmatically create your main menu, you must take care of this in order to show the whole menu. After creating the menu categories, you can add them to the main menu. To support this, the ToolStrip class is equipped with a property named Items and it makes this property available to the MenuStrip class. The Items property is of type ToolStripItemCollection. This class implements the IList, the ICollection, and the IEnumerable interfaces. Therefore, to add a menu category to a MenuStrip object, you can call the Add() method of the ToolStripItemCollection class. This method is overloaded with various versions and one of them uses the following version: public: int Add(ToolStripItem^ value); You can call this version and pass it a ToolStripItem-type of object, such as a ToolStripMenuItem value. Here is an example: public ref class CExercise : public Form { private: MenuStrip ^ mnuMain; ToolStripMenuItem ^ mnuFile; . . . public: CExercise() { InitializeComponent(); } void InitializeComponent() { mnuMain = gcnew MenuStrip; mnuFile = gcnew ToolStripMenuItem(L"File"); . . . mnuMain->Items->Add(mnuFile); Controls->Add(mnuMain); } }; In the same way, you can add the other items. Alternatively, you can create an array of menu categories and add them in a row. To support this, the ToolStripItemCollection is equipped with the AddRange() medhod that is overloaded with two versions. One of the versions uses the following syntax: public: void AddRange(array<ToolStripItem^>^ toolStripItems); When calling this method, pass it an array of ToolStripItem types of values. Here is an example: #include <windows.h> #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; public ref class CExercise : public Form { private: MenuStrip ^ mnuMain; ToolStripMenuItem ^ mnuFile; ToolStripMenuItem ^ mnuNew; ToolStripMenuItem ^ mnuOpen; ToolStripMenuItem ^ mnuExit; ToolStripMenuItem ^ mnuEdit; ToolStripMenuItem ^ mnuCopy; ToolStripMenuItem ^ mnuPaste; ToolStripMenuItem ^ mnuView; ToolStripMenuItem ^ mnuHelp; public: CExercise() { InitializeComponent(); } void InitializeComponent() { mnuMain = gcnew MenuStrip; mnuFile = gcnew ToolStripMenuItem(L"File"); mnuNew = gcnew ToolStripMenuItem(L"New"); mnuOpen = gcnew ToolStripMenuItem(L"Open"); mnuExit = gcnew ToolStripMenuItem(L"Exit"); array<ToolStripMenuItem ^> ^ mnuFileItems = { mnuNew, mnuOpen, mnuExit}; mnuFile->DropDownItems->AddRange(mnuFileItems); mnuEdit = gcnew ToolStripMenuItem(L"Edit"); mnuCopy = gcnew ToolStripMenuItem(L"Copy"); mnuEdit->DropDownItems->Add(mnuCopy); mnuPaste = dynamic_cast<ToolStripMenuItem ^>(mnuEdit->DropDownItems->Add(L"Paste")); mnuView = gcnew ToolStripMenuItem(L"View"); array<ToolStripMenuItem ^> ^ mnuViewItems = { gcnew ToolStripMenuItem(L"Standard Toolbar"), gcnew ToolStripMenuItem(L"Formatting Toolbar"), gcnew ToolStripMenuItem(L"Status Bar") }; mnuView->DropDownItems->AddRange(mnuViewItems); mnuHelp = gcnew ToolStripMenuItem(L"Help"); array<ToolStripMenuItem ^> ^ mnuHelpItems = { gcnew ToolStripMenuItem(L"Search"), gcnew ToolStripMenuItem(L"Contents"), gcnew ToolStripMenuItem(L"Index"), gcnew ToolStripMenuItem(L"Support Web Site"), gcnew ToolStripMenuItem(L"About this application") }; mnuHelp->DropDownItems->AddRange(mnuHelpItems); array<ToolStripMenuItem ^> ^ mnuAccessories = { mnuView, mnuHelp }; mnuMain->Items->Add(mnuFile); mnuMain->Items->AddRange(mnuAccessories); Controls->Add(mnuMain); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise()); return 0; } This would produce:
If you create a menu as we have just done, to write code for one of the menu items, you can double-click the menu item. This would open the Click event of the menu item in the Code Editor and you can start writing the desired code for that item.
|
|
|||||||||||
|