在vb.net中动态创建Menustrip [英] Dynamically creation of Menustrip in vb.net

查看:176
本文介绍了在vb.net中动态创建Menustrip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我想在vb.net中动态创建menustrip,它将从sqlserver表中读取信息,如右图所示,它将创建菜单。请帮助,如果一些示例解决方案转发到以下电子邮件地址谢谢。

问候,

asad

Friends,
I want to create menustrip dynamically in vb.net which will read information from sqlserver tables , as per right it will create the menu. please help if some sample solution forward to on following email address thank you.
regards,
asad

推荐答案

有人做了类似的事情,在代码项目动态创建MenuStrip - VB.NET [ ^ ]。
Someone did something similar, here at Code Project: "Dynamic Creation Of MenuStrip - VB.NET"[^].


由于OP尝试过解决方案1并且遇到了一些问题这是我的两个人...



我在VB.NET中创建了一个Windows窗体应用程序,其中Form上的唯一控件是一个空的MenuStrip - 即没有菜单项通过Designer添加



我决定将我的菜单定义保存在XML文档中... XML的样式非常适合菜单结构和XML文档在av。中填充各种各样的方式。出于本示例的目的,我只是将数据存储在一个文本文档中,我只是加载

As the OP has tried Solution 1 and is having some problems here's my twopennorth...

I created a Windows Forms application in VB.NET where the only control on the Form is an "empty" MenuStrip - i.e. no menu items added via the Designer

I decided to hold my Menu definitions in an XML document ... the style of XML lends itself nicely to a menu structure and an XML document can be populated in a variety of ways. For the purposes of this example I just stored the data in a text document which I just load
Private Function GetMenus() As XmlDocument
    'This would be replaced with extracting the data from a database or similar
    'For demo/testing I'm just loading a fixed xml document
    Dim xxml As XmlDocument = New XmlDocument()
    xxml.Load("c:\\temp\\SampleData.xml")
    Return xxml
End Function



Thi s是我使用的XML - 我希望我已经解释得很好


This is the XML I used - I hope I've explained it well enough

<?xml version="1.0" encoding="UTF-8"?>
<!--
layout here is TopLevel node has attribute 'text' which is the text for that menu item
Any sub-menus are in the NextLevel nodes which also have the 'text' attribute
NextLevel has InnerText set to the name of the function to call
If there are no NextLevel Nodes then the TopLevel node has InnerText set to the name of the function to call
-->
<Menus>
  <TopLevel text="Menu 1">
    <NextLevel text="Menu 1.A">Menu1A</NextLevel>
    <NextLevel text="Menu 1.B">Menu1B</NextLevel>
    <NextLevel text="Menu 1.C">Menu1C</NextLevel>
  </TopLevel>
  <TopLevel text="Menu 2">Menu2</TopLevel>
  <TopLevel text="Menu 3">
    <NextLevel text="Menu 3.A">Menu3A</NextLevel>
    <NextLevel text="Menu 3.B">Menu3B</NextLevel>
  </TopLevel>
</Menus>



代表这样的菜单结构


Which represents a menu structure like this

Menu 1				Menu 2		Menu 3
	Menu 1.A					Menu 3.A
	Menu 1.B					Menu 3.B
	Menu 1.C





在解决方案1的链接中,作者使用一个很好的技巧来确定要加载哪个表单。我选择使用Reflection来确定要调用哪个函数。



关键是要从菜单中调用所有可以简单调用的例程这是我用过的那个



In the link in Solution 1 the Author has used a nice trick to determine which Form to load. I've opted to use Reflection to work out which function to call.

The key is to have all of the routines that could be called from the menus in a simple class.
Here's the one I used

''' <summary>
''' Class DynamicMenuSubs contains all of the menu handlers for dynamically generated menus
''' </summary>
Public Class DynamicMenuSubs
    Public Sub Menu1A()
        Debug.Print("Called Menu1A")
    End Sub
    Public Sub Menu1B()
        Debug.Print("Called Menu1B")
    End Sub
    Public Sub Menu1C()
        Debug.Print("Called Menu1C")
    End Sub
    Public Sub Menu2()
        Debug.Print("Called Menu2")
    End Sub
    Public Sub Menu3A()
        Debug.Print("Called Menu3A")
    End Sub
    Public Sub Menu3B()
        Debug.Print("Called Menu3B")
    End Sub
End Class



请注意,子名称与上面的XML中的InnerText完全匹配。



因为我决定将函数的名称存储到,所以我只想访问数据库(或XML)调用每个MenuItem的标签,所以这是我创建菜单的代码


Note that the Sub names match (exactly) to the InnerText in the XML above.

Because I only want to visit the database (or XML) once I decided to store the name of the function to call as the Tag of each MenuItem, so here is my code for creating the menus

Private Sub BuildMenus()

    Dim xxml As XmlDocument = GetMenus()

    Dim toplevels As XmlNodeList = xxml.SelectNodes("Menus/TopLevel")
    Dim node As XmlNode
    'Top level menus
    For Each node In toplevels
        Dim tsmi As New ToolStripMenuItem
        tsmi.Text = node.Attributes("text").Value
        Dim sublevels As XmlNodeList = node.SelectNodes("NextLevel")
        If sublevels.Count = 0 Then
            'No sub-menus therefore set the tag of this menuitem to the function name
            tsmi.Tag = node.InnerText
            AddHandler tsmi.Click, AddressOf DynamicMenuHandler
        Else
            Dim subnode As XmlNode
            For Each subnode In sublevels
                Dim ddi As New ToolStripMenuItem
                ddi.Text = subnode.Attributes("text").Value
                ddi.Tag = subnode.InnerText
                AddHandler ddi.Click, AddressOf DynamicMenuHandler
                tsmi.DropDownItems.Add(ddi)
            Next
        End If
        MenuStrip1.Items.Add(tsmi)
    Next
End Sub



这里的一个关键特征是所有菜单项都具有相同的处理程序点击事件 - 无论是顶级项目还是子菜单项目。我从Form Load事件调用了BuildMenus函数



这是那个处理程序


A key feature here is that all of the menu items have the same handler for the click event - whether they are top level items or sub-menu items. I called the BuildMenus function from the Form Load event

Here is that handler

Private Sub DynamicMenuHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim s1 As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)

    'Use Reflection to invoke the named function
    If s1.Tag.ToString.Length > 0 Then
        Dim SubType As System.Type = GetType(DynamicMenuSubs)
        Dim SubInfo As System.Reflection.MethodInfo = SubType.GetMethod(s1.Tag.ToString())
        Dim SubController As New DynamicMenuSubs
        SubInfo.Invoke(SubController, Nothing)
    End If

End Sub





我终于重新找到了在单独的控制器类中使用这些功能的想法的来源。归功于这个想法(这个解决方案取决于此)是由于 Joris Bijvoets [ ^ ]我找不到他原来的文章但是链接上的解决方案详细介绍了[/ EDIT]



I've finally re-found the source for the idea of having the functions in a separate controller class. Credit for that idea (on which this solution hinges) is due to Joris Bijvoets[^] I can't find his original article but the solution on the link details most of it [/EDIT]


这篇关于在vb.net中动态创建Menustrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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