ArcObjects Library Reference  

clsCopyTableSelection

Migrating from VB6 to VB .NET for ArcGIS 10

[C#]

clsCopyTableSelection.cs


[Visual Basic .NET]

clsCopyTableSelection.vb

Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.GeoDatabaseUI
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.esriSystem

<ComClass(clsCopyTableSelection.ClassId, clsCopyTableSelection.InterfaceId, clsCopyTableSelection.EventsId), _
 ProgId("VB2008_BaseClass.clsCopyTableSelection")> _
Public NotInheritable Class clsCopyTableSelection
    Inherits BaseCommand

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "719e02c3-71b7-460e-8719-1c9ffe9d581e"
    Public Const InterfaceId As String = "0ae522ba-7791-4183-a385-b052817501c8"
    Public Const EventsId As String = "a8806b0c-a2cb-4d09-8eca-d1f27195b531"
#End Region

#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub RegisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType)
        'Add any COM registration code after the ArcGISCategoryRegistration() call
    End Sub
    <ComUnregisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub UnregisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType)
        'Add any COM unregistration code after the ArcGISCategoryUnregistration() call
    End Sub
#Region "ArcGIS Component Category Registrar generated code"
    Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        MxCommands.Register(regKey)
    End Sub
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        MxCommands.Unregister(regKey)
    End Sub
#End Region
#End Region

    Private m_application As IApplication
    Private m_TableView2 As ITableView2

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()

        ' TODO: Define values for the public properties
        MyBase.m_category = "Developer Samples"
        MyBase.m_caption = "Copy Table Selection VB2008 via Base Class"
        MyBase.m_message = "Copies the selected records in the table window VB2008 via Base Class"
        MyBase.m_toolTip = "Validate Table Selection VB2008 via Base Class"
        MyBase.m_name = "Developer Samples_Copy Table Selection VB2008 via Base Class"

        Try
            'TODO: change bitmap name if necessary
            Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
            MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
        End Try

    End Sub

    Public Overrides Sub OnCreate(ByVal hook As Object)
        If Not hook Is Nothing Then
            m_application = CType(hook, IApplication)

            'Disable if it is not ArcMap
            If TypeOf hook Is IMxApplication Then
                MyBase.m_enabled = True
            Else
                MyBase.m_enabled = False
            End If
        End If

        ' TODO:  Add other initialization code
    End Sub

    Public Overrides Sub OnClick()
        Copy()
    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

        'Initialize the returnString to be empty.
        Dim returnString As String = ""

        'Obtain all of the fields in table.
        Dim fields As IFields = rowBuffer.Fields

        '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.
                    returnString = returnString + rowBuffer.Value(index).ToString + ","

                End If

            Next index

            'Remove the trailing comma.
            returnString = Left(returnString, Len(returnString) - 1)

            'Add the line feed.
            returnString = returnString + 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(returnString)

    End Sub

    Public Overrides ReadOnly Property Enabled() As Boolean
        Get

            '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)

            '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
                    Return True
                End If

                'Iterate over the next ArcMap Window.
                tableWindow = CType(windowSet.Next, ITableWindow)

            Loop

            'If TableWindow is not found, disable the command.
            Return False
        End Get
    End Property

End Class