问题描述
大家好
首先,我在很久之后用C#编写代码。
如果我的工作线路错误,请告诉我。
我有树视图我根据某些条件为各个节点创建了contextmenustrip(没有问题)。
我无法将toolstripseperator插入mu contextmanustrip。
任何人都可以给我一条生命线这是下面的代码
ToolStripMenuItem Option1 = new ToolStripMenuItem(Option 1);
ToolStripMenuItem Option2 = new ToolStripMenuItem(Option 2);
ToolStripMenuItem Option3 = new ToolStripMenuItem(Option 3);
添加工具条菜单项我使用以下代码
NewContextMenuStrip.Items.AddRange( new ToolStripMenuItem [] {Option1,Option2,Option3});
我想在上面的option2之后找一个分隔符。我知道这不是很关键,但嘿,每一点点都很重要! :-D
感谢大家提前!!
Jason Bourne
\
Hi Everyone
First of all, I am writing code in C# after well ages.
If I am working along the wrong lines then please let me know.
I have a treeview for which I am creating contextmenustrip(s) for individual nodes depending on certain conditions (no problems there).
I am unable to insert a toolstripseperator for into mu contextmanustrip.
Can anyone throw me a lifeline?
Here is the code below
ContextMenuStrip NewContextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem Option1 = new ToolStripMenuItem("Option 1");
ToolStripMenuItem Option2 = new ToolStripMenuItem("Option 2");
ToolStripMenuItem Option3 = new ToolStripMenuItem("Option 3");
For adding the toolstrip menu items I am using the following code
NewContextMenuStrip.Items.AddRange(new ToolStripMenuItem[]{Option1,Option2,Option3});
I want a a seperator after option2 above. I know that this is not very critical, but hey, every little bit counts! :-D
Thanks to all of you in advance!!
Jason Bourne
\
推荐答案
foreach (ToolStripMenuItem TSI in CollectionTSI)
如果没有你看到它,就会发生隐式演员, Items
的每个对象正在将集合强制转换为 ToolStripMenuItem
并且显示分隔符对象显然失败。
一种解决方法是使用作为
运算符自己进行投射以检查无效的强制转换:
there's an implicit cast happening without you seeing it, each object of the Items
collection is being cast to a ToolStripMenuItem
and casting the separator object obviously fails.
One way around this is to do the cast yourself using the as
operator to check for invalid casts:
void contextMenuStrip_Click(object sender, EventArgs e)
{
ContextMenuStrip menu = sender as ContextMenuStrip;
if (menu == null)
{
return;
}
foreach (ToolStripItem item in menu.Items)
{
ToolStripMenuItem menuItem = item as ToolStripMenuItem;
if (menuItem == null)
{
continue;
}
// do whatever
}
}
在这个 foreach
循环中,枚举<$ c中的元素时不会进行强制转换$ c> Items ,但循环体的第一件事就是进行安全转换,看看每个 ToolStripItem
是否真的是 MenuItem
。
In this foreach
loop no casting is done when enumerating over the elements in Items
, but the first thing the body of the loop does is do a safe cast to see if each ToolStripItem
is really a MenuItem
.
contextMenuStrip.Items.AddRange(new ToolStripItem[] {
new ToolStripMenuItem("Item 1"),
new ToolStripSeparator(),
new ToolStripMenuItem("Item 2") });
或者你可以每次用不同种类的物品打电话添加
:
Alternatively you could just call Add
a bunch of times with different kinds of items each time:
contextMenuStrip.Items.Add(new ToolStripMenuItem("Item 1"));
contextMenuStrip.Items.Add(new ToolStripSeparator());
contextMenuStrip.Items.Add(new ToolStripMenuItem("Item 2"));
ToolStripSeparator Separator1;
Separator1 = new ToolStripSeparator();
Separator1.Size = new System.Drawing.Size(97, 6);
而不仅仅是将你的分隔符添加到菜单条:
than just add your separator to the menu strip:
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {this.Separator1});
和你的好去处!
and your good to go!
这篇关于将ToolStripSeperator添加到ContextmenuStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!