Drag/Drop and Copy/Paste Example

This is a Drag/Drop and Copy/Paste example for use with the PI Tag Search Dialog and PI Alias Search Dialog.  

 

For this example, open a new visual basic executable project.  Add a reference to the PI-SDK Dialogs, the PISDK Type Library, and the PISDKCommon Type Library.  Add 3 command buttons to Form1.  Name these command buttons cmdCheckClipboard, cmdPaste, and cmdConnections.  Add a label named lblClipStatus. Add a ListBox named List1 to Form1.  Copy and paste the code in this example to Form1.

 

After the following application is built, show the PI Tag Search Dialog or the PI Alias Search Dialog  from a separate application.  Drag PI Aliases or PI Points for the search results to the list box in the example application.  Alternatively, right-click on the search results in the PI Tag or Alias Search Dialog and select "copy" from the popup menu.  Next click on the cmdCheckClipboard button in the example application.  This should cause the cmdPaste button in the example application to become enabled.  Click on the paste button to paste the copied tags to the example application.

 

Option Explicit

Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long

Dim Drag As New DragDrop
Dim Connect As New Connections

Private Sub cmdConnections_Click()
Connect.ShowConnectionDialog False, vbModal
End Sub

Private Sub cmdCheckClipboard_Click()
Dim bFound As Boolean
If Drag.IsAliasListOnClipboard Then
cmdPaste.Enabled = True
lblClipStatus.Caption = "Alias list on clipboard"
bFound = True
End If

If Drag.IsPointListOnClipboard Then
cmdPaste.Enabled = True
lblClipStatus.Caption = "Point list on clipboard"
bFound = True
End If

If IsClipboardFormatAvailable(vbCFText) > 0 Then
lblClipStatus.Caption = lblClipStatus.Caption & ", vbCFText on Clipboard"
End If

If Not bFound Then
cmdPaste.Enabled = False
lblClipStatus.Caption = "Nothing on clipboard"
End If
End Sub

Private Sub cmdPaste_Click()
On Error GoTo handler
If Drag.IsAliasListOnClipboard Then
CopyAliasListToListBox Drag.GetAliasListFromClipboard
ElseIf Drag.IsPointListOnClipboard Then
CopyPointListToListBox Drag.GetPointListFromClipboard
End If
Exit Sub
handler:
MsgBox Err.Number & vbCrLf & Err.Source & vbCrLf & Err.Description
End Sub

Private Sub Form_Load()
On Error GoTo Handler
cmdPaste.Enabled = False
List1.OLEDropMode = vbOLEDropManual
Connect.Login Servers.DefaultServer
Exit Sub
Handler:
MsgBox Err.Number & vbCrLf & Err.Description
End Sub

Private Sub CopyAliasListToListBox(AliasList As PIAliasList2)
Dim Alias As PIAlias
List1.Clear
For Each Alias In AliasList
List1.AddItem Alias.Name & " " & Alias.DataSource.PathName
Next
End Sub

Private Sub CopyPointListToListBox(PtList As PointList)
Dim Pt As PIPoint
List1.Clear
For Each Pt In PtList
List1.AddItem Pt.Name
Next
End Sub

Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error GoTo handler
Dim AliasList As PIAliasList2
Dim PtList As PointList
Dim bByteStream() As Byte

Me.SetFocus

If Data.GetFormat(Drag.AliasListClipboardFormat) Then
bByteStream = Data.GetData(Drag.AliasListClipboardFormat)
Set AliasList = Drag.RestoreAliasList(CStr(bByteStream))
CopyAliasListToListBox AliasList
Effect = vbDropEffectCopy

ElseIf Data.GetFormat(Drag.PointListClipboardFormat) Then
bByteStream = Data.GetData(Drag.PointListClipboardFormat)
Set PtList = Drag.RestorePointList(CStr(bByteStream))
CopyPointListToListBox PtList
Effect = vbDropEffectCopy
End If
Exit Sub
handler:
MsgBox Err.Number & vbCrLf & Err.Source & vbCrLf & Err.Description
End Sub

Private Sub List1_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
If Data.GetFormat(Drag.AliasListClipboardFormat) Or _
Data.GetFormat(Drag.PointListClipboardFormat) Then
Effect = vbDropEffectCopy
Else
'Do not allow unformatted text to be dropped on the search results
Effect = vbDropEffectNone
End If
End Sub

Enabling Operational Intelligence