473,479 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reflection issue

I am trying to create a generic routine to create dynamically either Radio
buttons or CheckBoxes. The RadioButton has the event "CheckedChanged" which
doesn't exist in the CheckBox control.
I tried to create the controls through reflection, then check if it has this
property as follows to add a handler as follows

Dim rdoControl1 = GetControl(1)
Dim typelocal As Type = rdoControl1.GetType
Dim ev() As EventInfo = typelocal.GetEvents
Dim ev1 As EventInfo = typelocal.GetEvent("CheckedChanged")
If Not ev1 Is Nothing Then
AddHandler rdoControl1.CheckedChanged, AddressOf GenericClick
End If

Private Function GetControl(ByVal ControlType As Integer) As Control
Dim Configcontrol
If ControlType = 1 Then
Configcontrol = System.Activator.CreateInstance(GetType(RadioButto n))
end IF
Return Configcontrol
End Function

When I try to compile and run I can not because I get an error indicating
that "CheckedChanged" is not an event of the system.object.

Thanks for your help
May 31 '06 #1
7 1453
Hi,

Thank you for your post.

I think you need to cast the object to a strong type first:

If Not ev1 is Nothing Then
Dim r As RadioButton = DirectCast(rdoControl1, RadioButton)
AddHandler r.CheckedChanged, AddressOf GenericClick
End If

Also, both RadioButton and CheckBox should have the CheckedChanged event.
You can distinguish them by:

If TypeOf rdoControl1 Is CheckBox Then

EleseIf TypeOf rdoControl1 Is RadioButton Then

End If

Hope this helps. If anything is unclear, please feel free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 1 '06 #2
So man thanks, correct,with direct cast this can solve the issue. I was
trying to avoid to hard code if ...Radiobutton
Now, the real question, in the
***********
AddHandler r.CheckedChanged, AddressOf GenericClick
*************
Line
Is it possible or is there a way that the delegate (GenericClick) be a
variable. Let me explain/
Private Sub LoadUI(ByVal ControlType As Int32, ByVal EventHandlerName As
String)

MyCode.......
AddHandler obj.CheckedChanged, AddressOf EventHandlerName
End Sub

When I call the sub, I provide the delegate name as follows

LoadUI(1, "GenericClick_Radio')
LoadUI(1, "GenericClick_CheckBox')

Of course the 32 delegates exist in the class

Thanks again


"Walter Wang [MSFT]" wrote:
Hi,

Thank you for your post.

I think you need to cast the object to a strong type first:

If Not ev1 is Nothing Then
Dim r As RadioButton = DirectCast(rdoControl1, RadioButton)
AddHandler r.CheckedChanged, AddressOf GenericClick
End If

Also, both RadioButton and CheckBox should have the CheckedChanged event.
You can distinguish them by:

If TypeOf rdoControl1 Is CheckBox Then

EleseIf TypeOf rdoControl1 Is RadioButton Then

End If

Hope this helps. If anything is unclear, please feel free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 1 '06 #3
Hi,

Thank you for your update.

I think it can be done.

Let's assume you have 32 delegates (event handler) already defined in the
class and you know their signature (delegate type). We can use a hashtable
to hold these delegates:

Dim delegates as New Hashtable
delegates.Add("CheckedChanged", new EventHandler(AddressOf
Object_CheckedChanged))
delegates.Add("MouseClick", new MouseEventHandler(AddressOf
Object_MouseClick))
Then in LoadUI, we simply pass the hashtable key and get the delegate:

Private Sub LoadUI(ByVal ControlType As Int32, ByVal EventHandlerName As
String)

MyCode.......
AddHandler obj.CheckedChanged, delegates(EventHandlerName)
End Sub

Hope this helps.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 1 '06 #4
I t

"Walter Wang [MSFT]" wrote:
hink it is a good idea, however, it didn,t work.
I added

Dim delegates As New Hashtable

in the class level, then in Page_init, I populated it as follows

delegates.Add("RB_CheckedChanged", New EventHandler(AddressOf GenericClick))
delegates.Add("ChkB_CheckedChanged", New EventHandler(AddressOf
GenericClick_CheckBox))
In LoadUI when I replace
*********
AddHandler obj.CheckedChanged, AddressOf GenericClick
**************
By
****************************
AddHandler obj.CheckedChanged, AddressOf delegates(EventHandlerName)
*****************
I get the following desgin time error (continous blue ziczac line underneath
it:
'AddressOf' operand must be the name of a method; no parentheses are needed.'
'

Did it work for you?

Salam
Hi,

Thank you for your update.

I think it can be done.

Let's assume you have 32 delegates (event handler) already defined in the
class and you know their signature (delegate type). We can use a hashtable
to hold these delegates:

Dim delegates as New Hashtable
delegates.Add("CheckedChanged", new EventHandler(AddressOf
Object_CheckedChanged))
delegates.Add("MouseClick", new MouseEventHandler(AddressOf
Object_MouseClick))
Then in LoadUI, we simply pass the hashtable key and get the delegate:

Private Sub LoadUI(ByVal ControlType As Int32, ByVal EventHandlerName As
String)

MyCode.......
AddHandler obj.CheckedChanged, delegates(EventHandlerName)
End Sub

Hope this helps.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 1 '06 #5

Hi,

Thank you for your update.

AddressOf is a way to get the address of a method for use in creating a
delegate. Normally you would create a delegate like this (or something
similar):

Dim mydel as New EventHandler(AddressOf Object_CheckedChanged)

and then you would use the delegate variable mydel.

However, VB.NET allows you to use AddressOf to shortcut the delegate
creation step by creating the delegate for you behind the scenes. For
instance if you do the following:

AddHandler obj.CheckedChanged, AddressOf Object_CheckedChanged

the compiler knows that the call needs an EventHandler delegate and so
transparently creates one.

Since here we are using the hashtable to store the delegates, and a
hashtable stores generic objects, there is no way for the compiler to
transparently create your delegate. So we must explicitly create the
delegate and then put them in the hashtable.

Then when you use the key to retrieve a delegate from the hashtable, it's
already a delegate and you do not need to use AddressOf again.

Hope this helps. If anything is unclear, please feel free to post here.
Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 2 '06 #6
Hi,really sorry forthis mistake, i forgot to take oftheAdressof. Yes now its
working like a charm.
Best regards

"Walter Wang [MSFT]" wrote:

Hi,

Thank you for your update.

AddressOf is a way to get the address of a method for use in creating a
delegate. Normally you would create a delegate like this (or something
similar):

Dim mydel as New EventHandler(AddressOf Object_CheckedChanged)

and then you would use the delegate variable mydel.

However, VB.NET allows you to use AddressOf to shortcut the delegate
creation step by creating the delegate for you behind the scenes. For
instance if you do the following:

AddHandler obj.CheckedChanged, AddressOf Object_CheckedChanged

the compiler knows that the call needs an EventHandler delegate and so
transparently creates one.

Since here we are using the hashtable to store the delegates, and a
hashtable stores generic objects, there is no way for the compiler to
transparently create your delegate. So we must explicitly create the
delegate and then put them in the hashtable.

Then when you use the key to retrieve a delegate from the hashtable, it's
already a delegate and you do not need to use AddressOf again.

Hope this helps. If anything is unclear, please feel free to post here.
Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 2 '06 #7
Thank you for your update.

Glad to know it works now. Please feel free to post here if there's
anything I can help.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 2 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6350
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos...
8
16846
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
7747
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
3
2317
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
20
27723
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
2
4223
by: Wiktor Zychla [C# MVP] | last post by:
Could anyone confirm/deny that following is a bug (or at least an "unexpected behaviour")? If this is not a bug, I would be glad for a short explanation or a workaround. Issue: A generic...
15
2115
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
2
2962
by: =?Utf-8?B?UmVuYXVkIExhbmdpcw==?= | last post by:
Hello, I have an asp.net web page (say page.aspx) which derives from a custom base page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method called DoSomething(params). My web...
7
2661
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
11
1899
by: idoublepress | last post by:
Hi all, I've been struggling with an issue that I hope you can comment on or provide suggestions to. Our .NET 2.0 (VS2005) based product is crashing (when the user selects a particular feature on...
0
6903
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
7027
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
7071
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...
0
6861
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...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
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...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.