General Event Handler

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
 
O

One Handed Man \( OHM - Terry Burns \)

You could start your application from a Sub Main, and have a Try / Catch Block around the form


Try

Dim mf as new Form1
mf.ShowDialog()


Catch GeneralException as Exception

'

End Try


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


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
 
G

Guy Gani

This is a nice workaround that I haven't tought about, however I would like to handle on the form the user control events raised by RaiseEvent and deal with exceptions in the regular exception handler.
Please take a look at my code and see why it breaks, if this piece of code is fixed, it should work fine.

Thanks

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message You could start your application from a Sub Main, and have a Try / Catch Block around the form


Try

Dim mf as new Form1
mf.ShowDialog()


Catch GeneralException as Exception

'

End Try


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


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
 
O

One Handed Man \( OHM - Terry Burns \)

This on its own does not give me enough information. As you say this compiles ok, but I dont know how you are calling the show control and what you have on the form.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


This is a nice workaround that I haven't tought about, however I would like to handle on the form the user control events raised by RaiseEvent and deal with exceptions in the regular exception handler.
Please take a look at my code and see why it breaks, if this piece of code is fixed, it should work fine.

Thanks

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message You could start your application from a Sub Main, and have a Try / Catch Block around the form


Try

Dim mf as new Form1
mf.ShowDialog()


Catch GeneralException as Exception

'

End Try


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


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
 
P

Paul

Perhaps I'm wrong but this sounds like a solution I found in MS IssueVision Smart Client sample app (nice!). This uses a command object to sink the events to a common handler. See Command.vb class in download. http://www.windowsforms.net/Applications/application.aspx?PageID=40&tabindex=9

Paul
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
 

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