Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.0 C#3.0 C#4.0 .NET4
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! Big Grin | :-D
 
Thanks to all of you in advance!!
 
Jason Bourne
 
\
Posted 12-Aug-10 7:38am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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) 
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
    }
}
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.
  Permalink  
Comments
RebornDeveloper at 16-Sep-10 14:08pm
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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"));
  Permalink  
Comments
me773 at 12-Aug-10 13:40pm
   
Reason for my vote of 4
new ToolStripSeparator();
is much more efficient than the designer's code.
RebornDeveloper at 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 at 16-Sep-10 14:08pm
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
  Permalink  
Comments
Toli Cuturicu at 12-Aug-10 13:16pm
   
Reason for my vote of 1
Copied from autogenerated code
RebornDeveloper at 16-Sep-10 14:08pm
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

[]
  Permalink  

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 197
4 Sinisa Hajnal 176
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 28 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100