⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.frm

📁 VB开发OPC Client的教程和源码
💻 FRM
📖 第 1 页 / 共 5 页
字号:
                ' selected group.  If its not then we need to update the
                ' lvListView with a new list of items if they exist.
                If (Not NewGroupSelection Is Module1.SelectedOPCGroup) Then
                    ' A new group has been selected so we need to dump the current
                    ' list view and load a new one.
                    Set Module1.SelectedOPCGroup = NewGroupSelection
                    ' Dump the current listview contents and try to load
                    ' the items from the newly selected group if any.
                    GetNewItemList
                End If
                          
                ' Enable/Disable those option for a group selection
                mnuTreeViewNewServer.Visible = False ' Disable New Server
                mnuTreeViewNewServer.Enabled = False
                mnuTreeViewNewGroup.Visible = False     'Disable New Group
                mnuTreeViewNewGroup.Enabled = False
                mnuTreeViewDelete.Visible = True        ' Enable delete of group
                mnuTreeViewDelete.Enabled = True
                mnuTreeViewProperties.Visible = True    ' Enable view of group properties
                mnuTreeViewProperties.Enabled = True
                mnuTreeViewNewItem.Visible = True       ' Enable add item.
                mnuTreeViewNewItem.Enabled = True
            End If
        Else ' If no node was selected but the user right clicked then
             ' display the popup with only the "New Server Connection" enabled
            mnuTreeViewNewGroup.Visible = False
            mnuTreeViewNewGroup.Enabled = False
            mnuTreeViewDelete.Visible = False
            mnuTreeViewDelete.Enabled = False
            mnuTreeViewProperties.Visible = False
            mnuTreeViewProperties.Enabled = False
            mnuTreeViewNewItem.Visible = False
            mnuTreeViewNewItem.Enabled = False
        End If
        
        ' Show the TreeView popup.
        frmMain.PopupMenu mnuTreeView
        
    ' The left button is used to select and OPC server connection
    ' or group only no popup is displayed.
    ElseIf (Button = vbLeftButton) Then
        If Not SelectedNode Is Nothing Then
            If InStr(SelectedNode.Key, "Server") Then
                ' Get the selected OPCServerClass object using the
                ' SelectedNode.Key which is the same as the server key
                ' in the OPCServers collection.
                With OPCServers
                    Set Module1.SelectedOPCServer = .Item(SelectedNode.Key)
                End With
                ' When a server is selected we set the Module1.SelectedOPCGroup
                ' to nothing and clear the lvListView control.
                Set Module1.SelectedOPCGroup = Nothing
                lvListView.ListItems.Clear
            ElseIf InStr(SelectedNode.Key, "Group") Then
                ' Get a reference to the parent of group node.
                Set NodParent = SelectedNode.Parent
                ' Set the Selected Server to this parent
                With OPCServers
                    Set Module1.SelectedOPCServer = .Item(NodParent.Key)
                End With
                
                ' See explanation in the right click portion above.
                Set OPCServerGroupsCls = Module1.SelectedOPCServer.GetOPCServerGroupCollection
                Set NewGroupSelection = OPCServerGroupsCls.Item(SelectedNode.Key)
                If (Not NewGroupSelection Is Module1.SelectedOPCGroup) Then
                    ' A new group has been selected so we need to dump the current
                    ' list view and load a new one.
                    Set Module1.SelectedOPCGroup = NewGroupSelection
                    GetNewItemList
                End If
            End If
        End If
    
    End If
End Sub


' This sub is invoked from the mnuTreeView popup context menu
' and simply displays the frmSelectOPCServer form.
'
Private Sub mnuTreeViewNewServer_Click()
    Load frmSelectOPCServer
    frmSelectOPCServer.Show
    ' Prevent the tree view from being selected while the server
    ' selection 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 is invoked from the mnuTreeView popup context menu
' and simply displays the frmAddGroup form.
'
Private Sub mnuTreeViewNewGroup_Click()
    Load frmAddGroup
    frmAddGroup.Show
    ' Prevent the tree view from being selected while the server
    '  selection 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 is invoked from the mnuTreeView popup context menu
' and simply displays the frmAddItem form. This is only
' available when a group is selected from the tvTreeView control.
' Since this is the sane as the ListView action I use that sub.
'
Private Sub mnuTreeViewNewItem_Click()
    ' Simply call the list view handler here to add a new item to the
    ' seleted group.
    mnuListViewNewItem_Click
End Sub


' This sub is invoked from the mnuTreeView popup context menu
' and simply displays either the Server properties form or the
' Group properties form.
'
Private Sub mnuTreeViewProperties_Click()
    ' In this case we know that the only way this menu could
    ' have been invoked is when a treeview node has been selected
    ' so we can simply test the tvTreeView.SelectedItem.Key for
    ' either the "Server" portion or "Group" portion of the key.
    If InStr(tvTreeView.SelectedItem.Key, "Server") Then
        Load frmOPCServerProperties
        frmOPCServerProperties.Show
    ElseIf InStr(tvTreeView.SelectedItem.Key, "Group") Then
        Load frmOPCGroupProperties
        frmOPCGroupProperties.Show
    End If
    ' Prevent the tree view from being selected while the properties
    ' 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


' Up to this point the subs called from the mnuTreeView popup menu have simply
' invoked other VB forms.  The delete menu selection has a direct effect
' upon the state of you OPC data.  The mnuTreeViewDelete_Click will either
' delete the selected OPCServerClass object from the OPCServers
' collection and treeview or delete the selected OPCGroupClass from the
' selected OPCServerClass object.  In both cases the TreeView and ListView
' will also be updated accordingly.  This must be done to keep the node keys
' in sync with the OPC server and group object keys.
'
Private Sub mnuTreeViewDelete_Click()
    On Error GoTo SkipDelete
    ' Is the selected node in the treeview a server object?
    If InStr(tvTreeView.SelectedItem.Key, "Server") Then
        ' When removing the entire server from the system
        ' we first need to remove the groups that are part
        ' of that server.  We can do this by using the
        ' OPCServerGroups collection that is part of the OPCServerClass object
        ' The Groups are removed in reverse order as the collection
        ' shrinks this is important since the size of the collection goes down
        ' and the numerical indexes are adjust as you delete objects from
        ' the collection.
        
        Dim OPCServerGroupCls As Collection
        Set OPCServerGroupCls = Module1.SelectedOPCServer.GetOPCServerGroupCollection
        
        ' If there are groups in this server remove them from the OPCServerClass
        ' object and from the treeview.
        If OPCServerGroupCls.Count <> 0 Then
            Dim i As Integer
            Dim a As Integer
            Dim GroupKeyToRemove As String
            a = OPCServerGroupCls.Count ' intialize the count used when removing the objects in reverse order.
            For i = 1 To OPCServerGroupCls.Count
                Set Module1.SelectedOPCGroup = OPCServerGroupCls.Item(a)
                ' Get the group object key as a string
                GroupKeyToRemove = Module1.SelectedOPCGroup.GetGroupKey
                ' Remove the object from the OPCServerClass object's
                ' collection of group objects.
                Module1.SelectedOPCServer.RemoveOPCGroup (GroupKeyToRemove)
                tvTreeView.Nodes.Remove (GroupKeyToRemove)
                Set Module1.SelectedOPCGroup = Nothing
                a = a - 1
            Next i
        End If
        
        ' Clear the list view for this group
        lvListView.ListItems.Clear
                
        ' Now that all the groups have been removed from the OPCServerClass
        ' object we can disconnect the server from our application.  This
        ' is done by calling the DisconnectOPCServer function of the
        ' OPCServerClass object.
        Module1.SelectedOPCServer.DisconnectOPCServer
        
        ' Now we remove the OPCServerClass object itself from the
        ' the OPCServers collection.
        With OPCServers
            .Remove (tvTreeView.SelectedItem.Key)
        End With
        ' Finally we remove the server connection from the treeview.
        tvTreeView.Nodes.Remove (tvTreeView.SelectedItem.Key)
        ' clear the SelelctedOPCServer pointer.
        Set Module1.SelectedOPCServer = Nothing
     
    ' Removing the group object from a server object is a little easier
    ' since we don't need to do any looips to remove a list of groups
    ' from the OPCServerClass object or treeview.
    
    ElseIf InStr(tvTreeView.SelectedItem.Key, "Group") Then
        Dim Result As Boolean
        ' Atempt to remove the selected OPCGroupClass object from the
        ' selected OPCServerClass object by calling the RemoveOPCGroup method
        ' of the OPCServerClass object.
        Result = Module1.SelectedOPCServer.RemoveOPCGroup(tvTreeView.SelectedItem.Key)
        
        ' If the remove was sucessful remove the group from the treeview
        ' and clear the listview control.
        If Result = True Then
            Set Module1.SelectedOPCGroup = Nothing
            tvTreeView.Nodes.Remove (tvTreeView.SelectedItem.Key)
            lvListView.ListItems.Clear
        End If
    End If
SkipDelete:
End Sub


' The GetNewItemList sub handles loading a new list view with OPC
' items from the selected OPCGroupClass object.  This occurs
' whenever the user selects a new group from the treeview.
'
Private Sub GetNewItemList()
    On Error Resume Next
    ' First clear the existing list view
    lvListView.ListItems.Clear
    LastTopItem = -1
    
    Dim OPCItemData As OPCItemClass
    Dim OPCGroupItemsCls As Collection
    ' Get pointer to the collection of items in the OPCGroupClass object.
    Set OPCGroupItemsCls = Module1.SelectedOPCGroup.GetOPCGroupItemsCollection
    
    ' If there are no items exit
    If OPCGroupItemsCls.Count = 0 Then
        GoTo SkipListUpdate
    End If
     
    Dim i As Integer
    
    For i = 1 To OPCGroupItemsCls.Count
        ' Normally we use the "Item Key" as string to specify the item we want
        ' from the collection but in this case we don't know the item keys
        ' explicitly sense some items may have been deleted.  To
        ' address this we can use the item index as a numeric index
        ' then pull the item key from the OPCItemData and use this as
        ' our list view key.  This demonstrates how both the string key
        '  and the numeric key of a collection can be used to address
        ' what otherwise would be a real key management issue.
        
        ' Get an OPCItemClass object from the items collection
        Set OPCItemData = OPCGroupItemsCls.Item(i)
        
        Dim ItemKey As String
        Dim itmX As ListItem
        Dim Quality As Long
        Dim ItemValue As Variant
        
        ' Build a unique strin key for the list view item using the actual
        ' item index number.
        ItemKey = "Item" + Str(OPCItemData.GetItemIndex)
        ' Add Node objects.
        Set itmX = lvListView.ListItems.Add(, ItemKey, OPCItemData.GetItemID)
            ItemValue = OPCItemData.GetItemValue(OPCItemDirect)
            ' When not processing an array value then simply display
            ' the value as it is in variant form.
            If Not IsArray(ItemValue) Then
                itmX.SubItems(1) = ItemValue
            Else
                ' If we have an array then we need to format the display
                ' by converting the value to a string with each value
                ' separated by commas.
                Dim b As Integer
                itmX.SubItems(1) = ""
                ' Regardless of the Option Base setting arrays returned

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -