1 回答

TA贡献1851条经验 获得超3个赞
您需要一个契约来区分建议菜单项和其余菜单项,然后在添加建议时,首先删除现有建议项,然后添加新项。
这里作为一个例子,我使用Tag的属性ToolStripMenuItem作为合同,并且所有suggestion在其标签中的菜单条项目都被视为建议:
public void Suggest(List<string> words, ContextMenuStrip menu)
{
string suggestion = "suggestion";
menu.Items.Cast<ToolStripItem>().Where(x => x.Tag == (object)suggestion)
.ToList().ForEach(x => menu.Items.Remove(x));
words.ToList().ForEach(x =>
{
var item = new ToolStripMenuItem(x);
item.Tag = suggestion;
item.Click += (s, e) => MessageBox.Show(x);
menu.Items.Insert(0, item);
});
}
作为用法,一句话:
Suggest(new List<string> { "something", "something else" }, contextMenuStrip1);
换个说法:
Suggest(new List<string> { "another", "another one" }, contextMenuStrip1);
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报