开发者

How do you determine the actual type of a ToolStripItem in C#

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)

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

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.

(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 开发者_JAVA技巧you missing a using directive or an assembly reference?)"


You can do it one of two ways:

ToolStripDropDown tsdd = oItem as ToolStripDropDown
if (tsdd != null)
{
}

The 'as' cast will return null if it doesn't work, or you can do:

try
{
    ToolStripDropDown tsdd = (ToolStripDropDown)oItem;
}
catch
{
    // Do something ...
}

Also to check a type of an object do:

bool isToolStripDropDown = oItem is ToolStripDropDown;

or:

bool isToolStripDropDown = oItem.GetType() == typeof(ToolStripDropDown);


you could use the foreach mentioning an Object type or an anonymous type like this:

foreach (object oItem in voMenuItems)
...
if(typeof(oItem) == ... )
...
or
foreach(var oItem in voMenuItems)
...

In case the typeof doesn't return the type you wish you can try and to the cast using the 'as' and checking if the returned object is null

if(oItem as ToolstripDropDown != null)
....


You can use the is keyword.

So,

If TypeOf(oItem) is ToolStripDropDown (VB)

becomes

if (oItem is ToolStripDropDown) (C#)

You might also want to look at the as keyword, which allows you to attempt to cast to a type but returns null if the conversion's not possible.


In VB I was able to do

oItem.GetType.ToString

So in this code:

                    Dim tmenu As New ToolStripMenuItem(menu.Name)
                    ASRMenus.DropDownItems.Insert(mPosition, tmenu)
                    Dim t As ToolStripItem = CType(tmenu, ToolStripItem)

asking this in the debugger

?t.GetType.tostring

gave "System.Windows.Forms.ToolStripMenuItem"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜