vb.net 2005 动态加载菜单及反射+权限

来源:互联网 发布:linux chmod 只读 编辑:程序博客网 时间:2024/05/17 06:49
Imports System.Reflection
Public Class Form1
    
Public userflag As Object = "1,2,3,4,5,7,8"

    
Private Sub Form1_FormClosing(ByVal sender As ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Application.Exit()
    
End Sub

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
Me.WindowState = FormWindowState.Maximized
        AddMenu(userflag)
    
End Sub


    
Private Sub openform(ByVal sender As ObjectByVal e As EventArgs)
        
Dim asm As [Assembly= System.Reflection.Assembly.GetExecutingAssembly()
        
Dim frm As New Form
        frm 
= CType(asm.CreateInstance("WindowsApplication1." + CType(sender, ToolStripMenuItem).Tag.ToString), Form)
        
'CType(asm.CreateInstance("项目名称." + CType(sender, ToolStripMenuItem).Tag.ToString), Form)
        frm.Text = sender.ToString
        frm.WindowState 
= FormWindowState.Maximized
        
If OpenOnce(frm) = False Then
            frm.MdiParent 
= Me
            frm.Show()
        
End If
    
End Sub


    
Private Sub AddMenu(ByVal userflag As Object)
        
Dim ms As New MenuStrip()
        
For j As Integer = 0 To 4
            
Dim windowMenu As New ToolStripMenuItem("主菜单" & j)
            
For i As Integer = 1 To 10
                
Dim windowNewMenu As New ToolStripMenuItem("我的菜单" & i, Nothing)
                
AddHandler windowNewMenu.Click, AddressOf openform
                windowNewMenu.Tag 
= "Form" & i      '以这里获得窗体的路径
                flag(userflag, windowNewMenu, i)
                windowMenu.DropDownItems.Add(windowNewMenu)
                
CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowImageMargin = False
                
CType(windowMenu.DropDown, ToolStripDropDownMenu).ShowCheckMargin = False
                ms.Items.Add(windowMenu)
                ms.Dock 
= DockStyle.Top
                
Me.MainMenuStrip = ms
                
Me.Controls.Add(ms)
            
Next
        
Next
    
End Sub


    
Public Function OpenOnce(ByVal myform As Form) As Boolean
        
Dim form As Form
        
For Each form In Me.MdiChildren
            
If form.Text = myform.Text Then
                form.Activate()
                
Return True
            
End If
        
Next
        
Return False
    
End Function


    
Private Function flag(ByVal userflag As ObjectByVal MenuN As ObjectByVal i As Integer)
        userflag 
= Split(userflag, ",")
        
For k As Integer = 0 To UBound(userflag)
            
With MenuN
                
If userflag(k) - i = 0 Then
                    .Enabled 
= True
                    
Exit For
                
Else
                    .Enabled 
= False
                
End If
            
End With
        
Next
        
Return Nothing
    
End Function

End Class
 从别人那请教过来的,然后自已改了改,不足之处大家多多指教