Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am converting a VB.Net application to C#. Everything else is going great. Except this bit. I have a function which builds menus,sub menus and seperators from a database. It can build a MenuStrip or a ContextMenuStrip which can then be assigned to a form.
This means that menus can be extensive and conpmex but managed outside the application, which I also use as part of my security access model. All menus are controlled from the database externally, it also means I can develop visual tools (Treeviews etc) to manage user menus. The application uses menus extensively which is by design.
Programmatically I want to assign an eventhandler to every item that isnt a dropdown or a seperator
In VB I would do this, (works perfectly)
VB
Private Sub AddHandlers (ByVal voMenuItems As ToolStripItemCollection)
    For Each oItem in voMenuItems
        If TypeOf(oItem is ToolStripDropDown) then
            if oItem.DropDownItems.Count > 0 then
                AddHandlers(oItem.DropDownItems)
            Else
                AddHandler oItem.Click, AddressOf ToolBarButtonClick
            End If
        End If
    Next
End Sub
Private Sub ToolbarButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Do something
End Sub
but how would I achieve the same in C#
I have to code my loop like this

C#
foreach (ToolStripItem oItem in voMenuItems)

Which always returns a ToolStripItem (using either, GetType or typeof), I cannot find any way to determine the type of item (ToolStripDropDown or ToolStripSeperator) that the ToolStripItem actually is, and even then I can't cast to the type that I want e.g.
C#
(ToolStripDropDown)oItem.DropDownItems.Count ...

This gives a compile time error
"System.Windows.Forms.ToolStripItem' does not contain a definition for 'DropDownItems' and no extension method 'DropDownItems' accepting a first argument of type 'System.Windows.Forms.ToolStripItem' could be found (are you missing a using directive or an assembly reference?)"

What I want to do is this
C#
void AddMenuHandlers(ToolStripItemCollection voMenuItems)
{
    //ToolStripItem oItem;

    foreach (ToolStripItem oItem in voMenuItems)
        if (oItem is ToolStripDropDown)
        {
            if (ToolStripDropDown)oItem.DropDownItems.Count > 0)
            {
                AddMenuHandlers((ToolStripDropDown)oItem.DropDownItems);
            }
            else
            {
                oItem.Click += new EventHandler(moMenu_Click);
            }
        }
}
Posted
Updated 31-Jul-11 1:02am
v4

Try this. Casting is an expensive operation in .net, so you want to do it as few times as possible. It also cleans up your code:

C#
if (oItem is ToolStripDropDown) 
{
    ToolStripDropDown ddItem = oItem as ToolStripDropDown;
    if (ddItem.DropDownItems.Count > 0)
    {
        AddMenuHandlers(ddItem.DropDownItems);
    }
} 
else if (oItem is ToolStripSeperator) 
{
    oItem.Click += new EventHandler(moMenu_Click);
}
 
Share this answer
 
v2
C#
if (oItem is ToolStripDropDownItem) {
    int count = ((ToolStripDropDownItem)oItem.DropDownItems).Count;
} else if (oItem is ToolStripSeperator) {
    //Do stuff here

}
 
Share this answer
 
v2
Comments
djack10 31-Jul-11 6:56am    
This solution is not correct. Please see below and also my update the the question above.

if (oItem is ToolStripDropDown)

This gives the following warning "The given expression is never of the provided ('System.Windows.Forms.ToolStripDropDown')"


int count = (ToolStripDropDown)oItem.DropDownItems.Count;

This gives the following error "'System.Windows.Forms.ToolStripItem' does not contain a definition for 'DropDownItems' and no extension method 'DropDownItems' accepting a first argument of type 'System.Windows.Forms.ToolStripItem' could be found (are you missing a using directive or an assembly reference?)"
[no name] 31-Jul-11 7:48am    
Fixed the code. Thanks for pointing out.

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