1 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
您需要一個(gè)契約來區(qū)分建議菜單項(xiàng)和其余菜單項(xiàng),然后在添加建議時(shí),首先刪除現(xiàn)有建議項(xiàng),然后添加新項(xiàng)。
這里作為一個(gè)例子,我使用Tag的屬性ToolStripMenuItem作為合同,并且所有suggestion在其標(biāo)簽中的菜單條項(xiàng)目都被視為建議:
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);
換個(gè)說法:
Suggest(new List<string> { "another", "another one" }, contextMenuStrip1);
- 1 回答
- 0 關(guān)注
- 113 瀏覽
添加回答
舉報(bào)