Is it possible to dynamically create a MenuStrip without adding the instance via the form?
I have done..
//Create menu
MenuStrip menu = new MenuStrip();
menu.name = "MainMenu";
// Create a Menu Item
ToolStripMenuItem file = new ToolStripMenuItem("File");
//Add item to Menu
menu.Items.Add(file);
//Click event
file.Click += new System.EventHandler(this.FileItemClick);
private void FileItemClick(object sender, EventArgs e)
{
MessageBox.Show("File clicked");
}
I know I have to add it to the form with something like this..
this.MainMenuStrip = menu;
Controls.Add(menu);
but without an instance dragged onto the form already, the above isn't possible. Is there a way to 100% dynamically create a MenuStrip? Also, SubMenus?
Thanks
Yes, it is possible. In fact, all controls that you drag to the form are generated into code by the designer. To see exactly what code you need, just drag a MenuStrip
control onto the form and then open the designer file (in Notepad) and copy & paste the code from there. If your form is called Form1.cs
then your designer file is Form1.designer.cs
.
The designer file is all generated code that creates and initializes all controls on your form to the values you select in the Properties
tool window when you visually desing your form.