473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to set an objects property using Reflection

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
2 1917

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary K | last post by:
I'm retrieving all the properties from a DirectoryEntry object and some of the properties are __ComObjects. How can I handle these in my code? Basically, I want to display the values in HRF (human...
1
by: Erik | last post by:
I have a base collection class that I use to derive all my collections from. It derives from CollectionBase. I have added custom Sorting and now would like to add Filtering. I have spent a...
1
by: emma middlebrook | last post by:
Hi I want to find out what objects are due to receive an event i.e. those that have added themselves as an event handler via +=. Yes, it's a little pointless perhaps (or can anyone give some...
3
by: Steve Montague | last post by:
How does one test to insure a property of an objects exists? The debugger shows the object.property as: "error: 'object.property' does not exist" using: if (object.property != null) results in:...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
4
by: orekinbck | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
3
by: MarkusJ_NZ | last post by:
Hi, I am trying to dynamically populate an objects properties using reflection. Specifically, I have an object called the theObj and it has a number of properties. property is a PropertyInfo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.