English

How To: Programmatically loop through a text file using ArcObjects

Summary

This article provides a sample of looping through a text file using ArcObjects.

Procedure

 

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
    Note:
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy this code into the UIButtonControl's click event.
     
    Code:
    Dim pMxdoc As IMxDocument
    Set pMxdoc = ThisDocument
    Dim pTable As ITable
    Dim pStandColl As IStandaloneTableCollection
    Dim pStandTab As IStandaloneTable
    Set pStandColl = pMxdoc.FocusMap
    Set pStandTab = pStandColl.StandaloneTable(0)
    Set pTable = pStandTab
    Dim pRow As IRow, pCursor As ICursor
    Set pCursor = pTable.Search(Nothing, False)
    Set pRow = pCursor.NextRow
    
    'Display a message box for each record
    Do Until pRow Is Nothing
      MsgBox pRow.Value(0) & " : " & CStr(pRow.Value(1))
      Set pRow = pCursor.NextRow
    Loop
    
  5. Create a text file called Cities.txt with the following contents. Column names must be enclosed in double quotes and record values need to be comma delimited.
     
    "CITY","POPULATION" 
    Los Angles,14234953 
    New York,17834034
  6. Add Cities.txt to ArcMap.
  7. Click the new UIButtonControl to loop through the text file.