By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

How to set an objects property using Reflection

P: n/a
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.GetExecuti ngAssembly.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

Dec 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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
"diego" <di******@yahoo.comschreef in bericht
news:11*********************@j72g2000cwa.googlegro ups.com...
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.GetExecuti ngAssembly.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

Dec 2 '06 #2

P: n/a
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.GetExecuti ngAssembly.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]:
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
"diego" <di******@yahoo.comschreef in bericht
news:11*********************@j72g2000cwa.googlegro ups.com...
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.GetExecuti ngAssembly.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
Dec 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.