Migrating from VB6 to VB .NET for ArcGIS 10
[C#]
ButtonAddIn.cs
[Visual Basic .NET]
ButtonAddIn.vb
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.GeoDatabaseUI
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.ArcMapUI
Public Class ButtonAddIn
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Private m_TableView2 As ITableView2
Public Sub New()
End Sub
Protected Overrides Sub OnClick()
Copy()
End Sub
Protected Overrides Sub OnUpdate()
'If TableWindow is not found, disable the command.
Enabled = False
'Create a new instance of the TableWindowClass using the ITableWindow3 interface
Dim tableWindow3 As ITableWindow3 = New TableWindowClass
'Create an empty ISet to hold the open table windows
Dim windowSet As ISet = Nothing
'The .FindOpenTableWindows populates the windowSet object byRef
tableWindow3.FindOpenTableWindows(windowSet)
'If the windowSet is not populated stop execution of OnUpdate
If windowSet Is Nothing Then
Return
End If
'Go to the first item in the ISet
windowSet.Reset()
'Cast the first ISet object to an ITableWindow
Dim tableWindow As ITableWindow = CType(windowSet.Next, ITableWindow)
'Loop through the tableWindows.
Do Until tableWindow Is Nothing
'Get the TableControl from tthe TableWindow
Dim tableControl As ITableControl = tableWindow.TableControl
'Set the member (also known as, Global) variable ITableView2 interface.
m_TableView2 = CType(tableControl, ITableView2)
'If you have selected records, enable the command.
Dim selectionSet As ISelectionSet = m_TableView2.SelectionSet
If selectionSet.Count > 0 Then
Enabled = True
End If
'Iterate over the next ArcMap Window.
tableWindow = CType(windowSet.Next, ITableWindow)
Loop
End Sub
Private Sub Copy()
' Get the selection from the table.
Dim selectionSet As ISelectionSet = m_TableView2.SelectionSet
Dim table As ITable = m_TableView2.Table
Dim cursor As ICursor = Nothing
'Load the selection results into the cursor object.
selectionSet.Search(Nothing, False, cursor)
'Get the first row of the cursor.
Dim rowBuffer As IRowBuffer = cursor.NextRow
'Obtain all of the fields in table.
Dim fields As IFields = rowBuffer.Fields
'Initialize the clipboardString to be empty.
Dim clipboardString As String = ""
'Loop through all of the rows.
Dim Count As Integer = fields.FieldCount
Do Until rowBuffer Is Nothing
'Loop through all of the fields.
Dim index As Integer
For index = 0 To Count - 1
If Not TypeOf rowBuffer.Value(index) Is IGeometry Then
'Extract the strings from each field in the table row.
clipboardString = clipboardString + rowBuffer.Value(index).ToString + ","
End If
Next index
'Remove the trailing comma.
clipboardString = Left(clipboardString, Len(clipboardString) - 1)
'Add the line feed.
clipboardString = clipboardString + vbNewLine
'Go to the next row.
rowBuffer = cursor.NextRow
Loop
'Copy the contents of the clipboardString to the Clipboard.
My.Computer.Clipboard.Clear()
My.Computer.Clipboard.SetText(clipboardString)
End Sub
End Class