Imports System.Windows.Forms
Public Class HoofdMenu
'CONNECTIE NAAR ... nu naar MDB
Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\MENU.MDB; Persist Security Info=False"
Public EcMenuVars As New Hashtable 'Alle menu items variabelen
Public EcMenuFunc As New Hashtable 'Menu item uitvoeren
Private sFolder As String = System.Environment.CurrentDirectory()
Private Sub MenuBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadMenuItems()
End Sub
Function LoadMenuItems() As Boolean
'Laad alle menu items in het menu
Dim dsItems As New DataSet
Dim i As Integer
Dim Sql As String
Sql = "SELECT ROWCOL,TEXT,FUNC FROM ITEMS ORDER BY " & "ROWCOL" & " ASC"
Dim adap As System.Data.OleDb.OleDbDataAdapter
adap = New System.Data.OleDb.OleDbDataAdapter(Sql, conn)
adap.Fill(dsItems)
'************************************************************
'TestData (steeds uitzetten indien WriteXmlSchema aan staat)!
'Dim objXmlDocument As New System.Xml.XmlDocument
'Dim xsdFile As String = sFolder & "\TEST.xsd"
'objXmlDocument.LoadXml(dsItems.GetXml())
'objXmlDocument.Save(xsdFile)
'************************************************************
' Set PrimaryKey
dsItems.Tables(0).Columns("rowcol").Unique = True
dsItems.Tables(0).PrimaryKey = New DataColumn() {dsItems.Tables(0).Columns("rowcol")}
For i = 0 To dsItems.Tables(0).Rows.Count - 1
Dim id_ROWCOL As String 'sub items en sub sub items en ...
Dim parent As String 'tijdelijke var om parent index te bewaren
id_ROWCOL = dsItems.Tables(0).Rows(i)("ROWCOL")
'VERTICAAL
Dim menu_it As New DinamicMenu
menu_it.Text = dsItems.Tables(0).Rows(i)("ROWCOL")
menu_it.Func = dsItems.Tables(0).Rows(i)("FUNC")
menu_it.MenuItemobj = New MenuItem
menu_it.MenuItemobj.Text = dsItems.Tables(0).Rows(i)("TEXT")
AddHandler menu_it.MenuItemobj.Click, AddressOf CallForMenuItem
'sort is ok
If Not EcMenuVars.ContainsKey(dsItems.Tables(0).Rows(i)("ROWCOL")) Then
EcMenuVars.Add(dsItems.Tables(0).Rows(i)("ROWCOL"), menu_it)
End If
While id_ROWCOL.IndexOf(".") > 0
parent = id_ROWCOL.Substring(0, id_ROWCOL.LastIndexOf("."))
id_ROWCOL = parent
End While
Next
Dim d As DictionaryEntry
Dim parentMenu As New MenuItem
Dim sonMenu As New MenuItem
Dim parentItem As New DinamicMenu
Dim sonItem As New DinamicMenu
Dim sel_key, sel_KeyParent As String
For Each d In EcMenuVars
sonItem = d.Value
sel_key = d.Key
sonMenu = sonItem.MenuItemobj
If sel_key.LastIndexOf(".") >= 0 Then
sel_KeyParent = sel_key.Substring(0, sel_key.LastIndexOf("."))
parentItem = EcMenuVars(sel_KeyParent)
parentMenu = parentItem.MenuItemobj
parentMenu.MenuItems.Add(sonMenu)
Else
Me.MainMenu.MenuItems.Add(sonMenu)
End If
Next
CreateEcMenuFunc()
End Function
Private Sub CallForMenuItem(ByVal obj As Object, ByVal ea As EventArgs)
'menu item geklikt? actie !
Dim i As New MenuItem
Dim pai As New MenuItem
Dim position As String
'bepaal de positie
i = obj
pai = i.Parent
position = i.Index
While (pai.Parent.GetType().ToString <> "System.Windows.Forms.MainMenu")
position = pai.Index & "." & position
pai = pai.Parent
End While
position = pai.Index & "." & position
Dim funcToCall As String
funcToCall = EcMenuFunc(position)
'// begin OPDRACHT UITVOEREN
MsgBox("Opdracht " & funcToCall & " uitvoeren ...")
If funcToCall = "exit()" Then Application.Exit()
'// einde OPDRACHT UITVOEREN
End Sub
Function CreateEcMenuFunc() As Boolean
'De HashTable aanmaken (menu informatie)
'voor elke menu positie
Dim position As String
Dim pai As New MenuItem
Dim item As New MenuItem
Dim d As New DictionaryEntry
Dim func As String = ""
For Each d In EcMenuVars
item = d.Value.MenuItemObj
position = item.Index
If (item.Parent.GetType().ToString = "System.Windows.Forms.MainMenu") Then
position = item.Index
Else
pai = item.Parent
While (pai.Parent.GetType().ToString <> "System.Windows.Forms.MainMenu")
position = pai.Index & "." & position
pai = pai.Parent
End While
position = pai.Index & "." & position
func = d.Value.Func
MsgBox(position.ToString)
End If
'relatie menu positie en functie om uit te voeren
EcMenuFunc.Add(position, func)
Next
End Function
Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.LayoutMdi(MdiLayout.Cascade)
End Sub
Private Sub TileVerticleToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub
Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.LayoutMdi(MdiLayout.ArrangeIcons)
End Sub
Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
' Close all child forms of the parent.
For Each ChildForm As Form In Me.MdiChildren
ChildForm.Close()
Next
End Sub
Private m_ChildFormNumber As Integer
End Class
Public Class DinamicMenu
'Dinamisch Menu item
Public Func As String 'de functie welke wordt uitgevoerd
Public Text As String 'de tekst (getoonde)
Public MenuItemobj As MenuItem 'het menuitem zelf
End Class