📄 frmmain.frm
字号:
' from an OPC Server have a starting index of 0
For b = 0 To UBound(ItemValue)
itmX.SubItems(1) = itmX.SubItems(1) + Str(ItemValue(b)) + ", "
Next b
End If
' Get the item quailty condition directly from the OPC server
Quality = OPCItemData.GetItemQuality(OPCItemDirect)
' The "Data Access Automation Interface Standard" specification
' doesn't directly tell you the contents of the quailty field but
' in general if the field contains the value 192 of 0xC0 Hex the
' data can be considered OK.
If Quality And &HC0 Then
itmX.SubItems(2) = "Good" ' if quailty is 192 then OK
Else
itmX.SubItems(2) = "Bad " + Str(Quality) ' If the not 192 show Bad and value.
End If
Next i
SkipListUpdate:
End Sub
' Like the MouseUp handler for the TreeView, this sub handles all
' user interaction with the ListView control. If the user right clicks
' in the lsit view they will be presented with a context popup. If the
' user has an item selected when the right button is pressed they will
' be able to add a new item, make the selected item active or inactive,
' write to the item, or delete it. If the user right click without selecting
' an item they will be able to add a new item.
'
Private Sub lvListView_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
' As I mentioned above I use the enabled state of the tree view
' to determine if user input is allowed in the ListView. I do
' this since at each point a new form will be displayed I disable
' the TreeView control. This causes the treeview to become greyed out
' and prevents the user from invloking a popup from the tree view.
' If I uses the .enabled property of the ListView the whole view
' gets greyed out and looks pretty bad. By monitoring the
' .enabled state of the treeview I can disble the listview input while
' keeping the display clear.
If tvTreeView.Enabled = True Then
' First we need to make sure that a group is selected in the
' treeview control.
If Not tvTreeView.SelectedItem Is Nothing Then
If InStr(tvTreeView.SelectedItem.Key, "Group") Then
' A group is selected in the treeview if the right button
' is pressed show a popup.
If (Button = vbRightButton) Then
' The "New Item" selection is always allowed.
mnuListViewNewItem.Visible = True
mnuListViewNewItem.Enabled = True
' Is an item selected in the ListView
If Not lvListView.SelectedItem Is Nothing Then
Dim SelectedItem As ListItem
' We use the hit test here to make sure the user
' is directly on a specific item.
Set SelectedItem = lvListView.HitTest(X, Y)
If Not SelectedItem Is Nothing Then
Dim OPCGroupItemsCls As Collection
Set OPCGroupItemsCls = Module1.SelectedOPCGroup.GetOPCGroupItemsCollection
' Set the SelectedOPCItem pointer
Set Module1.SelectedOPCItem = OPCGroupItemsCls.Item(Mid(SelectedItem.Key, InStr(SelectedItem.Key, " ")))
' Display all Item options on the popup.
mnuListViewSetActive.Visible = True
mnuListViewSetActive.Enabled = True
mnuListViewSetInactive.Visible = True
mnuListViewSetInactive.Enabled = True
mnuListViewWriteValue.Visible = True
mnuListViewWriteValue.Enabled = True
mnuListViewDelete.Visible = True
mnuListViewDelete.Enabled = True
Else
' The user is not directly on an item so show only
' the "Add Item" selection.
mnuListViewSetActive.Visible = False
mnuListViewSetActive.Enabled = False
mnuListViewSetInactive.Visible = False
mnuListViewSetInactive.Enabled = False
mnuListViewWriteValue.Visible = False
mnuListViewWriteValue.Enabled = False
mnuListViewDelete.Visible = False
mnuListViewDelete.Enabled = False
End If
Else
' The user is not directly on an item so show only
' the "Add Item" selection.
mnuListViewSetActive.Visible = False
mnuListViewSetActive.Enabled = False
mnuListViewSetInactive.Visible = False
mnuListViewSetInactive.Enabled = False
mnuListViewWriteValue.Visible = False
mnuListViewWriteValue.Enabled = False
mnuListViewDelete.Visible = False
mnuListViewDelete.Enabled = False
End If
' Show the popup
frmMain.PopupMenu mnuListView
End If
End If
End If
End If
End Sub
' The mnuListViewSetActive_Click directly changes the active state of
' the selected OPC item. This is done by calling the .SetItemActiveState
' of the selected OPCItemClass object.
'
Private Sub mnuListViewSetActive_Click()
' Make sure an item is selected.
If Not Module1.SelectedOPCItem Is Nothing Then
' Change its active state to True. Normally you will want to call
' this function only once with out an associated call to make the item
' inactive. This however is not a requirement and you can call this
' function on an item as many times as you like. The next call
' with the state set to False will still turn the item off.
Module1.SelectedOPCItem.SetItemActiveState (True)
End If
End Sub
' The mnuListViewSetInactive_Click directly changes the active state of
' the selected OPC item. This is done by calling the .SetItemActiveState
' of the selected OPCItemClass object.
'
Private Sub mnuListViewSetInactive_Click()
' Make sure an item is selected.
If Not Module1.SelectedOPCItem Is Nothing Then
' Change its active state to False. Normally you will want to call
' this function only once with out an associated call to make the item
' active. This however is not a requirement and you can call this
' function on an item as many times as you like. The next call
' with the state set to True will still turn the item on.
Module1.SelectedOPCItem.SetItemActiveState (False)
End If
End Sub
' This sub simply displays the frmWriteItem form.
'
Private Sub mnuListViewWriteValue_Click()
'Module1.SelectedOPCItem will be set if this event occurs.
Load frmWriteItem
frmWriteItem.Show
' Prevent the tree view from being selected while the write item
' menu is displayed
'
' You'll see that I check this property in the lvListView_MouseUp
' as a simple way of preventing user input in the listview without
' using the .Enabled property of the ListView which cause the
' whole thing to grey out.
tvTreeView.Enabled = False
End Sub
' This sub directly removes an OPCIemClass object from the selected
' OPCGroupClass object.
'
Private Sub mnuListViewDelete_Click()
On Error GoTo SkipItemDelete
Dim Result As Boolean
Dim Error As Long
' Attempt to remove the selected OPCItemClass object from the
' selected OPCGroupClass object by passing in the item key to the
' .RemoveOPCItem function of the class.
Result = Module1.SelectedOPCGroup.RemoveOPCItem(lvListView.SelectedItem.Key, Error)
' If teh item was removed then remove it from the list view.
If Result = True Then
Set Module1.SelectedOPCItem = Nothing
lvListView.ListItems.Remove (lvListView.SelectedItem.Key)
End If
SkipItemDelete:
End Sub
' This sub is invoked from the mnuListView popup context menu
' and simply displays the frmAddItem form.
'
Private Sub mnuListViewNewItem_Click()
Load frmAddItem
frmAddItem.Show
' Prevent the tree view from being selected while the Add item
' menu is displayed
'
' You'll see that I check this property in the lvListView_MouseUp
' as a simple way of preventing user input in the listview without
' using the .Enabled property of the ListView which cause the
' whole thing to grey out.
tvTreeView.Enabled = False
End Sub
' The timer routine is used to update the contents of the list view display
' based on the contents data in the OPCItemClass objects attached to the
' selected OPCGroupClass object. Normally we could allow the actual update
' of the contents of the OPCItemsClass object do the updating of the listview
' but the flashing would be really bad. So I chose to use a timer tick
' to update the display. It still flashes but not a badly. There are ways to
' prevent the flashing but they go beyond the scope of this example.
' The other little pain you'll notice is that you can't scroll the list
' unless you select an Item at the bottom of each page of the list. I was
' not able to find a way to remove the selection from the item so the list
' could be scrolled easily. When a listview item is selected the list view
' tries to get that value in the view.
'
Private Sub Timer1_Timer()
On Error GoTo SkipDisplayUpdate
Dim OPCGroupItemsCls As Collection
' Make sure a group is selected before any update occurs.
If InStr(tvTreeView.SelectedItem.Key, "Group") Then
Dim OPCGroupToUpdate As OPCGroupClass
Dim OPCItemData As OPCItemClass
Dim OPCServerGroupsCls As Collection
' Get the OPCItemClass object collection from the selected OPC group
Set OPCServerGroupsCls = Module1.SelectedOPCServer.GetOPCServerGroupCollection
Set OPCGroupToUpdate = OPCServerGroupsCls.Item(tvTreeView.SelectedItem.Key)
Set OPCGroupItemsCls = OPCGroupToUpdate.GetOPCGroupItemsCollection
' If there aren't any items in the group skip the update
If OPCGroupItemsCls.Count = 0 Then
GoTo SkipDisplayUpdate
End If
' only update those items that are within the display area of the
' list view.
Dim itmX As ListItem
' Get the first Listitem in the ListView display
Set itmX = lvListView.GetFirstVisible
' This should contain a valid ListItem but just in case testing
' it is always a good idea.
If Not itmX Is Nothing Then
Dim NumLinesDisplayed As Integer
' This a hack, normally you would get the font metric for
' the system font, get the height and calculate the number
' of lines in the display area. I didn't want to do this
' at the time so here's a rough estimate of the value.
NumLinesDisplayed = (lvListView.Height / 214)
Dim i As Integer
Dim a As Integer
Dim GroupItemIndex As Integer
Dim OPCItemToUpdate As OPCItemClass
Dim Quality As Long
Dim ItemValue As Variant
a = itmX.Index
' See if the user is scrolling up or down and change the
' selected ListView item to allow the view to move beyond the
' current items in the view.
If LastTopItem <> a Then
If LastTopItem <> -1 Then
Set lvListView.SelectedItem = itmX
End If
LastTopItem = a
End If
For i = 1 To NumLinesDisplayed
' Get the ListItem key which is in the form "Item X"
' Since the OPCItemClass collection is key on a string
' value of just "X" we must remove the "Item" portion
' and conver thenumber back to a string.
GroupItemIndex = Val(Mid(itmX.Key, InStr(itmX.Key, " ")))
' Now that we have the key as a string we can get the
' OPCItemClass object associated with this listview item.
Set OPCItemToUpdate = OPCGroupItemsCls.Item(Str(GroupItemIndex))
' With the OPCItemClass object in hand we can update the list
' view item. For both the ItemValue and the Quality value
' I use the Const OPCItemLocal which forces the OPCItemClass
' functions to return their local copies of the data value and
' quality.
ItemValue = OPCItemToUpdate.GetItemValue(OPCItemLocal)
' If the ItemValue is not an array then simply use the Variant
' type to update the list box sub item 1 which the Value Heading.
If Not IsArray(ItemValue) Then
itmX.SubItems(1) = ItemValue
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -