StateSet Example

 

This example on load connects to the default server and displays the server's state sets in List1. Clicking on a set in list1 displays the digital states in List2. Clicking on a state from List2 displays the state name and its code at the bottom of the screen.

When entering a state name in Text1 and clicking Command1 the application calls StateSet.Add and adds the new state to the end of the selected StateSet. Entering a new name for a state in Text2 and clicking Command2 calls StateSet.RenameState changing the selected state to the name input.

 

It is recommended that you exercise care in modifying state sets with this or any program. Using a development PI server is recommended for testing applications that perform these types of operations.

Build the StateSet 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 state set"
    Exit Sub
  End If
  If Text1.Text = "" Then
    MsgBox "Please enter a name for the new state to add."
    Exit Sub
  End If
  Dim stset As StateSet
  Set stset = srv.StateSets(List1.Text)
  stset.Add Text1.Text
  ShowStates stset
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Command2_Click()
On Error GoTo handler
  If List1.Text = "" Then
    MsgBox "Please select a state set"
    Exit Sub
  End If
  If List2.Text = "" Then
    MsgBox "Please select a digital state to rename"
    Exit Sub
  End If
  If Text2.Text = "" Then
    MsgBox "Please enter a new name for the digital state"
    Exit Sub
  End If
  Dim stset As StateSet
  Set stset = srv.StateSets(List1.Text)
  stset.RenameState List2.Text, Text2.Text
  ShowStates stset
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Form_Load()
On Error GoTo handler
  ClearCaptions
  Set srv = Servers.DefaultServer
  srv.Open
  Dim stset As StateSet
  For Each stset In srv.StateSets
    List1.AddItem stset.Name
  Next
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub List1_Click()
On Error GoTo handler
  Dim stset As StateSet
  List2.Clear
  Set stset = srv.StateSets(List1.Text)
  Label8.Caption = stset.Name
  ShowStates stset
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub List2_Click()
On Error GoTo handler
  Dim stset As StateSet
  Dim dgst As DigitalState
  If List1.Text = "" Then
    MsgBox "Please select a state set"
    Exit Sub
  End If
  Set stset = srv.StateSets(List1.Text)
  Set dgst = stset(List2.Text)
  Label10.Caption = dgst.Name
  Label12.Caption = dgst.Code
Exit Sub
handler:
  MsgBox Err.Description
End Sub
Private Sub ClearCaptions()
  Label8.Caption = ""
  Label10.Caption = ""
  Label12.Caption = ""
  Text1.Text = ""
  Text2.Text = ""
End Sub
Private Sub ShowStates(stset As StateSet)
  Dim digst As DigitalState
  List2.Clear
  For Each digst In stset
    List2.AddItem digst.Name
  Next
End Sub

Enabling Operational Intelligence