473,748 Members | 6,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflecti on. 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(Sy stem.Reflection .Assembly.GetEx ecutingAssembly .GetName.Name()
& cFormName)
frm = Activator.Creat eInstance(frmTy pe)

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

frmType.InvokeM ember("Show",
Reflection.Bind ingFlags.Invoke Method, Nothing, frm, Nothing)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
End Sub
thanks in advance

diego

Dec 2 '06 #1
2 1937

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******** *************@j 72g2000cwa.goog legroups.com...
hi everyone,

i have a sub that opens a form given the form's name as string and
opens it using System.Reflecti on. 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(Sy stem.Reflection .Assembly.GetEx ecutingAssembly .GetName.Name()
& cFormName)
frm = Activator.Creat eInstance(frmTy pe)

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

frmType.InvokeM ember("Show",
Reflection.Bind ingFlags.Invoke Method, Nothing, frm, Nothing)
Catch ex As Exception
MsgBox(ex.Messa ge)
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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(Sy stem.Reflection .Assembly.GetEx ecutingAssembly .GetName.Name()
& "." & cFormName)
frm = Activator.Creat eInstance(frmTy pe)
oForm = DirectCast(frm, Form)

If Not IsFormOpen(cFor mName) Then
frmType.InvokeM ember("Show",
Reflection.Bind ingFlags.Invoke Method, Nothing, oForm, Nothing)
End If
oForm.BringToFr ont()
oForm.MdiParent = frmParent
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
End Sub

Private Function IsFormOpen(ByVa l 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.MenuIte ms.Clear()
Try
cmd = cnn.CreateComma nd
With cmd
.CommandText = "SELECT * FROM MENUS ORDER BY ID"
.CommandType = CommandType.Tex t
End With
da.SelectComman d = cmd
da.Fill(ds, "MENUS")

For Each oRow As DataRow In ds.Tables(0).Ro ws
Select Case oRow("MENUTYPE" )
Case 1 ' TOP LEVEL MENU
If Not lFirst Then
mnuMain.MenuIte ms.Add(oMenuIte m)
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.EventHan dler(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.EventHan dler(AddressOf DoMenu))
oItem.FormName = oRow("FORMNAME" )
End If
oMenuItem.MenuI tems.Add(oItem)
End Select
Next
mnuMain.MenuIte ms.Add(oMenuIte m)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
End Sub

Private Sub DoMenu(ByVal sender As System.Object, ByVal e As
System.EventArg s)
Dim oItem As MyMenuItem
oItem = DirectCast(send er, 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******** *************@j 72g2000cwa.goog legroups.com...
hi everyone,

i have a sub that opens a form given the form's name as string and
opens it using System.Reflecti on. 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(Sy stem.Reflection .Assembly.GetEx ecutingAssembly .GetName.Name()
& cFormName)
frm = Activator.Creat eInstance(frmTy pe)

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

frmType.InvokeM ember("Show",
Reflection.Bind ingFlags.Invoke Method, Nothing, frm, Nothing)
Catch ex As Exception
MsgBox(ex.Messa ge)
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
310
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 readable format). I'm pretty sure that the values I'm dealing with are date/times but I'm not sure, so I will have to check the type as well.
1
7165
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 decent amount of time looking thru code and really have not found anything that suits my needs. I simply want to pass in a filter expression use that that expression against the protected List (exposed by CollectionBase) object and return an array list...
1
2542
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 good uses for this?!!). How do I do this for an event on a class I implement? Also, how may I do this for an event in the .Net framework e.g. a control?
3
21625
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: "Object reference not set to an instance of an object." Thanks for help!
11
12789
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
4
1741
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 properties are equal. It seems to work OK except when a object has a property that is an array. The GetValue method of the PropertyInfo class returns an object, and I want to convert it to an array of objects. For some reason, this
16
6430
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 sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
4
7139
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 Building._Name to Building._BName in case the duplicate name was the issue, but that didn't help. Is there a native way to serialize nested objects, or will I have to write my own? public class LocationCell
3
4123
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 object What I want to do is to be able to set the property value dynamically which I am doing so using this line of code (Simplified version below)
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.