Reflection issue

G

Guest

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(RadioButton))
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
 
W

Walter Wang [MSFT]

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.
 
G

Guest

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
 
W

Walter Wang [MSFT]

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.
 
G

Guest

I t

:
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
 
W

Walter Wang [MSFT]

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.
 
G

Guest

Hi,really sorry forthis mistake, i forgot to take oftheAdressof. Yes now its
working like a charm.
Best regards
 
W

Walter Wang [MSFT]

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.
 

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