将ToolStripSeperator添加到ContextmenuStrip [英] Add ToolStripSeperator to ContextmenuStrip

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

问题描述

大家好



首先,我在很久之后用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

\

推荐答案

RE:评论贴在我之前的答案上



无效的强制转换异常是因为 ContextMenuStrip Items 集合持有 ToolStripItem s,而不是 ToolStripMenuItem s。在这段代码中:

RE: the comment posted to my previous answer

The invalid cast exception is because the Items collection of the ContextMenuStrip holds ToolStripItems, not ToolStripMenuItems. In this bit of code:
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.


如果您将呼叫更改为 AddRange 以获取 ToolStripItem 数组而不是 ToolStripMenuItem 数组然后你应该可以插入其他类型的项目:

If you change the call to AddRange to take a ToolStripItem array instead of a ToolStripMenuItem array then you should be able to insert other types of items into it:
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"));


您可以使用ms visual studio designer为您添加分隔符,但如果您想要手动完成:

you can use the ms visual studio designer to add a separator for you, but if you want to do it manually than:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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