Added ToolStripMenuItem to multiple locations?

I have a large list of objects that the user must select. I have a ContextMenuStrip in my window and have several MenuItems for each entity category.

In the library example, think of "Personnel," "Borrowing," "Patrons," etc.

"Personnel" may contain "By type of employment" → {"Full time", "Part-time"} or "By gender" → {"Man", "Woman"}, etc.

“Borrowings” may contain “By Type” → {“Books”, “Magazines”, “DVD”} or “By Genre” → {“Fiction” → {“Sky-fi”, “Romance”), “Crime” }, "Non-fiction" ("Science", "Music", "History"}}, etc.

In principle, one person can be in several places at the same time. The employee may be full time as a female. A borrowed book may be a novel and a novel. and etc.

I programmatically created List<ToolStripMenuItem>along with event handlers, tags, everything. Then I programmatically went through each of them and added them to various menus and submenus so that they could be obtained from different places. The idea is that I only need 1 object in memory on the ToolStripMenuItem, and if it is checked / not installed, then it should affect all of them.

This seemed like an elegant solution, and I really looked forward to it working, but apparently when I add ToolStripMenuItem to one ToolStripMenuItemDropDownItems, it is removed from wherever it was added.

. , , , , . p >

?

, :

foreach (ToolStripMenuItem item in staffItems)
{
  Staff s = (Staff)item.Tag;

  foreach (ToolStripMenuItem tsmi in byStaffLocationToolStripMenuItem.DropDownItems)
    if ((Location)tsmi.Tag == s.Location)
      tsmi.DropDownItems.Add(item); // [1] Item added here

  foreach (ToolStripMenuItem tsmi in byStaffTypeToolStripMenuItem.DropDownItems)
    if ((StaffType)tsmi.Tag == s.StaffType)
      tsmi.DropDownItems.Add(item); // removed from [1] and added here instead :(
}
+3
1

ControlCollection. , , ControlCollection .

, () :

public interface ICommand
{
    string Name {get;set;}
    bool Enabled { get; set; }
    bool Checked { get; set; }

    void OnClick();
}

, ToolStripMenuItem ICommand OnClick. , ICommand .

+1

All Articles