Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重新排列contextmenustrip中的项目?_C#_.net_Winforms_Contextmenustrip - Fatal编程技术网

C# 如何重新排列contextmenustrip中的项目?

C# 如何重新排列contextmenustrip中的项目?,c#,.net,winforms,contextmenustrip,C#,.net,Winforms,Contextmenustrip,如何重新排列ContextMenuStrip的项目?例如,如果我创建了一个带有按钮的windows窗体应用程序,并将ContextMenuStrip添加到窗体,并在按钮单击事件中分配它,然后按顺序添加项目 run process1 run process2 run process3 然后过了一天,我决定在ContextMenuStrip中添加另一项,比如说“runprocess4”,我希望序列是这样的 run process1 run process4 run process2 run pro

如何重新排列
ContextMenuStrip
的项目?例如,如果我创建了一个带有按钮的windows窗体应用程序,并将
ContextMenuStrip
添加到窗体,并在按钮单击事件中分配它,然后按顺序添加项目

run process1
run process2
run process3
然后过了一天,我决定在
ContextMenuStrip
中添加另一项,比如说“runprocess4”,我希望序列是这样的

run process1
run process4
run process2
run process3

我如何才能做到这一点(除了重命名每个项目并在每个单击事件上交换代码)

除了添加项目之外,您还可以在指定索引处插入项目:

this.contextMenuStrip1.Items.Add("Item1");
this.contextMenuStrip1.Items.Add("Item2");
this.contextMenuStrip1.Items.Add("Item3");
this.contextMenuStrip1.Items.Insert(1, new ToolStripMenuItem("Item4"));

除了添加项目外,您还可以在指定索引处插入项目:

this.contextMenuStrip1.Items.Add("Item1");
this.contextMenuStrip1.Items.Add("Item2");
this.contextMenuStrip1.Items.Add("Item3");
this.contextMenuStrip1.Items.Insert(1, new ToolStripMenuItem("Item4"));

当我点击表单上的ContextMenuStrip时,我没有看到
Item4
,但当我运行程序时,
Item4
会出现在按钮点击上……为什么会发生这种情况?另外,如果在表单中看不到,如何在
Item4
上添加单击事件?我在答案中共享的代码用于在运行时添加项目。如果您的问题与设计时间有关,只需通过拖放或使用
ContextMenuStrip
Items
集合对项重新排序即可。要添加
请单击事件处理程序,对于运行时,
ToolStripMenuItem
有一个带3个参数的构造函数,第二个参数是image,第三个是click事件处理程序。同样对于设计时,只需使用属性网格的
Events
选项卡添加事件处理程序。当我单击表单上的ContextMenuStrip时,我看不到
Item4
,但当我运行程序时,
Item4
会出现在按钮单击上……为什么会发生这种情况?另外,如果在表单中看不到,如何在
Item4
上添加单击事件?我在答案中共享的代码用于在运行时添加项目。如果您的问题与设计时间有关,只需通过拖放或使用
ContextMenuStrip
Items
集合对项重新排序即可。要添加
请单击事件处理程序,对于运行时,
ToolStripMenuItem
有一个带3个参数的构造函数,第二个参数是image,第三个是click事件处理程序。同样对于设计时,只需使用属性网格的
Events
选项卡添加事件处理程序。