The main Menu is called the MenuStrip
Menu’s that pop up within the context of some control, for instance when you right click, use the ContextMenuStrip.
Creating a MenuStrip
Drag the menu tool onto the top of the form
You can edit the menu contents using the properties > collections (recomended for complex menu’s), or just by clicking on the bar and adding entries, then double clicking them to edit their action code
Adding And Removing MenuStrip Items
//REMOVE ANY EXISTING MENU ITEMS
ToolStripItem ^ExistingMenuItem;
while (recentFilesToolStripMenuItem->DropDownItems->Count)
{
ExistingMenuItem = recentFilesToolStripMenuItem->DropDownItems[0];
recentFilesToolStripMenuItem->DropDownItems->Remove(ExistingMenuItem);
}
//ADD NEW MENU ITEMS
ToolStripMenuItem ^NewMnuItem;
for (Count = 0; Count < RecentFilenamesCount; Count++)
{
NewMnuItem = gcnew ToolStripMenuItem(); //(Need to create new each time)
TempString = RecentFilenames[Count];
NewMnuItem->Text = TempString;
NewMnuItem->Size = System::Drawing::Size(160, 22);
NewMnuItem->Click += gcnew System::EventHandler(this, &frmMain::someToolStripFileMenuRecentFile_Click);
recentFilesToolStripMenuItem->DropDownItems->Add(NewMnuItem);
}
//OR USE THIS FOR SINGLE ADDITION
launchCCAsAdminToolStripMenuItem = gcnew System::Windows::Forms::ToolStripMenuItem();
launchCCAsAdminToolStripMenuItem->Name = L"launchCCAsAdminToolStripMenuItem";
launchCCAsAdminToolStripMenuItem->Size = System::Drawing::Size(200, 22);
launchCCAsAdminToolStripMenuItem->Text = L"Some Text";
launchCCAsAdminToolStripMenuItem->Click += gcnew System::EventHandler(this, &frmMain::launchCCAsAdminToolStripMenuItem_Click);
toolsToolStripMenuItem->DropDownItems->Add(someToolStripMenuItem);
Removing an individual item
toolsToolStripMenuItem->DropDownItems->Remove(someToolStripMenuItem);
Adding An Array Of MenuStrip Items
Define global array
private: array<System::Windows::Forms::ToolStripMenuItem^> ^graphicStatusFormToolStripMenuItem;
In Form Load
//CREATE THE ARRAY
graphicStatusFormToolStripMenuItem = gcnew array<System::Windows::Forms::ToolStripMenuItem^>(NUMBER_OF_GRAPHIC_STATUS_FORMS);
//CREATE EACH MENU ITEM
ToolStripMenuItem ^NewMnuItem;
for (Count = 0; Count < NUMBER_OF_GRAPHIC_STATUS_FORMS; Count++)
{
NewMnuItem = gcnew ToolStripMenuItem(); //(Need to create new each time)
NewMnuItem->Text = L"Graphic Status " + Convert::ToString(Count + 1);
NewMnuItem->Size = System::Drawing::Size(165, 22);
NewMnuItem->Click += gcnew System::EventHandler(this, &frmMain::graphicStatusToolStripMenuItem_Click);
//Add the item to the menu
this->graphicStatusToolStripMenuItem->DropDownItems->Add(NewMnuItem);
}
In the event handler
int Count;
int FormId = 0xff;
ToolStripMenuItem ^ClickedItem = dynamic_cast<ToolStripMenuItem^>(sender);
if (ClickedItem != nullptr)
{
for (Count = 0; Count < NUMBER_OF_GRAPHIC_STATUS_FORMS; Count++)
{
if (ClickedItem->Text == L"Graphic Status " + Convert::ToString(Count + 1))
{
FormId = Count;
break;
}
}
}
//EXIT IF NOT VALID
if (FormId >= NUMBER_OF_GRAPHIC_STATUS_FORMS)
return;
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.