ArcObjects Library Reference  

PropertyForm

About the Server spatial query server object extension Sample

[C#]

PropertyForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace SpatialQuerySOE.ArcCatalog
{
    /// <summary>
    /// Defines the UI for the Spatial Query SOE
    /// </summary>
    public partial class PropertyForm : Form
    {
        #region Member Variables

        // The current SOE layer
        private string m_layer;

        // Current SOE field
        private string m_field;

        // Tracks whether the form is initializing
        private bool m_init = false;

        private System.Collections.Generic.Dictionary<string, string[]> m_fieldsDictionary = 
            new System.Collections.Generic.Dictionary<string,string[]>();

        #endregion

        #region Constructor

        public PropertyForm()
        {
            InitializeComponent();

            // Get ArcCatalog's representation of the map service being modified
            System.Type type = System.Type.GetTypeFromCLSID(typeof(ESRI.ArcGIS.Framework.AppRefClass).GUID);
            ESRI.ArcGIS.CatalogUI.IGxApplication gxApp = Activator.CreateInstance(type) as ESRI.ArcGIS.CatalogUI.IGxApplication;
            ESRI.ArcGIS.Catalog.IGxAGSObject gxAgsObj = gxApp.SelectedObject as ESRI.ArcGIS.Catalog.IGxAGSObject;

            // If the service is not stopped, disable the layers and fields drop-down lists so that the SOE cannot be configured
            if (gxAgsObj.Status != "Stopped")
            {
                ComboLayers.Enabled = false;
                ComboFields.Enabled = false;
            }
        }

        #endregion

        #region Internal Properties - getHWnd, Layer, Field, PageSite

        /// <summary>
        /// The form's handle
        /// </summary>
        internal int getHWnd()
        {
            return this.Handle.ToInt32();
        }

        /// <summary>
        /// The currently selected layer
        /// </summary>
        internal string Layer
        {  
            get { return m_layer; }
            set { m_layer = value; }
        }

        /// <summary>
        /// The currently selected field
        /// </summary>
        internal string Field
        {
            get { return m_field; }
            set { m_field = value; }
        }

        /// <summary>
        /// The PageSite of the IComPropertyPage object using the form.  Allows the form to report when its contents have changed.
        /// </summary>
        internal ESRI.ArcGIS.Framework.IComPropertyPageSite PageSite { private get; set; }

        #endregion

        #region Internal Methods - SetMap

        /// <summary>
        /// Sets the map used to populate the layers and fields drop-down lists
        /// </summary>
        /// <param name="filePath">the path to the map document</param>
        internal void SetMap(string filePath)
        {
            // Open the map document
            ESRI.ArcGIS.Carto.IMapDocument mapDocument = new ESRI.ArcGIS.Carto.MapDocumentClass();
            mapDocument.Open(filePath, null);

            // The call to get_Map() will create a lock (ldb) on a personal gdb.  Make sure ArcCatalog user and 
            // ArcGIS Server container account has read\write access to data location.  If not,
            // feature classes in the personal gdb may not be accessible (or visible in the map).
            ESRI.ArcGIS.Carto.IMap map = mapDocument.get_Map(0);

            // Get IGeoFeatureLayers from the map
            ESRI.ArcGIS.esriSystem.UID id = new ESRI.ArcGIS.esriSystem.UIDClass();
            id.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
            ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(id, true);

            int selectedIndex = 0;
            string[] fieldNames = null;
            m_fieldsDictionary.Clear();
            // Add the names of simple polygon feature layers to the layers drop-down and store the names
            // of each layer's fields in a dictionary
            ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = null;
            while ((featureLayer = enumLayer.Next() as ESRI.ArcGIS.Carto.IFeatureLayer) != null)
            {
                if (featureLayer.FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon &&
                featureLayer.FeatureClass.FeatureType == ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple)
                    ComboLayers.Items.Add(featureLayer.Name);

                if (featureLayer.Name == m_layer)
                    selectedIndex = ComboLayers.Items.Count - 1;

                fieldNames = new string[featureLayer.FeatureClass.Fields.FieldCount];
                for (int i = 0; i < fieldNames.Length; i++)
                    fieldNames[i] = featureLayer.FeatureClass.Fields.get_Field(i).Name;

                m_fieldsDictionary.Add(featureLayer.Name, fieldNames);
            }

            mapDocument.Close();
            mapDocument = null;
            map = null;

            // Toggle the init flag and initialize the layers drop-down to show the current SOE layer.  Init is used
            // to prevent the drop-down's SelectedIndexChanged logic from setting m_field during initialization.
            m_init = true;
            ComboLayers.SelectedIndex = selectedIndex;
        }

        #endregion

        #region Control Event Handlers - ComboLayers_SelectedIndexChanged, ComboFields_SelectedIndexChanged, PropertyForm_FormClosed

        // Fires when a new item is selected from the layers drop-down
        private void ComboLayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_layer = ComboLayers.Text;

            ComboFields.Items.Clear();

            // Add the fields for the currently selected layer to the fields drop-down
            int selectedIndex = 0;
            string[] fieldNames = m_fieldsDictionary[m_layer];
            for (int i = 0; i < fieldNames.Length; i++)
            {
                ComboFields.Items.Add(fieldNames[i]);

                // Get the index of the current field if the method is executing during initialization and
                // the current field match's the SOE field
                if (m_init && fieldNames[i] == m_field)
                    selectedIndex = ComboFields.Items.Count - 1;
            }

            // Update the field and layer properties if executing during initialization.  Otherwise, toggle the
            // init boolean to indicate initialization is complete.
            if (m_init)
                m_init = false;

            // Set the fields drop-down to show the current SOE field.
            ComboFields.SelectedIndex = selectedIndex;
        }

        // Fires when a new item is selected from the fields drop-down
        private void ComboFields_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Update the current SOE field
            m_field = ComboFields.Text;

            // Notify ArcCatalog that the properties have changed
            this.PageSite.PageChanged();
        }

        #endregion
    }
}
[Visual Basic .NET]

PropertyForm.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms

Namespace SpatialQuerySOE.ArcCatalog_VBNet
  ''' <summary>
  ''' Defines the UI for the Spatial Query SOE
  ''' </summary>
  Partial Public Class PropertyForm
    Inherits Form
#Region "Member Variables"

    ' The current SOE layer
    Private m_layer As String

    ' Current SOE field
    Private m_field As String

    ' Tracks whether the form is initializing
    Private m_init As Boolean = False

    Private m_fieldsDictionary As New System.Collections.Generic.Dictionary(Of String, String())()

#End Region

#Region "Constructor"

    Public Sub New()
      InitializeComponent()

      ' Get ArcCatalog's representation of the map service being modified
      Dim type As System.Type = System.Type.GetTypeFromCLSID(GetType(ESRI.ArcGIS.Framework.AppRefClass).GUID)
      Dim gxApp As ESRI.ArcGIS.CatalogUI.IGxApplication = TryCast(Activator.CreateInstance(type), ESRI.ArcGIS.CatalogUI.IGxApplication)
      Dim gxAgsObj As ESRI.ArcGIS.Catalog.IGxAGSObject = TryCast(gxApp.SelectedObject, ESRI.ArcGIS.Catalog.IGxAGSObject)

      ' If the service is not stopped, disable the layers and fields drop-down lists so that the SOE cannot be configured
      If gxAgsObj.Status <> "Stopped" Then
        ComboLayers.Enabled = False
        ComboFields.Enabled = False
      End If
    End Sub

#End Region

#Region "Internal Properties - getHWnd, Layer, Field, PageSite"

    ''' <summary>
    ''' The form's handle
    ''' </summary>
    Friend Function getHWnd() As Integer
      Return Me.Handle.ToInt32()
    End Function

    ''' <summary>
    ''' The currently selected layer
    ''' </summary>
    Friend Property Layer() As String
      Get
        Return m_layer
      End Get
      Set(ByVal value As String)
        m_layer = value
      End Set
    End Property

    ''' <summary>
    ''' The currently selected field
    ''' </summary>
    Friend Property Field() As String
      Get
        Return m_field
      End Get
      Set(ByVal value As String)
        m_field = value
      End Set
    End Property

    ''' <summary>
    ''' The PageSite of the IComPropertyPage object using the form.  Allows the form to report when its contents have changed.
    ''' </summary>
    Private privatePageSite As ESRI.ArcGIS.Framework.IComPropertyPageSite
    Friend Property PageSite() As ESRI.ArcGIS.Framework.IComPropertyPageSite
      Private Get
        Return privatePageSite
      End Get
      Set(ByVal value As ESRI.ArcGIS.Framework.IComPropertyPageSite)
        privatePageSite = value
      End Set
    End Property

#End Region

#Region "Internal Methods - SetMap"

    ''' <summary>
    ''' Sets the map used to populate the layers and fields drop-down lists
    ''' </summary>
    ''' <param name="filePath">the path to the map document</param>
    Friend Sub SetMap(ByVal filePath As String)
      ' Open the map document
      Dim mapDocument As ESRI.ArcGIS.Carto.IMapDocument = New ESRI.ArcGIS.Carto.MapDocumentClass()
      mapDocument.Open(filePath, Nothing)

      ' The call to get_Map() will create a lock (ldb) on a personal gdb.  Make sure ArcCatalog user and 
      ' ArcGIS Server container account has read\write access to data location.  If not,
      ' feature classes in the personal gdb may not be accessible (or visible in the map).
      Dim map As ESRI.ArcGIS.Carto.IMap = mapDocument.Map(0)

      ' Get IGeoFeatureLayers from the map
      Dim id As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
      id.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"
      Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = map.Layers(id, True)

      Dim selectedIndex As Integer = 0
      Dim fieldNames() As String = Nothing
      m_fieldsDictionary.Clear()
      ' Add the names of simple polygon feature layers to the layers drop-down and store the names
      ' of each layer's fields in a dictionary
      Dim featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = Nothing
      featureLayer = TryCast(enumLayer.Next(), ESRI.ArcGIS.Carto.IFeatureLayer)
      Do While featureLayer IsNot Nothing
        If featureLayer.FeatureClass.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon AndAlso featureLayer.FeatureClass.FeatureType = ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple Then
          ComboLayers.Items.Add(featureLayer.Name)
        End If

        If featureLayer.Name = m_layer Then
          selectedIndex = ComboLayers.Items.Count - 1
        End If

        fieldNames = New String(featureLayer.FeatureClass.Fields.FieldCount - 1) {}
        For i As Integer = 0 To fieldNames.Length - 1
          fieldNames(i) = featureLayer.FeatureClass.Fields.Field(i).Name
        Next i

        m_fieldsDictionary.Add(featureLayer.Name, fieldNames)
        featureLayer = TryCast(enumLayer.Next(), ESRI.ArcGIS.Carto.IFeatureLayer)
      Loop

      mapDocument.Close()
      mapDocument = Nothing
      map = Nothing

      ' Toggle the init flag and initialize the layers drop-down to show the current SOE layer.  Init is used
      ' to prevent the drop-down's SelectedIndexChanged logic from setting m_field during initialization.
      m_init = True
      ComboLayers.SelectedIndex = selectedIndex
    End Sub

#End Region

#Region "Control Event Handlers - ComboLayers_SelectedIndexChanged, ComboFields_SelectedIndexChanged, PropertyForm_FormClosed"

    ' Fires when a new item is selected from the layers drop-down
    Private Sub ComboLayers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboLayers.SelectedIndexChanged
      m_layer = ComboLayers.Text

      ComboFields.Items.Clear()

      ' Add the fields for the currently selected layer to the fields drop-down
      Dim selectedIndex As Integer = 0
      Dim fieldNames() As String = m_fieldsDictionary(m_layer)
      For i As Integer = 0 To fieldNames.Length - 1
        ComboFields.Items.Add(fieldNames(i))

        ' Get the index of the current field if the method is executing during initialization and
        ' the current field match's the SOE field
        If m_init AndAlso fieldNames(i) = m_field Then
          selectedIndex = ComboFields.Items.Count - 1
        End If
      Next i

      ' Update the field and layer properties if executing during initialization.  Otherwise, toggle the
      ' init boolean to indicate initialization is complete.
      If m_init Then
        m_init = False
      End If

      ' Set the fields drop-down to show the current SOE field.
      ComboFields.SelectedIndex = selectedIndex
    End Sub

    ' Fires when a new item is selected from the fields drop-down
    Private Sub ComboFields_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboFields.SelectedIndexChanged
      ' Update the current SOE field
      m_field = ComboFields.Text

      ' Notify ArcCatalog that the properties have changed
      Me.PageSite.PageChanged()
    End Sub

#End Region
  End Class
End Namespace