G
Guy Gani
Hello,
I have a small application with one form and a user control on it. The user control is loaded during runtime and can be changed when the user selects the appropriate one from a tree, something like microsoft .msc files.
I would like to have one function on the form that handles all the user events that raised from the various user controls. The problem is that that user controls are different and raised different events and I couldn't figure out how to do that. (it is possible in VB6 by declaring the user control with VBControlExtender and handle all events using ObjectEvent procedure on the form).
I used the following code whenever creating the usercontrol at runtime on the form:
Private ctl_Module As Windows.Forms.UserControl 'Embedded user control object
Private Sub ShowControl(ByRef pCtrl As Windows.Forms.UserControl)
Dim Idx As Integer
Dim eh As System.Delegate
eh = New EventHandler(AddressOf myGeneralEventHandler)
Dim events_info As System.Reflection.EventInfo() = ctl_Module.GetType.GetEvents()
For Idx = 0 To events_info.Length - 1
If events_info(Idx).Name = "LogStatus" Then 'Test to exclude user control events
events_info(Idx).AddEventHandler(pCtrl, eh) '<-- This is where the error occured
End If
Next
End Sub
This piece of code compiles fine, however when I run it I got the following error message:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Object type cannot be converted to target type.
Anyone knows why ?
Thanks,
Guy
I have a small application with one form and a user control on it. The user control is loaded during runtime and can be changed when the user selects the appropriate one from a tree, something like microsoft .msc files.
I would like to have one function on the form that handles all the user events that raised from the various user controls. The problem is that that user controls are different and raised different events and I couldn't figure out how to do that. (it is possible in VB6 by declaring the user control with VBControlExtender and handle all events using ObjectEvent procedure on the form).
I used the following code whenever creating the usercontrol at runtime on the form:
Private ctl_Module As Windows.Forms.UserControl 'Embedded user control object
Private Sub ShowControl(ByRef pCtrl As Windows.Forms.UserControl)
Dim Idx As Integer
Dim eh As System.Delegate
eh = New EventHandler(AddressOf myGeneralEventHandler)
Dim events_info As System.Reflection.EventInfo() = ctl_Module.GetType.GetEvents()
For Idx = 0 To events_info.Length - 1
If events_info(Idx).Name = "LogStatus" Then 'Test to exclude user control events
events_info(Idx).AddEventHandler(pCtrl, eh) '<-- This is where the error occured
End If
Next
End Sub
This piece of code compiles fine, however when I run it I got the following error message:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Object type cannot be converted to target type.
Anyone knows why ?
Thanks,
Guy