473,378 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

General Event Handler

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

Jul 21 '05 #1
4 1415
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))
--
"Guy Gani" <ga*****@netvision.net.il> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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

Jul 21 '05 #2
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 news:%2***************@tk2msftngp13.phx.gbl...
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))
--
"Guy Gani" <ga*****@netvision.net.il> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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

Jul 21 '05 #3
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))
--
"Guy Gani" <ga*****@netvision.net.il> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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 news:%2***************@tk2msftngp13.phx.gbl...
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))
--
"Guy Gani" <ga*****@netvision.net.il> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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

Jul 21 '05 #4
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/Applicat...=40&tabindex=9

Paul
"Guy Gani" <ga*****@netvision.net.il> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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

Jul 21 '05 #5

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

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
6
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
2
by: Eric Sabine | last post by:
I built a generic exception handler form which allows the user to get information from it, print it, email it, etc. for exceptions for which I explicitly didn't handle in code, such as missing...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
3
by: Woody Splawn | last post by:
We have a client server application where we would like to not show Save and Cancel buttons on a winform until the user has actually entered something into a textbox or combobox etc., and there is...
3
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I...
5
by: Richard Grant | last post by:
Hi, I need to "save" in a variable the event handler sub of a control's event, then perform some process, and finally "restore" the originally saved event handler. Example in pseudo-code: 1)...
7
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.