how to raise user events in VS 2005 (COM) sub-Winform, handle in VB6
I am trying to raise a user-defined Event in a VB.NET 2005 COM Class(sub)form and have my calling VB6 form handle it.Code samples follow. In the VS 2005 code two events are coded identically -when they are RAISEd in reaction to a Framework Event they WORK, but when they are RAISEd in response to by user action within the VB.NET 2005 Subform they do NOT:===================================' 1. Here are the Events, defined in the same fashion in the VS 2005 (sub)form===================================#Region "User-Defined Variables"Public Event ExitOBRpayerDialog()Public Event NetworkAvailabilityChanged( _ByVal IsAvailable As Boolean)===================================' 2. Here are the Event Handlers for the Subs that will Raise the Events' to the calling VB6 form, both added in the VS 2005 (sub)form constructor===================================#Region " Windows Form Designer generated code "Public Sub New()MyBase.New()'This call is required by the Windows Form Designer.InitializeComponent()'Add any initialization after the InitializeComponent() callAddHandler Me.FormClosing, AddressOf frmOBRpayer_FormClosingAddHandler My.Computer.Network.NetworkAvailabilityChanged, _AddressOf My_NetworkAvailabilityChangedAddHandler btnCustom.Click, AddressOf btnCustom_ClickEnd Sub
===================================3. Here are the VS 2005 Subs that are supposed to Raise the Events.When the OS raises the My.Computer.Network.NetworkAvailabilityChangedevent, both User Events are raised by the first Sub below. They BOTHfire and are handled properly by the VB6 calling form!!!When either of the other two Subs is executed it is because of a FormEvent, i.e. a user clicking a button (Form Close in the 2nd sub andbtnCustom in the second). In this case, NEITHER User Event that is RAISEDgets back to the VB6 program and neither is handled!!! The VB6 code for both Events is similar also, but I include it further below for completenessanyhow.WHY oh WHY cannot I fire my User Events with a form button click? WHY does an Event raised by the OS work and a user action does not??? (BTW, the way the OS fires both events is when I pull the network cable out of myworkstation).===================================Public Sub My_NetworkAvailabilityChanged(ByVal sender As Object, _ByVal e As _Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)RaiseEvent ExitOBRpayerDialog()RaiseEvent NetworkAvailabilityChanged(e.IsNetworkAvailable)Application.DoEvents()End Sub-----------------------------------------------------------------------------Public Sub frmOBRpayer_FormClosing(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.FormClosingEventArgs)Dim crCloseReason As Windows.Forms.CloseReasonDim ev As NewMicrosoft.VisualBasic.Devices.NetworkAvailableEventArgs(False)TrycrCloseReason = CloseReason.UserClosingcrCloseReason = e.CloseReasonSelect Case crCloseReasonCase CloseReason.UserClosingPayerCode = "71LA"RaiseEvent ExitOBRpayerDialog()' DOES NOT FIRE - why oh why?Application.DoEvents()Case ElsePayerCode = "OOPS"If OBRMode ThenRaiseEvent ExitOBRpayerDialog()' DOES NOT FIRE - why oh why?Application.DoEvents()ElseRaiseEvent ExitOBRpayerDialog()' DOES NOT FIRE - why oh why?Application.DoEvents()End IfEnd SelectCatch ex As ExceptionIf exceptionPolicy.HandleException(ex) ThenMe.handle_Ignorable_Exception(ex)FinallyCall My_NetworkAvailabilityChanged(Me, ev)' DOES NOT FIRE even though the Sub is the one that RAISEsbothEvents!End TryEnd Sub-----------------------------------------------------------------------------Public Sub btnCustom_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)' Does a custom form operation. First removes any errors on the form,' then restores the data at it looked before the last user Save.Dim ev As NewMicrosoft.VisualBasic.Devices.NetworkAvailableEventArgs(True)TryCall My_NetworkAvailabilityChanged(Me, ev)' DOES NOT FIRE even though the Sub is the one that RAISEsbothEvents!Catch ex As ExceptionIf exceptionPolicy.HandleException(ex) ThenMe.handle_Ignorable_Exception(ex)End TryEnd Sub===================================' 4. Here is the VB6 calling form code for handling the two User-DefinedEvents===================================Option ExplicitPrivate WithEvents clsOBRPayer As frmOBRpayer...Private Sub Form_Load()' Instantiate the object defined above as "With Events"' Note that this object is NEVER reset to 'Nothing'.Set clsOBRPayer = New frmOBRpayer...' Here is where the .NET (sub) form is actually runPublic Sub cmdManipulatePayers_Click()Screen.MousePointer = vbHourglassWith clsOBRPayer.Billing_Patient_ID = 902264244.Admit_ID = 1.Patient_First_Name = "Silly".Patient_Last_Name = "Willy".OBRMode = True.runFormEnd WithScreen.MousePointer = vbDefaultEnd Sub-----------------------------------------------------...' Here are the Event Handlers for the two User-Defined events. BOTH are' handled and ONLY handled when the OS fires the' My.Computer.Network.NetworkAvailabilityChanged event-----------------------------------------------------Private Sub clsOBRPayer_ExitOBRpayerDialog()ShowPayerCodeEnd SubPrivate Sub clsOBRPayer_NetworkAvailabilityChanged( _ByVal IsAvailable As Boolean)ShowAvailable IsAvailableEnd SubPrivate Sub ShowPayerCode()Dim intReturnCode As IntegerintReturnCode = MsgBox("PayerCode: [" & _clsOBRPayer.PayerCode & "], " & _"Application_Startup_Path: [" & _clsOBRPayer.Application_Startup_Path & "].")End SubPrivate Sub ShowAvailable(IsAvailable As Boolean)MsgBox (IIf(IsAvailable, "Online", "Offline"))End Sub===================================Help would be much appreciated! Warm regards ... triunity
Answers
I'm not clear how you are actually exposing the events to com, but in any case, for scenarios like this, you should check out using the InteropForms toolkit. It contains full (or better) support for this type of thing and I verified that your scenario seems to work fine with an InteropForm form.
Check out this for more details:
http://blogs.msdn.com/vbteam/archive/tags/VB6_5F00_Migration_2F00_Interop/default.aspx
Toddap_MS
All Replies
I'm not clear how you are actually exposing the events to com, but in any case, for scenarios like this, you should check out using the InteropForms toolkit. It contains full (or better) support for this type of thing and I verified that your scenario seems to work fine with an InteropForm form.
Check out this for more details:
http://blogs.msdn.com/vbteam/archive/tags/VB6_5F00_Migration_2F00_Interop/default.aspx
Toddap_MS
I am also facing the exact problem. Can some one trace out what is the problem?
Note :
1. When I am rasing the event from a .NET class, the events are handled in VB6.
2. I am not using Interop Toolkit and dont want to use it.
However, the following scenario is failing:
1. I try to raise the event from a .NET Win Form on a Button click.
2. I am handling this event in .NET class in and it is handled.
3. Again if I try to raise an event from the above event handler and try to capture in VB6, it is not handled.
Please suggest any solution.


