Procedure for Listening for Events in VB.NET |
Take the following steps to create a program that listens for event notifications from FREDI.
Example in Visual Basic
This example will use the following:
In this example, the program is created to listen for event notifications returned from FREDI ActiveX/COM when it reads an EDI file. The visual basic program contains a simple form having a command button to start the program execution, and a text control to display the event notifications.
Declare ediDocument object with WithEvents. Declare ediDocument as object that is an event source. The WithEvents keyword is used in the declaration as follows:
' Declare FREDI as event source.
Private WithEvents m_oEdiDoc As ediDocument
Create an instance of ediDocument object, and apply environment settings. The ediDocument object is the top level application instance for Framework EDI (FREDI). This object is the topmost object in the object model hierarchy of FREDI. This instance is created at the top of the form and the variable is a class member of the form.
Dim oSchemas As ediSchemas
Dim oSchema As ediSchema' Instance of FREDI as ediDocument.
m_oEdiDoc = New ediDocument' To traverse all the data segments in the document at the best possible speed, enable Cursor_ForwardOnly.
m_oEdiDoc.CursorType = Cursor_ForwardOnly' Disable standard reference library.
oSchemas = m_oEdiDoc.GetSchemas
oSchemas.EnableStandardReference = False
' Load implementation guideline used by the EDI document.
oSchema = m_oEdiDoc.LoadSchema(AppDomain.CurrentDomain.BaseDirectory & "..\SampleSefX12_850.SEF", SchemaTypeIDConstants.Schema_Standard_Exchange_Format)
Load EDI document. Load the EDI document to open and begin file traversal. When loaded successfully, traverse the file by stepping to each data segments in the EDI document.
' Load EDI document via the mailDocument object.
m_oEdiDoc.Load(AppDomain.CurrentDomain.BaseDirectory & "..\SampleEdiX12_850.X12")
' The EDI data is now made available in the ediDocument object.
Dim oSegment As ediDataSegment' Traverse all the data segments in the document.
oSegment = m_oEdiDoc.FirstDataSegment
While Not oSegment Is Nothing
' TODO: Do something with oSegment here.Wend
' Use the .Set method to let FREDI know that the client no longer
' needs the old data in oSegment. The new data returned
' by oSegment.Next will replace old data in oSegment.
ediDataSegment.Set(oSegment, oSegment.Next())MsgBox "Done"
Add event handler. By declaring the ediDocument as an event source using WithEvents, the variable m_oEdiDoc variable will appear on the drop-down list to the left of the editor.
Select the m_oEdiDoc variable and the drop-down list to the right of the editor will display all the events available to the ediDocument class.
Select the EventNotify event and the m_oEdiDoc_EventNotify event procedure is automatically added to the form. Add the following code to display the events in the text control Text1.
Private Sub m_oEdiDoc_EventNotify(ByVal EventNotify As Edidev.FrameworkEDI.ediEventNotify) Handles m_oEdiDoc.EventNotify
If EventNotify.Severity = EventIDConstants.Event_Info_InterchangeStart ThenEnd Sub
TextBox1.Text = TextBox1.Text & "Start of Interchange. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLfElseIf EventNotify.Severity = EventIDConstants.Event_Info_InterchangeEnd Then
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "End of Interchange. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLfElseIf EventNotify.Severity = EventIDConstants.Event_Info_FunctionalGroupStart Then
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Start of Functional Group. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLfElseIf EventNotify.Severity = EventIDConstants.Event_Info_FunctionalGroupEnd Then
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "End of Functional Group. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLfElseIf EventNotify.Severity = EventIDConstants.Event_Info_TransactionSetStart Then
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Start of Transaction Set/Message. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLfElseIf EventNotify.Severity = EventIDConstants.Event_Info_TransactionSetEnd Then
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "End of Transaction Set/Message. Notification: " & CStr(EventNotify.ErrorDescription) & vbCrLfElseIf EventNotify.Severity = EventIDConstants.Event_Notify Then
Application.DoEvents()
' Any general notifications sent by FREDI is captured here.ElseIf EventNotify.Severity = EventIDConstants.Event_Warning Then
TextBox1.Text = TextBox1.Text & "Code: " & CStr(EventNotify.ErrorCode) & ", Notification: " & EventNotify.ErrorDescription & vbCrLf
Application.DoEvents()
' Any warnings sent by FREDI is captured here.ElseIf EventNotify.Severity = EventIDConstants.Event_Severe Then
TextBox1.Text = TextBox1.Text & "Warning Code: " & CStr(EventNotify.ErrorCode) & ", Warning Description: " & EventNotify.ErrorDescription & vbCrLf
Application.DoEvents()
' Any severe errors sent by FREDI is captured here.End If
TextBox1.Text = TextBox1.Text & "Severe Code: " & CStr(EventNotify.ErrorCode) & ", Severe Description: " & EventNotify.ErrorDescription & vbCrLf
Application.DoEvents()
Select the EventNotifyInfo event and the m_oEdiDoc_EventNotifyInfo event procedure is automatically added to the form. Add the following code to display the events in the text control Text1.
Private Sub m_oEdiDoc_EventNotifyInfo(ByVal EventNotifyInfo As Edidev.FrameworkEDI.ediEventNotifyInfo) Handles m_oEdiDoc.EventNotifyInfo
If EventNotifyInfo.EventInfoType = EventIDConstants.Event_Info_Error_Segment_Handle ThenEnd Sub
' Supplemental error has been passed. In this case, the errorElseIf EventNotifyInfo.EventInfoType = EventIDConstants.Event_Info_Error_Element_Position Then
' generated has happened in the following segment handle.
TextBox1.Text = TextBox1.Text & "Segment Handle: " & CStr(EventNotifyInfo.NumericInfo) & vbCrLf
' The following error code ties this supplemental error to an
' error generated by EventNotify
TextBox1.Text = TextBox1.Text & "Error Code: " & EventNotifyInfo.StringInfo & vbCrLf
Application.DoEvents()
' Supplemental error has been passed. In this case, the errorElseIf EventNotifyInfo.EventInfoType = EventIDConstants.Event_Info_Error_Composite_Position Then
' generated has happened in the following data element in
' a segment having the following data segment handle.
TextBox1.Text = TextBox1.Text & "Segment Handle: " & CStr(EventNotifyInfo.NumericInfo) & vbCrLf
' Data element position in error.
TextBox1.Text = TextBox1.Text & "Element Position: " & EventNotifyInfo.StringInfo & vbCrLf
Application.DoEvents()
' Supplemental error has been passed. In this case, the errorEnd If
' generated has happened in a sub element belong to a composite
' of a segment having the following segment handle.
TextBox1.Text = TextBox1.Text & "Segment Handle: " & CStr(EventNotifyInfo.NumericInfo) & vbCrLf
' Data element and sub element position in error.
TextBox1.Text = TextBox1.Text & "Component Element Position: " & EventNotifyInfo.StringInfo & vbCrLf
Application.DoEvents()
Select the DllNotifyInfo event and the m_oEdiDoc_DllNotifyInfo event procedure is automatically added to the form. Add the following code to display the events in the text control Text1.
Private Sub m_oEdiDoc_DllNotifyInfo(ByVal DllNotify As Edidev.FrameworkEDI.ediDllNotifyInfo) Handles m_oEdiDoc.DllNotifyInfo
' DLL notifications are captured here.End Sub
TextBox1.Text = TextBox1.Text & "DLL Notify. DLL File Name: " & DllNotify.DllFileName & vbCrLf
TextBox1.Text = TextBox1.Text & "DLL Notify. Handle: " & CStr(DllNotify.Handle) & vbCrLf
TextBox1.Text = TextBox1.Text & "DLL Notify. String Parameter: " & DllNotify.StringParam & vbCrLf
TextBox1.Text = TextBox1.Text & "DLL Notify. Numeric Parameter: " & CStr(DllNotify.NumParam) & vbCrLf
Application.DoEvents()
Select the ProgressNotify event and the m_oEdiDoc_ProgressNotify event procedure is automatically added to the form. Add the following code to display the events in the text control Text1.
Private Sub m_oEdiDoc_ProgressNotify(ByVal ProgressNotify As Edidev.FrameworkEDI.ediProgressNotify) Handles m_oEdiDoc.ProgressNotify
If ProgressNotify.ProgressState = ProgressStateConstants.Progress_Phase_Start ThenEnd Sub
Select Case ProgressNotify.ProgressTypeElseIf ProgressNotify.ProgressState = ProgressStateConstants.Progress_Phase_Update Then
Case ProgressTypeConstants.Progress_Type_Single_ImportEnd Select
TextBox1.Text = TextBox1.Text & "Progress Start. Importing single SEF" & vbCrLfCase ProgressTypeConstants.Progress_Type_Codes_Read
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Start. Reading SEF Codes" & vbCrLfCase ProgressTypeConstants.Progress_Type_Document_Read
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Start. Reading EDI Document" & vbCrLfCase ProgressTypeConstants.Progress_Type_Document_Save
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Start. Saving EDI Document" & vbCrLf
Application.DoEvents()
Select Case ProgressNotify.ProgressTypeElseIf ProgressNotify.ProgressState = ProgressStateConstants.Progress_Phase_Complete Then
Case ProgressTypeConstants.Progress_Type_Single_ImportEnd Select
TextBox1.Text = TextBox1.Text & "Progress Update. Importing single SEF at %" & CStr(ProgressNotify.Percent) & vbCrLfCase ProgressTypeConstants.Progress_Type_Codes_Read
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Update. Reading SEF Codes at %" & CStr(ProgressNotify.Percent) & vbCrLfCase ProgressTypeConstants.Progress_Type_Document_Read
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Update. Reading EDI Document at %" & CStr(ProgressNotify.Percent) & vbCrLfCase ProgressTypeConstants.Progress_Type_Document_Save
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Update. Saving EDI Document at %" & CStr(ProgressNotify.Percent) & vbCrLf
Application.DoEvents()
Select Case ProgressNotify.ProgressTypeEnd If
Case ProgressTypeConstants.Progress_Type_Single_ImportEnd Select
TextBox1.Text = TextBox1.Text & "Progress Completed. Importing single SEF" & vbCrLfCase ProgressTypeConstants.Progress_Type_Codes_Read
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Completed. Reading SEF Codes" & vbCrLfCase ProgressTypeConstants.Progress_Type_Document_Read
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Completed. Reading EDI Document" & vbCrLfCase ProgressTypeConstants.Progress_Type_Document_Save
Application.DoEvents()
TextBox1.Text = TextBox1.Text & "Progress Completed. Saving EDI Document" & vbCrLf
Application.DoEvents()