帮助C#如何确定真正的ToolStripItem类型 [英] HELP c# How to deteming the real ToolStripItem type

查看:119
本文介绍了帮助C#如何确定真正的ToolStripItem类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将VB.Net应用程序转换为C#.其他一切都很好.除了这一点.我有一个从数据库构建菜单,子菜单和分隔符的功能.它可以构建一个MenuStrip或一个ContextMenuStrip,然后可以将其分配给表单.
这意味着菜单可以扩展和使用conpmex,但可以在应用程序外部进行管理,我也将其用作安全访问模型的一部分.所有菜单均从外部数据库控制,这也意味着我可以开发可视化工具(Treeviews等)来管理用户菜单.该应用程序广泛使用菜单,这是设计使然.
我想以编程方式为没有下拉列表或分隔符的每个项目分配一个事件处理程序
在VB中,我会这样做(完美运行)

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

,但是我将如何在C#中实现相同效果
我必须像这样编写我的循环

but how would I achieve the same in C#
I have to code my loop like this

foreach (ToolStripItem oItem in voMenuItems)


它总是返回一个ToolStripItem(使用GetType或typeof),我找不到任何方法来确定ToolStripItem实际为的项目类型(ToolStripDropDown或ToolStripSeperator),即使如此,我也无法转换为我自己的类型想要例如


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 ...


这样会产生编译时错误
"System.Windows.Forms.ToolStripItem"不包含"DropDownItems"的定义,找不到扩展方法"DropDownItems"接受类型为"System.Windows.Forms.ToolStripItem"的第一个参数(您是否缺少using指令或程序集引用?)"

我想做的是这个


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

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);
            }
        }
}

推荐答案

尝试一下.在.net中,投射是一项昂贵的操作,因此您希望尽可能少地进行一次.它还会清理您的代码:

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:

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);
}


if (oItem is ToolStripDropDownItem) {
    int count = ((ToolStripDropDownItem)oItem.DropDownItems).Count;
} else if (oItem is ToolStripSeperator) {
    //Do stuff here

}


这篇关于帮助C#如何确定真正的ToolStripItem类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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