StateSets Example

 

This example on load displays a list of the servers from the Known Servers Table. Clicking on a member of the list box displays in List2 the StateSet collections on the selected Server. Selecting a StateSet from List2 displays the digital states that make up the StateSet in List3.

The bottom half of the screen demonstrates adding and removing state sets from a server. The user selects one or more of the sample digital states displayed in List4, specifies a name for the new state set and clicks Command1 to add it. To remove a state set the user selects a server from List1 and a state set from List2 then clicks Command2. A confirmation prompt is displayed as deleting a state set that is needed can be troublesome. It is recommended that you exercise care in adding and deleting state sets with this or any program. Using a development PI server is recommended for testing applications that perform this type of operation.

 

Build the StateSets example as follows:

1. Create a new project and add a reference to the PISDK library.

2. Add the visual elements as shown in the picture. Make sure the numbered element names (label1, list1, etc.) match those shown.

3. Cut and paste the code below into the project.

Option Explicit
Dim srv As Server

Private Sub Command1_Click()
On Error GoTo handler
  If List1.Text = "" Then
    MsgBox "Please select a server to which to add the StateSet"
    Exit Sub
  End If
  If Text1.Text = "" Then
    MsgBox "Please enter a name for the new StateSet."
    Exit Sub
  End If
  If List4.SelCount < 1 Then
    MsgBox "Please select one or more states to put in the new StateSet"
    Exit Sub
  End If
  ' Build an array of the selected states
  Dim saStates() As String
  Dim indx As Integer
  Dim arrIndx As Integer
  ReDim saStates(0 To List4.SelCount - 1)
  arrIndx = 0
  For indx = 0 To List4.ListCount - 1
    If List4.Selected(indx) = True Then
      saStates(arrIndx) = List4.List(indx)
      arrIndx = arrIndx + 1
    End If
  Next
  ' Do the add
  Set srv = Servers(List1.Text)
  srv.StateSets.Add Text1.Text, saStates
  ShowStateSets srv
Exit Sub
handler:
MsgBox Err.Description
End Sub

Private Sub Command2_Click()
On Error GoTo handler
  If List1.Text = "" Then
    MsgBox "Please select a server from which to remove a StateSet"
    Exit Sub
  End If
  If List2.Text = "" Then
    MsgBox "Please select a StateSet to remove"
    Exit Sub
  End If
  Dim res As VbMsgBoxResult
  res = MsgBox("You have asked to remove StateSet " & List2.Text _
    & " from server " & List1.Text & "." & vbCrLf _
    & "Is this OK?", vbYesNo, "Remove StateSet?")
  If res <> vbYes Then
    Exit Sub
  End If
  Set srv = Servers(List1.Text)
  srv.StateSets.Remove List2.Text
  ShowStateSets srv
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Form_Load()
For Each srv In Servers
  List1.AddItem srv.Name
Next
Text1.Text = ""
LoadSampleDigStates
End Sub

Private Sub List1_Click()
On Error GoTo handler
  Dim stset As StateSet
  Set srv = Servers(List1.Text)
  ShowStateSets srv
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub List2_Click()
On Error GoTo handler
  If List1.Text = "" Then
    MsgBox "Please select a server before selecting a State Set."
    Exit Sub
  End If
  Set srv = Servers(List1.Text)
  Dim stset As StateSet
  List3.Clear
  Set stset = srv.StateSets(List2.Text)
  Dim dgst As DigitalState
  For Each dgst In stset
    List3.AddItem dgst.Name
  Next
Exit Sub
handler:
  MsgBox Err.Description
End Sub
Private Sub LoadSampleDigStates()
  List4.AddItem "January"
  List4.AddItem "February"
  List4.AddItem "March"
  List4.AddItem "April"
  List4.AddItem "May"
  List4.AddItem "June"
  List4.AddItem "July"
  List4.AddItem "August"
  List4.AddItem "September"
  List4.AddItem "October"
  List4.AddItem "November"
  List4.AddItem "December"
End Sub
Private Sub ShowStateSets(srv As Server)
  List2.Clear
  List3.Clear
  Dim stset As StateSet
  For Each stset In srv.StateSets
    List2.AddItem stset.Name
  Next
End Sub
Enabling Operational Intelligence