📄 frmadditem.frm
字号:
' Simle place holder for the connected server key string since it is
' used in number of places in the browser code.
Dim ServerKey As String
' On form load we initialize the treeview control and listview controls
' to allow the user to expand the treeview by one level below the
' actual server name.
Private Sub Form_Load()
Dim ServerName As String
' Get the OPCBrowserClass object that will be used during this
' instance of the AddItem dialog.
'
Module1.SelectedOPCServer.GetServerBrowseObject OPCBrowserClassObj
' Get the Server Name that will act as the base item in the
' treeview control. This first branch of the treeview is always displayed.
'
Module1.SelectedOPCServer.GetServerName ServerName
' Get the server key used to determine when the branch selected is the root
' ServerName branch and also to be used to build unique branch keys
' in the tree view as the browse space is expanded.
'
ServerKey = Module1.SelectedOPCServer.GetOPCServerKey
' Intiailize the var used to create the dummy nodes that allow the
' treeview to always have an expansion available for every branch.
'
DummyNodeNum = 1
' Set the list view column headers.
lvItemView.ColumnHeaders.Add , , "ItemID", lvItemView.Width
lvItemView.View = lvwReport ' Place list view text report mode
' If the server supports browsing initialize the
' treeview and listview controls.
If Not OPCBrowserClassObj Is Nothing Then
' Add Node objects.
Dim nodX As Node ' Declare Node variable.
' Add the new server as a root in the tree view
Set nodX = tvBranchView.Nodes.Add(, , ServerKey, ServerName)
nodX.EnsureVisible
Dim Organization As Long
OPCBrowserClassObj.GetBrowserOrganization Organization
' If the server is a flat browse space then populate the list view
' now since there won't be any other branches.
If Organization = 2 Then
OPCBrowserClassObj.ShowLeafs False
Dim i As Integer
For i = 1 To OPCBrowserClassObj.GetItemCount
Dim ItemName As String
OPCBrowserClassObj.GetItemName ItemName, i
lvItemView.ListItems.Add i, i, ItemName
Next i
Else
' Add a dummy node that will allow the first branch to
' be expanded. We use the phrase "DummyNode" plus the DummyNodeNum
' to generate a dummy node that will always be unique. The
' use the phrase "DummyNode" is crucial since this string is
' used to identify the dummy node so it can be removed as the
' branch is expanded. When you look at the treeview expand method
' you will see the dummy node is removed since it is known to be
' the first and only child when a branch is collapsed. The
' dummy node is placed back into the treeview at the specific
' level when the branch is collapse after all other children
' for that brach have been removed. In this way the treeview
' is always shown with little expansion + signs.
'
tvBranchView.Nodes.Add ServerKey, tvwChild, "DummyNode" + Str(DummyNodeNum), ""
End If
End If
DummyNodeNum = DummyNodeNum + 1
End Sub
Private Sub Form_Unload(Cancel As Integer)
' Free the memory of the OPCBrowserClass object.
Set OPCBrowserClassObj = Nothing
End Sub
' This first section deals with the Item Definition portion of the
' dialog.
' When the user clicks the Add Item button the ItemID,
' DataType, and Active state of teh item will be passed to
' the AddOPCItem function in the frmMain form. With the browser
' you can add multiple tags to the group by duble clicking on the
' desired tag in the browsers list view and then clicking the Add
' button.
'
Private Sub AddItem_Click()
Dim DataTypeSelected As Integer
' Make sure the user makes a specific selection for the
' data type
If DataType.ListIndex = -1 Then
DataTypeSelected = 0
Else
DataTypeSelected = DataType.ItemData(DataType.ListIndex)
End If
' Call the AddOPCItem function of the frmMain and see if
' this new item can be added to the OPC group. The actual
' call to the AddOPCItem of the OPCGroupClass can be seen in
' this frmMain form function. Normally you would be calling
' the AddOPCItem function of the OPCGroupClass directly.
If fMainForm.AddOPCItemMain(ItemID.Text, DataTypeSelected, ActiveState.Value) = False Then
MsgBox "The OPC Server has rejected the item please check the Item ID and data type selections"
End If
End Sub
' The Deactivate event is used here as a cheap way
' of keeping the form on top of the application if
' the user clicks on the main window. There may be
' other ways to do this but this works fairly well.
'
Private Sub Form_Deactivate()
' use the show memthod to force the form
' back to the front
frmAddItem.Show
End Sub
' This simply clears the Report.Text string when a
' change to this control occurs. This is done to allow
' the validate to show a new report string.
'
Private Sub ItemID_Change()
Report.Text = ""
End Sub
' This is just a cute way of clearing the contents
' the Report.Text control so we can provide a new
' validate response.
'
Private Sub Validate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
' Let the user know we are about to test the item.
Report.Text = "Testing Item"
End If
End Sub
' This event handler will call the actual ValidateOPCItem
' function found in the OPCGroupClass object. The
' instance of the OPCGroupClass used is contained in the
' variable SelectedOPCGroup of Module1. In this example
' you can count on that variable being set at this point.
' In general it's very important to make sure that you
' use the appropriate OPCGroupClass object when attempting
' to either Validate or Add an OPC item to the server. If you
' use the wrong instance of the OPCGroupClass object the
' Validate or Add functions may fail or worse you'll add an
' item to the wrong group.
'
Private Sub Validate_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim DataTypeSelected As Integer
Dim Error As Long
If Button = vbLeftButton Then
' Make sure the user makes a specific selection for the
' data type
If DataType.ListIndex = -1 Then
DataTypeSelected = 0
Else
DataTypeSelected = DataType.ItemData(DataType.ListIndex)
End If
' Although the Module1.SelectedOPCGroup is set at this
' point it's always good practice test it before we use it.
If Not Module1.SelectedOPCGroup Is Nothing Then
' Call the ValidateOPCItem function of the OPCGroupClass
If Module1.SelectedOPCGroup.ValidateOPCItem(ItemID.Text, DataTypeSelected, ActiveState.Value, Error) = False Then
' The OPC Server didn't like something about the
' ItemID, DataType or Active state.
Report.Text = "Error - Check ItemID or Datatype"
Else
' Item has been accepted as given.
Report.Text = "ItemID and Datatype Accepted"
End If
End If
End If
End Sub
' Simple cancel of the AddItem form
'
Private Sub Abort_Click()
' Reenable the tree view
fMainForm.tvTreeView.Enabled = True
Unload Me
End Sub
' This simply clears the Report.Text string when a
' change to this control occurs. This is done to allow
' the validate to show a new report string.
'
Private Sub ActiveState_Click()
Report.Text = ""
End Sub
' The Expand operation will attempt to display any branches that
' the server may contain at the current position of the expansion.
' The current position is established each time a branch of the
' treeview is expanded. The treeview can always be expand due to
' the dummy node that is added to the control at each new branch.
' This cna be seen in the Collapse event.
'
Private Sub tvBranchView_Expand(ByVal Node As Node)
' Make sure the server support browsing.
If Not OPCBrowserClassObj Is Nothing Then
Dim i As Integer
Dim ItemName As String
Dim DataTypeSelected As Integer
Dim AccessSelected As Long
' Remove the dummy node that we know will always exist
' when a branch is expanded.
If Node.Children Then
' Make sure that the child node is the dummy node and
' remove it.
If InStr(Node.Child.Key, "DummyNode") Then
tvBranchView.Nodes.Remove (Node.Child.Key)
End If
End If
' The node key is built using the name of each branch
' of the actual server browse space. This makes moving to
' a particular branch easy. The node key is built below in
' this function.
'
SetBrowsePosition Node.Key
' Make sure that the Name filter is set to empty
' before we call to get any branches.
OPCBrowserClassObj.SetFilter ""
' Tell the OPC Server to load it's internal item collection
' with Branch items.
OPCBrowserClassObj.ShowBranches
' Now add each new branch if any under the current branch.
' Also add a dummy node to these new branches so they have
' an expansion available as well.
For i = 1 To OPCBrowserClassObj.GetItemCount
DummyNodeNum = DummyNodeNum + 1
' Get a branch name that exist under the current position of the
' server.
OPCBrowserClassObj.GetItemName ItemName, i
' Load this name as a child to the current node of our
' treeview control. As you can see the node key of the new child
' is built using the current node key of the parent + the
' ASCII character CHR(2) + the new itme name. In this way a
' nodes key will contain the full path to an item as
' you browse down the tree.
' A node key might appear as
' "Server 0\02Channel_1\02Device_1" In this case the \02 signifies the
' CHR(2) character. I use the CHR(2) character as a delimiter
' between each branch name. This in turn is used in the SetBrowsePosition
' function to find each of these branch names and move down the browse tree.
' I use the CHR(2) simply because it is a non printable character
' that in all likely hood will not appear in the name of a branch
' in a server. The Server Key is applied to the first branch
' in the treeview and is done in the Form Load.
'
tvBranchView.Nodes.Add Node.Key, tvwChild, Node.Key + Chr(2) + ItemName, ItemName
' Add a dummy node to these new child branches that will allow
' them to be expanded when the user clicks on them. In this case
' the relative parameter is the node key of the child we jsut added
' to the parent branch.
tvBranchView.Nodes.Add Node.Key + Chr(2) + ItemName, tvwChild, "DummyNode" + Str(DummyNodeNum), ""
Next i
End If
End Sub
' The SetBrowsePosition is my little short cut function that
' takes a node.key string and parses out the various branches of the node
' and then performs a series of MoveDown operations to get to the
' desired branch level. The function always starts at the root of
' the server before it moves down so getting out of sync with the server
' should be hard to do in practice.
'
Private Sub SetBrowsePosition(ByVal Position As String)
Dim NormalPosition As String
Dim NextBranch As Integer
Dim LastBranch As Integer
Dim FindBranch As String
' Init the indexes used to parse the Node.Key string.
NextBranch = 1
LastBranch = 1
' Establish the character that will be used to delimit each
' branch string within the Node.Key.
FindBranch = Chr(2)
' Start at the roo of the server's browse space. This helps to keep
' this application in sync with the server's browse tree and position.
OPCBrowserClassObj.MoveToRoot
' See is the key is just the server root name in which case we do nothing
' since we are already at the root.
If Len(Position) > Len(ServerKey) Then
' First step remove the Server Key from the first part of the Node.Key
NormalPosition = Mid(Position, (Len(ServerKey) + 2))
' Now loop through each branch name in the NormalPositon string
' and MoveDown to that new branch as we go.
Do While (NextBranch < Len(NormalPosition) And NextBranch <> 0)
NextBranch = InStr((NextBranch + 1), NormalPosition, FindBranch)
If NextBranch <> 0 Then
OPCBrowserClassObj.MoveDown (Mid(NormalPosition, LastBranch, NextBranch - LastBranch))
LastBranch = NextBranch + 1
Else
If LastBranch = 1 Then
OPCBrowserClassObj.MoveDown (NormalPosition)
Else
OPCBrowserClassObj.MoveDown (Mid(NormalPosition, LastBranch))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -