0

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.

Community
  • 1
  • 1
Mark Anthony
  • 29
  • 1
  • 12

2 Answers2

0

You can create a class with required fields, and pass the required data.

public class MenuItemInfo
{
    public string Text { get; set; }
    public object Tag { get; set; }
    public EventHandler Handler { get; set; }
}

var menuItems = new List<MenuItemInfo>
{
    new MenuItemInfo
    {
        Text = "whatever",
        Tag = whatever,
        Handler = (o, s) =>
        {
            //Do whatever
        }
    }
};

ToolStripMenuItem toolStripMenuItem;
foreach (var mi in menuItems)
{
    ToolStripMenuItem foo = new ToolStripMenuItem(mi.Text);
    foo.Click += mi.Handler;
    foo.Tag = mi.Tag;

    toolStripMenuItem.DropDownItems.Add(foo);
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • That looks really good, but I'm wracking my brains trying to figure out how I'm going to implement it, as I'm trying to do it inside of an existing eventhandler: `code` private void newWindowToolStripMenuItem_Click(object sender, EventArgs e) { var newForm = new Form1(); newForm.Show(); foreach (Form form in Application.OpenForms) { // add menuitems with code to close one of the open windows, replacing existing menuitems. } }`code` – Mark Anthony May 19 '14 at 04:16
  • Am sorry, I don't get it. You solved your problem or you need more help? If you need more help edit your question with more info, your updated code etc. – Sriram Sakthivel May 19 '14 at 04:28
0

Add "ItemClicked"-event to your ContexMenuStrip menu. "ContextMenuStrip1" is the name of the menu used here. Edit part of your code as follows:

        var newForm = new Form1() { Name = "myForm" };
        newForm.Show();

        foreach (Form form in Application.OpenForms)
        {
            // Add new menuitem with the name of the form, and save the reference to "Tag"-property
            ToolStripMenuItem newItem = new ToolStripMenuItem() { Name = newForm.Name, Text = newForm.Name, Tag = newForm };

            // Add the new item to the menu
            contextMenuStrip1.Items.Add(newItem);
        }

Then make the "ItemClicked"-event:

    private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

        // Close the linked form, if it isn't disposed
        if (!((Form)((ToolStripItem)e.ClickedItem).Tag).IsDisposed)
        {
            ((Form)((ToolStripItem)e.ClickedItem).Tag).Close();
        }

        // Remove this menuitem from the menu
        contextMenuStrip1.Items.Remove((ToolStripItem)e.ClickedItem);
    }
W0lfw00ds
  • 2,018
  • 14
  • 23