Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

\
Posted

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:
C#
foreach (ToolStripMenuItem TSI in CollectionTSI) 

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:
C#
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
    }
}

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.
 
Share this answer
 
Comments
RebornDeveloper 16-Sep-10 14:08pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
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:
C#
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:
C#
contextMenuStrip.Items.Add(new ToolStripMenuItem("Item 1"));
contextMenuStrip.Items.Add(new ToolStripSeparator());
contextMenuStrip.Items.Add(new ToolStripMenuItem("Item 2"));
 
Share this answer
 
Comments
me773 12-Aug-10 13:40pm    
Reason for my vote of 4
new ToolStripSeparator();
is much more efficient than the designer's code.
RebornDeveloper 13-Aug-10 9:08am    
hi

Thanks for your inputs. The problem is that i need an array of toolstripmenuitem, since I need to access the CheckOnClick & CheckState properties.

Let me show you the code from the contextmenustrip click event
void NewContextMenuStrip_Click(object sender, EventArgs e)
{
var ObjectSender = (ContextMenuStrip)sender;
ToolStripItemCollection CollectionTSI = (ToolStripItemCollection)ObjectSender.Items;
foreach (ToolStripMenuItem TSI in CollectionTSI)
{
// August 14
if (TSI.CheckOnClick)
{
TSI.CheckState = CheckState.Unchecked;

}
}

}
Now if I use toolstripitem in the foreach instead of toolstripmenu item the code gives me an invalid cast exception error
RebornDeveloper 16-Sep-10 14:08pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
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!
 
Share this answer
 
Comments
Toli Cuturicu 12-Aug-10 13:16pm    
Reason for my vote of 1
Copied from autogenerated code
RebornDeveloper 16-Sep-10 14:08pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
[]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900