Que tal amigos? fijense que ando desarrollando una aplicacion con menús dinámicos en VB.Net , los menús me corren de maravilla, sin embargo mi problema viene en la conversion de el nombre del frm que se obtiene de la base de datos en tipo cadena a un objeto de tipo form que pueda usarse para invocar el formulario correcto.
Encontré varios códigos en internet, pero ninguno me ha funcionado, el que actualmente estoy trabajando es este:
Código:
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic.CallType
Public Class Main
Public user, idrol, iuseraccessmode As String
Dim consulta As String
Dim con As New MySqlConnection
Dim comando As New MySqlCommand
Dim adaptador As New MySqlDataAdapter
Dim lector As MySqlDataReader
Dim datos As New DataTable
Dim mnMenu As MenuStrip
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoginForm1.Close()
'MsgBox(iuseraccessmode)
con = New MySqlConnection
con.ConnectionString = "server = 192.168.1.3;" & "user = zzzzzzzz;" & "password = xxxxxxxxx;" & "database = xxxxxx;"
Try
con.Open()
consulta = "Select menutext from menumaster Where mainmenu = 0" & _
" And menuid in (Select menuid from acceso Where idrol =" & _
CInt(iuseraccessmode) & ") " & _
"And isActive = 1"
comando.Connection = con
comando.CommandText = consulta
lector = comando.ExecuteReader
If lector.HasRows Then
mnMenu = New MenuStrip
While lector.Read()
mnMenu.Items.Add(lector(0).ToString, Nothing, New System.EventHandler(AddressOf MainMenu_OnClick))
Me.Controls.Add(mnMenu)
End While
End If
lector.Close()
Catch ex As Exception
MsgBox("El siguiente error fue detectado: " & ex.Message.ToString, MsgBoxStyle.Critical)
End Try
con.Close()
End Sub
Private Sub MainMenu_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cms As New ContextMenuStrip()
Dim sMenu() As String
con = New MySqlConnection
con.ConnectionString = "server = 192.168.1.3;" & "user = xxxxxxxx;" & "password = xxxxxxxx;" & "database = xxxxxxxx;"
Try
con.Open()
consulta = "Select menuid from menumaster Where menutext = '" & sender.ToString & "'"
comando.Connection = con
comando.CommandText = consulta
lector = comando.ExecuteReader
Dim parentMenuID As Integer
If lector.HasRows Then
lector.Read()
parentMenuID = lector("menuid")
End If
lector.Close()
consulta = "Select menutext from menumaster Where mainmenu ='" & _
parentMenuID & " '" & _
"And isActive = 1" & _
" And MenuID in (" & _
"Select menuid from acceso Where idrol =" & _
CInt(iuseraccessmode) & ")" & _
" Order BY MenuOrder"
comando.Connection = con
comando.CommandText = consulta
lector = comando.ExecuteReader
ReDim Preserve sMenu(0)
Dim i As Integer
If lector.HasRows Then
ReDim Preserve sMenu(0)
i = 0
While lector.Read()
ReDim Preserve sMenu(i)
sMenu(i) = lector("menutext")
i = i + 1
End While
End If
lector.Close()
For Each sMn As String In sMenu
cms.Items.Add(sMn, Nothing, New System.EventHandler(AddressOf SelectedChildMenu_OnClick))
Next
Dim tsi As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
tsi.DropDown = cms
tsi.ShowDropDown()
Catch ex As Exception
MsgBox("El siguiente error fue detectado: " & ex.Message.ToString, MsgBoxStyle.Critical)
End Try
con.Close()
End Sub
Private Sub SelectedChildMenu_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim frmName As String
'Dim frm As New Form
con = New MySqlConnection
con.ConnectionString = "server = 192.168.1.3;" & "user = xxxxxxxxx;" & "password = xxxxxxxxx;" & "database = xxxxxxx;"
Try
con.Open()
consulta = "Select formname from menumaster Where menutext = '" & _
sender.ToString & "'"
comando.Connection = con
comando.CommandText = consulta
lector = comando.ExecuteReader
If lector.HasRows Then
lector.Read()
frmName = lector(0).ToString
lector.Close()
Dim frm As Form = DynamicallyLoadedObject(frmName)
frm.MdiParent = Me
frm.Show()
Else
MsgBox("Under Construction", MsgBoxStyle.Exclamation, "Technical Error")
End If
Catch ex As Exception
MsgBox("El siguiente error fue detectado: " & ex.Message.ToString, MsgBoxStyle.Critical)
End Try
con.Close()
End Sub
Private Function DynamicallyLoadedObject(ByVal objectName As String, _
Optional ByVal args() As Object = Nothing) As Form
Dim returnObj As Object = Nothing
Dim Type As Type = Assembly.GetExecutingAssembly().GetType( _
"SysROCAR." & objectName)
If Type IsNot Nothing Then
returnObj = Activator.CreateInstance(Type, args)
End If
Return returnObj
End Function
End Class
Al darle click al menu que quiero abrir me manda el error: "Referencia a objeto no establecida como instancia de un objeto".
El problema empieza en la funion SelectedChildMenu_OnClick la cual manda un parametro a la funcion DynamicallyLoadedObject quien se encarga de el ensamblado de la cadena a objeto. Espero alguien pueda ayudarme porque ya me siento frustrado ... saludos.