Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / COM / June 2007



Tip: Looking for answers? Try searching our database.

how to raise user events in VB 2005 COM Class and handle with VB6

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
triunity - 04 Jun 2007 21:18 GMT
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() call
       AddHandler Me.FormClosing, AddressOf frmOBRpayer_FormClosing
       AddHandler My.Computer.Network.NetworkAvailabilityChanged, _
           AddressOf My_NetworkAvailabilityChanged
       AddHandler btnCustom.Click, AddressOf btnCustom_Click

   End Sub
===================================
3. Here are the VS 2005 Subs that are supposed to Raise the Events.
When the OS raises the My.Computer.Network.NetworkAvailabilityChanged
event, both User Events are raised by the first Sub below.  They BOTH
fire and are handled properly by the VB6 calling form!!!
When either of the other two Subs is executed it is because of a Form
Event, i.e. a user clicking a button (Form Close in the 2nd sub and
btnCustom in the second).  In this case, NEITHER User Event that is RAISED
gets 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 completeness
anyhow.
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 my
workstation).
===================================
   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 As
System.Windows.Forms.FormClosingEventArgs)
       Dim crCloseReason As Windows.Forms.CloseReason
       Dim ev As New
Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs(False)
       Try
           crCloseReason = CloseReason.UserClosing
           crCloseReason = e.CloseReason
           Select Case crCloseReason
               Case CloseReason.UserClosing
                   PayerCode = "71LA"
                   RaiseEvent ExitOBRpayerDialog()
           ' DOES NOT FIRE - why oh why?
                   Application.DoEvents()
               Case Else
                   PayerCode = "OOPS"
                   If OBRMode Then
                       RaiseEvent ExitOBRpayerDialog()
           ' DOES NOT FIRE - why oh why?
                       Application.DoEvents()
                   Else
                       RaiseEvent ExitOBRpayerDialog()
           ' DOES NOT FIRE - why oh why?
                       Application.DoEvents()
                   End If
           End Select
       Catch ex As Exception
           If exceptionPolicy.HandleException(ex) Then
Me.handle_Ignorable_Exception(ex)
       Finally
           Call My_NetworkAvailabilityChanged(Me, ev)
           ' DOES NOT FIRE even though the Sub is the one that RAISEs
bothEvents!
       End Try

   End Sub
-----------------------------------------------------------------------------
   Public Sub btnCustom_Click(ByVal sender As System.Object, ByVal e As
System.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 New
Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs(True)
       Try
           Call My_NetworkAvailabilityChanged(Me, ev)
           ' DOES NOT FIRE even though the Sub is the one that RAISEs
bothEvents!
       Catch ex As Exception
           If exceptionPolicy.HandleException(ex) Then
Me.handle_Ignorable_Exception(ex)
       End Try
   End Sub
===================================
' 4. Here is the VB6 calling form code for handling the two User-Defined
Events
===================================
Option Explicit

Private 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 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()
   ShowPayerCode
End Sub

Private Sub clsOBRPayer_NetworkAvailabilityChanged( _
       ByVal IsAvailable As Boolean)
   ShowAvailable IsAvailable
End Sub

Private Sub ShowPayerCode()
   Dim intReturnCode As Integer
   intReturnCode = MsgBox("PayerCode: [" & _
       clsOBRPayer.PayerCode & "], " & _
       "Application_Startup_Path: [" & _
       clsOBRPayer.Application_Startup_Path & "].")
End Sub

Private Sub ShowAvailable(IsAvailable As Boolean)
   MsgBox (IIf(IsAvailable, "Online", "Offline"))
End Sub

===================================
Help would be much appreciated!  Warm regards,
Signature

triunity

Ken Halter - 04 Jun 2007 21:27 GMT
>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.

If that VB6 form is modal, try showing it non-modal, at least while running
in the IDE. Other than that single "gotcha", the problem is surely a VSNet
problem and since very few around here use VSNet, you'll be far better off
asking in a dotNet group. They all contain "dotnet" or "vsnet" in their
names. All groups on the ms server that start with "microsoft.public.vb" are
for pre-dotnet versions of VB.

Signature

Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.