This is similar to: How to add things to a menustrip programatically?, but I need something slightly different.
I have a winform, and I'm creating new instances of the form. However, I'm also collecting all of the current instances of the form when I make a new one, and populating a "Window" menu with menuitems to allow me to close the windows. As such, I need to not only add things to a menustrip programmatically, I need to also specify what those menus do as well. Is that possible?
Code:
private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
{
var newForm = new Form1();
newForm.Show();
foreach (Form form in Application.OpenForms)
{
// add menu items under "Window" with the name of the window and the
// event handler to close that window, aka form.Close() I assume;
}
}
I would like to do it this way, so that I update the "Window" menu every time I create a new window so that my list of windows to close is accurate without any weird extra stuff.