Visual Basic > Visual Basic Forums > Visual Basic Interop and Upgrade > how to raise user events in VS 2005 (COM) sub-Winform, handle in VB6
Ask a questionAsk a question
 

Answerhow to raise user events in VS 2005 (COM) sub-Winform, handle in VB6

  • Thursday, June 07, 2007 1:08 PMtriunity Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 is where the .NET (sub) form is actually run
     
    Public Sub cmdManipulatePayers_Click()
     
       Screen.MousePointer = vbHourglass
       
          With clsOBRPayer
             .Billing_Patient_ID = 902264244
             .Admit_ID = 1
             .Patient_First_Name = "Silly"
             .Patient_Last_Name = "Willy"
             .OBRMode = True
             .runForm
          End With
     
       Screen.MousePointer = vbDefault
       
     End 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()
        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 ... triunity

Answers

All Replies

  • Friday, June 08, 2007 3:50 PMToddap_MSModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

     

  • Friday, November 06, 2009 5:03 AMarvindch Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.