The most important function is
Public Function DynamicallyLoadedObject(ByVal objectName As String, Optional ByVal args() As Object = Nothing) As Object
Dim returnObj As Object = Nothing
Dim type As Type = Assembly.GetExecutingAssembly().GetType(Application.ProductName & "." & objectName)
If Not type Is Nothing Then
returnObj = Activator.CreateInstance(type, args)
End If
Return returnObj
End Function
This function converts form names from text format to form objects. Here is my piece of code generating menu and assigning events to menu click.
Private Sub menuClickedEvent(ByVal sender As Object, ByVal e As EventArgs)
Dim ChildForm As New System.Windows.Forms.Form
ChildForm = DynamicallyLoadedObject(newItem.Tag)
ChildForm.Show()
End Sub
Public Sub CreateMenu(ByVal MyMenuStrip As MenuStrip, Optional ByVal MyToolbar As ToolStrip = Nothing)
Dim MyDB As New LogicPool.DB(_MySettings._DataConnection)
Dim MYDS As New Data.DataSet
MyDB.SQLReturnDT(MYDS, "Menu", "SELECT * FROM SysMenu M, SysUserRight H WHERE H.SysMenuID = M.ID AND H.SysUserID = " & _User._ID & " ORDER BY SortOrder")
MyMenuStrip.Items.Clear()
MenuPrepare(MYDS.Tables("Menu"), MyMenuStrip, MyToolbar)
End Sub
Private Sub MenuPrepare(ByVal db As DataTable, ByVal MS As MenuStrip, Optional ByVal tb1 As System.Windows.Forms.ToolStrip = Nothing, Optional ByVal ref As Integer = 0, Optional ByVal ItemKey As Object = Nothing)
Dim z As Integer
Dim MyDB As New LogicPool.DB(_MySettings._DataConnection)
For z = 0 To db.Rows.Count - 1
' 1. Seviye
If ref = 0 Then
If CInt(db.Rows(z).Item("RefID")) = 0 Then
Dim newItem As New System.Windows.Forms.ToolStripMenuItem
If db.Rows(z).Item("FormName").ToString.Trim.Length > 0 Then
newItem = MS.Items.Add(db.Rows(z).Item("MenuName"), Nothing, New System.EventHandler(AddressOf menuClickedEvent))
newItem.Tag = db.Rows(z).Item("FormName")
Else
newItem = MS.Items.Add(db.Rows(z).Item("MenuName"), Nothing)
End If
If db.Rows(z).Item("ImageName").ToString.Trim.Length > 0 Then
Dim Newimg As System.Drawing.Image
Newimg = FrmImages.imgMenu.Images(db.Rows(z).Item("ImageName").ToString)
newItem.Image = Newimg
End If
newItem.Name = "Mn" & db.Rows(z).Item("ID")
MenuPrepare(db, MS, tb1, db.Rows(z).Item("ID"), newItem)
End If
' Diğer Seviyeler
Else
If CInt(db.Rows(z).Item("RefID")) = ref Then
Dim newItem As New System.Windows.Forms.ToolStripMenuItem
Dim OldItem As System.Windows.Forms.ToolStripMenuItem
OldItem = ItemKey
newItem = OldItem.DropDown.Items.Add(db.Rows(z).Item("MenuName"), Nothing, New System.EventHandler(AddressOf menuClickedEvent))
If db.Rows(z).Item("FormName").ToString.Trim.Length > 0 Then
newItem.Tag = db.Rows(z).Item("FormName")
End If
If db.Rows(z).Item("ImageName").ToString.Trim.Length > 0 Then
Dim Newimg As System.Drawing.Image
Newimg = FrmImages.imgMenu.Images(db.Rows(z).Item("ImageName").ToString)
newItem.Image = Newimg
If Not tb1 Is Nothing Then
If db.Rows(z).Item("Toolbar") = 1 Then
Dim TB As System.Windows.Forms.ToolStripItem
TB = tb1.Items.Add(db.Rows(z).Item("MenuName"), Newimg, New System.EventHandler(AddressOf ToolbarClickEvent))
TB.Tag = db.Rows(z).Item("FormName")
If MyDB.ParameterGet("ToolbarText") = 0 Then
TB.DisplayStyle = ToolStripItemDisplayStyle.Image
Else
TB.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
End If
TB.ToolTipText = db.Rows(z).Item("MenuName")
tb1.Items.Add(New System.Windows.Forms.ToolStripSeparator)
End If
End If
End If
newItem.Name = "Mn" & db.Rows(z).Item("ID")
MenuPrepare(db, MS, tb1, db.Rows(z).Item("ID"), newItem)
End If
End If
Next
End Sub
No comments:
Post a Comment