PIContexts Example

Picture

 

This example, during the Form_Load, connects to the default server, displays the server name, and fills a list box with the server's context strings. Clicking on a context in the list box displays its description (Label2). Contexts can be removed from the server by selecting the desired PIContext from the list box (List1) and hitting the button Command1. The list of contexts is refreshed if the removal is successful. To add a context, fill in the name and description text boxes as desired and hit the button Command2. The list of contexts is refreshed if the add is successful.

 

Build the PIContexts 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, &ldots;) 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 ErrorHandler
  If List1.Text = "" Then
    MsgBox "Please select a PIContext to remove."
    Exit Sub
  End If
srv.PIContexts.Remove List1.Text
RepaintContexts
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub

Private Sub Command2_Click()
On Error GoTo ErrorHandler
  If Text1.Text = "" Then
    MsgBox "Please enter a context name to add"
    Exit Sub

  End If
  srv.PIContexts.Add Text1.Text, Text2.Text
  RepaintContexts
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub

Private Sub Form_Load()
On Error GoTo ErrorHandler
  Set srv = Servers.DefaultServer
  ' Open with a user with enough permission to add contexts
  srv.Open "UID=piadmin"
  ' Fill the list of contexts
  Dim ctx As PIContext
  For Each ctx In srv.PIContexts
    List1.AddItem ctx.Name
  Next
  List1.ListIndex = 0

  Text1.Text = ""
  Text2.Text = ""
  Label1.Caption = srv.Name
  Label2.Caption = ""
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub
Private Sub RepaintContexts()
  List1.Clear
  Dim ctx As PIContext
  For Each ctx In srv.PIContexts
    List1.AddItem ctx.Name
  Next
End Sub

Private Sub List1_Click()
On Error GoTo ErrorHandler
  'Show the description for this context
  Dim ctx As PIContext
  Set ctx = srv.PIContexts.Item(List1.Text)

  Label2.Caption = ctx.Description
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub
Enabling Operational Intelligence