How to set an objects property using Reflection

D

diego

hi everyone,

i have a sub that opens a form given the form's name as string and
opens it using System.Reflection. How can I set the form's properties
at runtime. Here is my code.

Public Sub OpenForm(ByVal cFormName)
Dim frm As Object
Dim frmType As Type

Try
frmType =
Type.GetType(System.Reflection.Assembly.GetExecutingAssembly.GetName.Name()
& cFormName)
frm = Activator.CreateInstance(frmType)

'**********************************
' i would like to set the form's mdichild property, text, etc before i
show it. how can this be done?
'**********************************

frmType.InvokeMember("Show",
Reflection.BindingFlags.InvokeMethod, Nothing, frm, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


thanks in advance

diego
 
M

Michel Posseth [MCP]

well just cast the object pointer to the actuall interface and you can do
everything you want

In your eaxample code i do not see what is the advantage to use reflection
i only use reflection when i need a generic object invoke pattern ( late
bound with public interface ) i.o.w. a plugin architecture

regards

Michel Posseth
 
D

diego

Hi Michael,

thanks for your reply.

what i want to do is, read all my menu items from a database and from
that get the corresponding form name to be opened when the menu item is
clicked.

Anyway i have found a way to do it. below is my code. Please tell me of
what the disadvatahes are when using this approach. Thanks in advance.

'*********************************************
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadMenu()
Me.Menu = mnuMain
End Sub

Public Sub OpenForm(ByVal cFormName, ByVal frmParent)
Dim frm As Object
Dim frmType As Type
Dim oForm As Form

Dim lIsOpen As Boolean

Try
frmType =
Type.GetType(System.Reflection.Assembly.GetExecutingAssembly.GetName.Name()
& "." & cFormName)
frm = Activator.CreateInstance(frmType)
oForm = DirectCast(frm, Form)

If Not IsFormOpen(cFormName) Then
frmType.InvokeMember("Show",
Reflection.BindingFlags.InvokeMethod, Nothing, oForm, Nothing)
End If
oForm.BringToFront()
oForm.MdiParent = frmParent
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Function IsFormOpen(ByVal cFormName As String) As Boolean
Dim oForm As Form
Dim lRet As Boolean = False

Try
For Each oForm In Me.MdiChildren
If oForm.Name = cFormName Then
If oForm.Visible Then
lRet = True
Exit For
End If
End If
Next
Catch ex As Exception
Finally
IsFormOpen = lRet
End Try
End Function

Private Sub LoadMenu()

Dim oItem As MyMenuItem
Dim lFirst As Boolean = True

mnuMain.MenuItems.Clear()
Try
cmd = cnn.CreateCommand
With cmd
.CommandText = "SELECT * FROM MENUS ORDER BY ID"
.CommandType = CommandType.Text
End With
da.SelectCommand = cmd
da.Fill(ds, "MENUS")

For Each oRow As DataRow In ds.Tables(0).Rows
Select Case oRow("MENUTYPE")
Case 1 ' TOP LEVEL MENU
If Not lFirst Then
mnuMain.MenuItems.Add(oMenuItem)
Else
lFirst = False
End If
oMenuItem = New MenuItem(oRow("MENUITEM"))
Case 2 ' MENU ITEM
If oRow("MENUKIND") = 0 Then ' EXIT MENU
' CloseMe() just calls Me.Close()
oItem = New MyMenuItem(oRow("MENUITEM"),
New System.EventHandler(AddressOf CloseMe))
Else ' REGULAR MENU ITEM
' I HAVE CREATED A CLASS THAT INHERITS FROM THE MENUITEM CLASS
' SO THAT I CAN STORE THE FORM'S NAME IN THE MENUITEM
' DoMenu simply calls the OpenForm procedure
oItem = New MyMenuItem(oRow("MENUITEM"),
New System.EventHandler(AddressOf DoMenu))
oItem.FormName = oRow("FORMNAME")
End If
oMenuItem.MenuItems.Add(oItem)
End Select
Next
mnuMain.MenuItems.Add(oMenuItem)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub DoMenu(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim oItem As MyMenuItem
oItem = DirectCast(sender, MyMenuItem)

OpenForm(oItem.FormName, Me)
End Sub
'*********************************************


Ayon kay Michel Posseth [MCP]:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top