ToolStripMenuItem添加到几个地方? [英] ToolStripMenuItem added to several places?

查看:358
本文介绍了ToolStripMenuItem添加到几个地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体的一个大的列表,其中,用户需要能够从选择。
我有我的窗口上的ContextMenuStrip,并拥有实体的每个类别几的MenuItems。



在图书馆的例子,认为工作人员,Borrowables ,食客等......



工作人员可能含有按就业类型 - > {全职,兼职}或者按性别 - > {男,女}。等等



Borrowables可能含有按类型 - > {书籍,杂志 的DVD}或按类型 - > {小说 - > {科幻,浪漫,犯罪},非小说{科学,音乐,历史}}等



基本上,一个实体可以同时在几个地方。工作人员可能是全职,和女性。一个borrowable可能是一本书,一本爱情小说。与事件处理程序,标签,一切在一起;等等



我已编程方式创建了一个列表< ToolStripMenuItem过夜。我已经然后编程通过他们每个人的消失,并将它们加入到各种菜单和子菜单,以便它们可以从不同的地方进行访问。这个想法是,我只需要在每个ToolStripMenuItem内存1对象,如果它的选中/取消那么的是对所有的人都反映出来。



这似乎是一个优雅的解决方案,我真的很期待看到它的工作,但显然当我添加一个ToolStripMenuItem一个ToolStripMenuItemDropDownItems,它就会从其它什么加入它删除。



这让我伤心。如果他们都是同一个对象,我就不会写很多代码要经过检查所有匹配的项目当一个人被选中,是因为检查一个会检查他们,并取消选中一会取消他们所有。



有没有其他办法,我可以用最少的代码做到这一点?



这是什么,我有一个小例子

 的foreach(在staffItems ToolStripMenuItem项目)
{
职工S =(职员)item.Tag ;

的foreach(ToolStripMenuItem tsmi在byStaffLocationToolStripMenuItem.DropDownItems)
如果((位置)tsmi.Tag == s.Location)
tsmi.DropDownItems.Add(项目); // [1]在这里添加的项目

的foreach(ToolStripMenuItem tsmi在byStaffTypeToolStripMenuItem.DropDownItems)
如果((StaffType)tsmi.Tag == s.StaffType)
tsmi.DropDownItems 。新增项目);从//删除[1]在这里,而不是添加:(
}


解决方案

这是ControlCollection的功能。从Control派生的任何对象只能在同一一次一个的ControlCollection。



如果你想跟随你的方法,我建议(草案)为您的命令引入抽象:

 公共接口的ICommand 
{
串名称{;设置;}
BOOL启用{获取;集;}
布尔经过{获取;集;}

无效的OnClick();
}

然后你可以创建自己的类将从ToolStripMenuItem派生,采取的ICommand作为参数,并使用它的OnClick现在,你应该能够确定实施ICommand的单个命令单个对象。


I have a large list of entities which the user needs to be able to select from. I have a ContextMenuStrip on my window, and has a few MenuItems for each category of entity.

In a library example, think "Staff", "Borrowables", "Patrons", etc...

"Staff" may contain "By Employment Type" -> { "Full Time", "Part Time" } or "By Gender" -> { "Male", "Female" } etc.

"Borrowables" may contain "By Type" -> { "Books", "Magazines", "DVDs" } or "By Genre" -> { "Fiction" -> { "Sci-Fi", "Romance", "Crime" }, "Non-Fiction" { "Science", "Music", "History" } } etc.

Basically, one entity can be in several places at once. A staff member can be full time, and female. A borrowable might be a book, and a romance novel. etc.

I have programatically created a List<ToolStripMenuItem> together with event handlers, tags, everything. I have then programatically gone through each of them and added them to various menus and sub-menus so that they can be accessed from a variety of places. The idea is that I only need 1 object in memory per ToolStripMenuItem, and if it's checked/unchecked then that should be reflected on all of them.

This would seem to be an elegant solution, and I was really looking forward to seeing it work, but apparently when I add a ToolStripMenuItem to one ToolStripMenuItemDropDownItems, it gets removed from wherever else it was added.

This makes me sad. If they were all the same object, I wouldn't have to write a lot of code to go through and check all matching items when one is checked, because checking one would check them all, and unchecking one would uncheck them all.

Is there any other way I can accomplish this with minimal code?

This is a small example of what I have:

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 :(
}

解决方案

That's the feature of the ControlCollection. Any object derived from Control can be only in one ControlCollection at the same time.

If you want to follow your approach I suggest (draft) introducing abstraction for your command:

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

    void OnClick();
}

Then you can create your own class which will derive from ToolStripMenuItem and take ICommand as a parameter and use it OnClick. Now you should be able to define single object implementing ICommand for a single command.

这篇关于ToolStripMenuItem添加到几个地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆