[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

how to raise user events in VS 2005 (COM) sub-Winform, handle in V

triunity

6/4/2007 9:44:00 PM

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,
--
triunity
1 Answer

JJ

6/6/2007 6:47:00 PM

0

I had to do the same thing. I got the answer here....

http://www.15seconds.com/issue/...