📄 frmmain.frm
字号:
Else
' If it is an array we need to turn the array into a string
' that shows the values separated by commas.
Dim b As Integer
itmX.SubItems(1) = "" ' listview item string to start
' Regardless of the Option Base setting arrays returned
' from an OPC Server have a starting index of 0
For b = 0 To UBound(ItemValue)
' Build the array display by looping through each value and appending it with a comma.
itmX.SubItems(1) = itmX.SubItems(1) + Str(ItemValue(b)) + ", "
Next b
End If
' Get the item Quailty for this item.
Quality = OPCItemToUpdate.GetItemQuality(OPCItemLocal)
If Quality And &HC0 Then
itmX.SubItems(2) = "Good"
Else
itmX.SubItems(2) = "Bad" + Str(Quality)
End If
' Get the next ListView item using its numeric index number.
' If we exceed the number of item available in the list
' display the error handler will automatically to us to the
' SkipDisplayUpdate label
a = a + 1
Set itmX = lvListView.ListItems.Item(a)
Next i
End If
End If
SkipDisplayUpdate:
End Sub
' The AddSelectedOPCServerMain is called from the frmSelectOPCServer form.
' The function is housed here because of the dependecy it has on
' updating the TreeView control once a new server connection is added.
' This function also creates the actual working instance of the OPCServerClass
' object that will be used to contain your OPC groups and their associated
' items. If the server connection is successfully made with the server
' the OPCServers collection will be updated to contain the new OPCServerClass
' object. The TreeView will also be updated with this new server connection.
' Whether your OPC application has a user interface or is driver progrmatically,
' you will always start by making a connection to an OPC server first.
' Once the connection to the OPC server is made you can add groups and items
' to the connection and start putting OPC to use. In this example this
' function handle both the class objects and the user interface objects.
' In your application you may not need the user interface code.
'
Sub AddSelectedOPCServerMain(OPCServerName As String)
' Create a OPCServerClass object that we will attempt to connect to the server
' if the connection is sucessful we will add this new OPCServerClass object to
' OPCServers collection.
Dim OPCServer As OPCServerClass
Set OPCServer = New OPCServerClass
Dim Result As Boolean
' Create a unique key for this new server
Dim SrvName As String
SrvName = "Server" + Str(Module1.ServerIndex)
' Attempt a connection to the selected OPC Server
Result = OPCServer.ConnectOPCServer(OPCServerName, SrvName, Module1.ServerIndex)
' Prepare for next server connection.
Module1.ServerIndex = Module1.ServerIndex + 1
' If the connection was sucessful then add this new OPCServerClass
' object to the OPCServers collection and add this to the TreeView.
If (Result = True) Then
With OPCServers
.Add OPCServer, SrvName
End With
' Add Node objects.
Dim nodX As Node ' Declare Node variable.
' Add the new server as a root in the tree view
Set nodX = fMainForm.tvTreeView.Nodes.Add(, , SrvName, OPCServerName)
nodX.EnsureVisible
' Make this new server the selected server
Set Module1.SelectedOPCServer = OPCServer
' Clear any selected group or item
Set Module1.SelectedOPCGroup = Nothing
Set Module1.SelectedOPCItem = Nothing
' Remove all items from the list view if there are any.
lvListView.ListItems.Clear
End If
' If the connect fails the DisplayOPC_COM_ErrorValue inside of the OPCServerClass
' will display the error.
End Sub
' The AddOPCGroupMain function is called from the frmAddGroup form.
' The function is housed here because of the dependecy it has on
' updating the TreeView control once a new group object is added.
' Unlike the AddSelectedOPCServerMain function above this function does not
' actually create the OPCGroupClass object. That is done in the
' OPCServerClass object you now have. The OPCServerClass objects are handled
' directly by your application. After that the OPCGroupClass object and
' OPCItemClass object are handled by the class modules. You have access to the
' the collections within these object that house the groups or items but
' your application doesn't directly need to store these objects.
' You'll also notice that unlike the OPCServerClass object, we as the
' application don't determine the OPC group key instead the AddOPCGroup
' function of the OPCServerClass object returns a key to us. This key
' can then be used in the list view as a key. The key is unique for all
' server connection since it is built using the group index and the server
' index numbers. Example group keys are "Group 1 1" for a group on
' OPCServerClass object with a server index of 1, and "Group 1 2" on an
' OPCServerClas object with a server index of 2. In this example this
' function handle both the class objects and the user interface objects.
' In your application you may not need the user interface code.
'
Sub AddOPCGroupMain(ByVal GroupName As String, ByVal UpdateRate As Long, ByVal DeadBand As Single, ByVal ActiveState As Boolean)
Dim GroupKey As String
If Module1.SelectedOPCServer.AddOPCGroup(GroupName, UpdateRate, DeadBand, ActiveState, GroupKey) = True Then
' Add Node objects.
Dim nodX As Node ' Declare Node variable.
' Add the new server as a root in the tree view
Set nodX = fMainForm.tvTreeView.Nodes.Add(Module1.SelectedOPCServer.GetOPCServerKey, tvwChild, GroupKey, GroupName) ' + Str(Module1.SelectedOPCServer.GetOPCServerIndex)
nodX.EnsureVisible
End If
' If the add group fails the DisplayOPC_COM_ErrorValue inside of the OPCServerClass
' will display the error.
End Sub
' The AddOPCItemMain function is called from the frmAddItem Form.
' The function is housed here because of the dependecy it has on
' updating the ListView control once a new group object is added.
' Unlike the AddSelectedOPCServerMain function above this function does not
' actually create the OPCItemClass object. That is done in the
' OPCGroupClass object you now have. Once you have a group added to your
' OPCServerClass object you can add items to it by calling the
' OPCGroupClass.AddOPCItem function. The AddOPCItem function takes
' the ItemID, Datatype, and Active state as parameters. It also
' takes a ItemKey string to hold the returned ItemKey for use in the
' ListView control. In this example this
' function handle both the class objects and the user interface objects.
' In your application you may not need the user interface code.
'
Function AddOPCItemMain(ByVal ItemID As String, ByVal DataTypeSelected As Integer, ByVal ActiveState As Integer)
Dim ItemKey As String
' Make sure the SelectedOPCGroup pointer is valid
If Not Module1.SelectedOPCGroup Is Nothing Then
' Attempt to add an OPC item to the selected group.
If Module1.SelectedOPCGroup.AddOPCItem(ItemID, DataTypeSelected, ActiveState, ItemKey) = False Then
AddOPCItemMain = False
GoTo ErrorOnAdd
End If
End If
Dim itmX As ListItem
' Add the new item to the ListView's ListItem objects.
Set itmX = lvListView.ListItems.Add(, ItemKey, ItemID)
itmX.SubItems(1) = "" 'Initialize to no value
itmX.SubItems(2) = "Bad" ' Initialize to Bad quality
' Let the caller know the function worked.
AddOPCItemMain = True
ErrorOnAdd:
End Function
'*****************************************************************
' The remainder of this module was code generated by the VB wizard
'*****************************************************************
Private Sub mnuHelpAbout_Click()
frmAbout.Show vbModal, Me
End Sub
Private Sub mnuViewStatusBar_Click()
If mnuViewStatusBar.Checked Then
sbStatusBar.Visible = False
mnuViewStatusBar.Checked = False
Else
sbStatusBar.Visible = True
mnuViewStatusBar.Checked = True
End If
SizeControls imgSplitter.Left
End Sub
Private Sub mnuViewUpdateDispaly_Click()
Load frmItemUpdateInterval
frmItemUpdateInterval.Show
End Sub
Private Sub Form_Resize()
On Error Resume Next
If Me.Width < 3000 Then Me.Width = 3000
SizeControls imgSplitter.Left
End Sub
Private Sub imgSplitter_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
With imgSplitter
picSplitter.Move .Left, .Top, .Width \ 2, .Height - 20
End With
picSplitter.Visible = True
mbMoving = True
End Sub
Private Sub imgSplitter_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim sglPos As Single
If mbMoving Then
sglPos = X + imgSplitter.Left
If sglPos < sglSplitLimit Then
picSplitter.Left = sglSplitLimit
ElseIf sglPos > Me.Width - sglSplitLimit Then
picSplitter.Left = Me.Width - sglSplitLimit
Else
picSplitter.Left = sglPos
End If
End If
End Sub
Private Sub imgSplitter_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
SizeControls picSplitter.Left
picSplitter.Visible = False
mbMoving = False
End Sub
Sub SizeControls(X As Single)
On Error Resume Next
'set the width
If X < 3500 Then X = 2500
If X > (Me.Width - 2500) Then X = Me.Width - 2500
tvTreeView.Width = X
imgSplitter.Left = X
lvListView.Left = X + 40
lvListView.Width = Me.Width - (tvTreeView.Width + 140)
lblTitle(0).Width = tvTreeView.Width
lblTitle(1).Left = lvListView.Left + 20
lblTitle(1).Width = lvListView.Width - 40
tvTreeView.Top = picTitles.Height
lvListView.Top = tvTreeView.Top
'set the height
If sbStatusBar.Visible Then
tvTreeView.Height = Me.ScaleHeight - (picTitles.Top + picTitles.Height + sbStatusBar.Height)
Else
tvTreeView.Height = Me.ScaleHeight - (picTitles.Top + picTitles.Height)
End If
lvListView.Height = tvTreeView.Height
imgSplitter.Top = tvTreeView.Top
imgSplitter.Height = tvTreeView.Height
End Sub
Private Sub TreeView1_DragDrop(Source As Control, X As Single, Y As Single)
If Source = imgSplitter Then
SizeControls X
End If
End Sub
Private Sub mnuFileExit_Click()
'unload the form
Unload Me
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -