View active alarms in user defined ListView
The following example shows how to create a user defined List View in Visual Basic for the TcEventLogger, that shows active events.
Do following steps to create the sample:
- Create a standard EXE project with a form.
- Add MS Standard Controls 6 to the Control list.
- Add the Beckhoff TcEventLogger Library to the references.
- Add the List View ActiveX Control to the form.
- Rename the List View to lvwAlarm
- Copy following Code to the form:
Option Explicit
Dim WithEvents objEventLogger As TcEventLog
'------------------------------------------------------
' this method is called when the form is loaded
'
Private Sub Form_Load()
Set objEventLogger = New TcEventLog
Call initListView
Call DisplayActiveAlarms
End Sub
'------------------------------------------------------
' this method is called when a new alarm is issued
'
Private Sub objEventLogger_OnNewEvent(ByVal evtObj As Object)
' cast to TcEvent interface
Dim objEvent As TcEvent
Set objEvent = evtObj
' get the key
Dim newKey As String
newKey = GetKeyFromIDs(objEvent.SrcId, objEvent.Id)
'get the Event Message Text
Dim strMessage As String
strMessage = objEvent.GetMsgString(1033)
'add the Message to the listview
Dim objListItem As ListItem
Set objListItem = lvwAlarm.ListItems.Add(, newKey, strMessage & objEvent.Date)
End Sub
'------------------------------------------------------
' this method is called when a alarm is reset
'
Private Sub objEventLogger_OnResetEvent(ByVal evtObj As Object)
' cast to TcEvent interface
Dim objEvent As TcEvent
Set objEvent = evtObj
' get the key
Dim newKey As String
newKey = GetKeyFromIDs(objEvent.SrcId, objEvent.Id)
'remove the event from the listview
Call lvwAlarm.ListItems.Remove(newKey)
End Sub
'------------------------------------------------------
' initalize the list view
'
Sub initListView()
lvwAlarm.View = lvwReport
lvwAlarm.GridLines = True
'init the Header
Dim objColHdr As ColumnHeaders
Set objColHdr = lvwAlarm.ColumnHeaders
Debug.Assert Not objColHdr Is Nothing
Call objColHdr.Clear
Call objColHdr.Add(1, "Message", "Message", 5000)
End Sub
'------------------------------------------------------
' display all active alarms
'
Private Sub DisplayActiveAlarms()
'clear the listview
Call lvwAlarm.ListItems.Clear
Debug.Assert Not objEventLogger Is Nothing
Dim objEvent As TcEvent
'get the number of active Events
For Each objEvent In objEventLogger.EnumActiveEventsEx
If (Not objEvent Is Nothing) Then
'here we get one Event from the collection
Dim strMessage As String
'get the Event Message Text
strMessage = objEvent.GetMsgString(1033)
'get the key
Dim newKey As String
newKey = GetKeyFromIDs(objEvent.SrcId, objEvent.Id)
'add the Message to the listview
Dim objListItem As ListItem
Set objListItem = lvwAlarm.ListItems.Add(, newKey, strMessage & objEvent.Date)
End If
Next
End Sub
'------------------------------------------------------
' create unique key from event group and event id
'
Public Function GetKeyFromIDs(ByVal SourceId As Long, ByVal EventId As Long) As String
GetKeyFromIDs = SourceId & "-" & EventId
End Function
The List View has a column with the Event message. The Method DisplayActiveAlarms gets the active alarms from the EventLogger and shows them. When a new Event is shown or a actual event is reset the OnNewEvent event-function adds events to the List View and the OnResetEvent event-function removes events from the List View. The method GetKeyFromIDs created a key of the event group and the event id.
The user defined Event View only supports events that are not quit-requiring. To use it for quit-requiring events you have to add functions for ITcEventLogEvents::OnSignalEvent and ITcEventLogEvents::OnConfirmEvent.
Language / IDE |
Source code |
---|---|
Visual Basic 6.0 |