English

How To: Freeze fields in ArcMap using ArcObjects

Summary

It is possible to right-click one or more fields in an open table in ArcMap and select 'Freeze/Unfreeze Column'. Frozen fields move to the left-hand side of the table window and stay there as the horizontal scroll bar is used.

The following sample code shows how to perform this operation using ArcObjects. The code assumes that the attribute table contains five fields in the following order:

OBJECTID, SHAPE, NAME, FEA_CODE, SHAPE_LENGTH

The code also assumes the map document only has one table in it. If this is not the case, it will be necessary to loop through the IEnumTableProperties, in order to QI up to IDatset::Name, or something similar.

This code example will freeze the NAME and FEA_CODE items.

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select 'ArcMap Objects > ThisDocument', then right-click and select 'View Code'.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in the Project's 'ThisDocument' code module will only run in the current map document. If you want to store the code in all your map documents open the Normal.mxt 'ThisDocument' code module instead.

  4. Copy the following code into the code module.
    Code:
    Public Sub freezeFields()
    ' Start by getting the feature layer
    ' to be shown in a table window.
    Dim pMxDoc As IMxDocument
    Dim pFeatLayer As IFeatureLayer
    Set pMxDoc = ThisDocument
    Set pFeatLayer = pMxDoc.FocusMap.Layer(0)
    ' Open the feature layer in a table window.
    Dim pTableWindow As ITableWindow
    Set pTableWindow = New TableWindow
    Set pTableWindow.FeatureLayer = pFeatLayer
    Set pTableWindow.Application = Application
    pTableWindow.ShowAliasNamesInColumnHeadings = True
    ' This will open the table in a window.
    pTableWindow.Show True
    ' Get the properties for this table window.
    Dim pTableProperties As ITableProperties
    Dim pEnumTableProperties As IEnumTableProperties
    Dim pTableProperty As ITableProperty
    Set pTableProperties = pMxDoc.TableProperties
    Set pEnumTableProperties = pTableProperties.IEnumTableProperties
    pEnumTableProperties.Reset
    Set pTableProperty = pEnumTableProperties.Next
    ' Re-order the fields so the ones to be frozen
    ' are on the left-side of the table window.
    pTableProperty.FieldOrder _
    = "NAME, FEA_CODE, OBJECTID, Shape, Shape_Length"
    ' Freeze the required number of fields,
    ' starting on the left-side of the table window.
    pTableProperty.FrozenFields = 2 ' set to 0 to unfreeze.
    ' Refresh the table window.
    ' The fields NAME and FEA_CODE will be frozen.
    Dim pTableControl As ITableControl
    Set pTableControl = pTableWindow.TableControl
    pTableControl.Redraw
    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.