ArcFM Engine Developer Guide
Control Code Sample

Version: 10.2.1c and 10.2.1c SP3

Resource Center Home

This code sample illustrates a custom control that zooms the extent of the map to the specified scale. Control commands must be added to the custom layout XML file before they will appear in the user interface.

This control provides a list of options from which the user can choose. The code sample in this section provides a control that offers a list of map extents.


Combo box control

This sample requires installation of the ArcFM Solution Software Developer Kit (SDK). You may need to change the existing installation in Add/Remove Programs to add Developer Resources.

While custom controls may be created for Responder Explorer, this particular sample may not be used in Responder Explorer as there is no map display in which to zoom.

Product Availability: ArcFM Engine applications, ArcFM Viewer for ArcGIS Engine

References: Miner.Framework, Miner.Interop.Framework, Miner.Interop.System, Miner.mmSystem, Miner.Windows, Miner.Windows.ArcGIS, Miner.Windows

  1. Set the using directives.

  2. Declare inheritance from CommandComboBox.

  3. Set private declarations.

  4. Use constructors to define how the object is to be constructed. In this example "DevMapZoomControl" is the Key value.

  5. Set the command properties.

  6. Sets up the event handler for when the current map changes.

  7. Override the OnClick event.

  8. Enabled calls the IsEnabled event. This is not required. The code executed in the IsEnabled event could be executed in the Enabled event instead.

  9. OnActivating sends a message to the status bar, then calls back to the base class.

  10. OnDeactivated clears the status bar.

  11. OnDropDownOpening builds a list of map scales from which the user may select.

  12. OnKeyPress allows the user to enter a value in the map extent field. This event validates that the input is numerical.

  13. Calls the OnClick event.

  14. Executes when the event is fired.

  15. Displays the current map scale in the combo box.

C# Sample
Copy Code
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;
 
using Esri.ArcGIS.Carto;
using Esri.ArcGIS.esriSystem;
 
using Miner.Interop;
//1
using Miner.Windows.ArcGIS;
using Miner.Windows.Commands;
 
namespace Miner.DeveloperSamples.Engine
{
//2
      public class DevMapZoomControl : ComboBoxCommand
      {
            #region Private Declarations
 
            private const int CONTROL_WIDTH = 90;
            private bool _buildList = true;
            private ListDictionary _mapScaleItems = new ListDictionary();
//3         
        bool _addToList = true;
            bool _shouldExecute = false;
 
            #endregion Private Declarations
 
            #region Public Constructors/Destructors
            
            //base constructor parameters:  base(key, caption)
            //key = unique identifier
            //caption = caption of control
//4
            public DevMapZoomControl() : base("DevMapZoomControl", "&MapZoomControl")
            {
                  this.Width = CONTROL_WIDTH;
                  this.DropDownStyle = DropDownStyle.DropDown;
//5
                  this.Category = "ArcFM Developer Samples";
                  this.Tooltip = "Dev: MapScale";
                  this.CustomizerCaption = "Dev: MapScale";
                  this.CustomizerDescription = "Dev: Allows the user to set the map scale.";
 
                  BuildListDictionary();
//6
                  MainArcEngineApp.CurrentMapChanged +=new MainArcEngineApp.CurrentMapChangedEventHandler(MainArcEngineApp_CurrentMapChanged);
            }
 
            #endregion Public Constructors/Destructors
 
            #region Command Overrides
//7         
            protected override void OnClick(EventArgs args)
            {
                  try 
                  {
                        _shouldExecute = false;
                        if (IsEnabled(MainArcEngineApp.FocusMap()) == false) return;
 
                        string itemName = this.Text;                    
                        if (itemName.Length <= 0) return;
 
                        AddItemToList(itemName, true);
                        
                        double mapScale = FormatScaleItemDouble(itemName);
                        if (mapScale <= 0.0) return;
 
                        IMap currentMap = MainArcEngineApp.FocusMap();
                        if (null == currentMap) return;
 
                        currentMap.MapScale = mapScale;
                        IActiveView activeView = currentMap as IActiveView;
                        activeView.Refresh();
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::Execute" + exc.ToString());
                        return;
                  }
            }
//8
            public override bool Enabled
            {
                  get
                  {
                        base.Enabled = IsEnabled(MainArcEngineApp.FocusMap()); 
                        return base.Enabled;
                  }
                  set
                  {
                        base.Enabled = value;
                  }
            }
//9
            protected override void OnActivating(CancelableCommandEventArgs args)
            {
                  //Display Message
                  try 
                  {
                        IMMArcGISRuntimeEnvironment pRTE = new Miner.Framework.ArcGISRuntimeEnvironment();
                        if (null == pRTE) return;
                        
                        pRTE.SetStatusBarMessage("Dev: Allows user to set map scale.");
                  }
                  catch 
                  {
                        //do nothing; we do not want to fail just because we cannot write 
                        //to the status bar
                  }
                  base.OnActivating (args);
            }
//10
            protected override void OnDeactivated(CommandEventArgs args)
            {
                  //Clear Message
                  try 
                  {
                        IMMArcGISRuntimeEnvironment pRTE = new Miner.Framework.ArcGISRuntimeEnvironment();
                        if (null == pRTE) return;
                        
                        pRTE.SetStatusBarMessage("");
                  }
                  catch 
                  {
                        //do nothing; we do not want to fail just because we cannot write 
                        //to the status bar
                  }
                  base.OnDeactivated (args);
            }
//11
            protected override void OnDropDownOpening(CancelableCommandEventArgs args)
            {
                  BuildDropDownList();
                  base.OnDropDownOpening (args);
            }
//12
            protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs args)
            {
                  //prevent overflow
                  try 
                  {
                        int testScale = Convert.ToInt32(FormatScaleItemDouble(this.Text + Convert.ToString(args.KeyChar)));
                  }
                  catch (System.OverflowException)
                  {
                        args.Handled = true;
                        return;
                  }
 
                  //prevent illegal chars
                  try 
                  {
                        if (args.KeyChar == ':') 
                        {
                              string currentText = this.Text;
                              int indexSeparator = currentText.IndexOf(":");
                              if (indexSeparator < 0) 
                              {
                                    if (currentText.Length > 0) 
                                    {
                                          char firstChar = currentText[0];
                                          if (firstChar == 49) 
                                          {
                                                return;
                                          }
                                    }
                                    else 
                                    {
                                          return;
                                    }
                              }
                              else 
                              {
                                    args.Handled = true;
                                    return;
                              }
                        }
 
                        //if (!(Char.IsDigit(args.KeyChar)) && !(Char.IsControl(args.KeyChar)) && (args.KeyChar != ':'))
                        if (!(Char.IsDigit(args.KeyChar)) && !(Char.IsControl(args.KeyChar)))
                        {
                              args.Handled = true;
                        }
 
                        if (args.KeyChar != 13) 
                        {
                              _shouldExecute = true;
                        }
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::OnKeyPress" + exc.ToString());
                        return;
                  }
            }
//13
            protected override void OnTextCommitted(EventArgs args)
            {
                  if (_shouldExecute == true) 
                  {
                        System.Diagnostics.Debug.WriteLine("Executing...");
                        OnClick(new EventArgs());
                  }
            }
 
 
            #endregion Command Overrides
 
            #region Private Methods
            
            private void BuildListDictionary() 
            {
                  try 
                  {
                        if (null == _mapScaleItems) 
                        {
                              _mapScaleItems = new ListDictionary();
                        }
                        _mapScaleItems.Add("1:1000", "1:1,000");
                        _mapScaleItems.Add("1:10000", "1:10,000");
                        _mapScaleItems.Add("1:24000", "1:24,000");
                        _mapScaleItems.Add("1:100000", "1:100,000");
                        _mapScaleItems.Add("1:250000", "1:250,000");
                        _mapScaleItems.Add("1:500000", "1:500,000");
                        _mapScaleItems.Add("1:750000", "1:750,000");
                        _mapScaleItems.Add("1:1000000", "1:1,000,000");
                        _mapScaleItems.Add("1:3000000", "1:3,000,000");
                        _mapScaleItems.Add("1:10000000", "1:10,000,000");
                  }
                  catch (Exception exc) 
                  {
                        _buildList = true;
                        MessageBox.Show("MapZoomControl::BuildListDictionary" + exc.ToString());
                        return;
                  }
            }
            
            private void BuildDropDownList() 
            {
                  if (_buildList == false) return;
                  if (null == _mapScaleItems) return;
 
                  try 
                  {
                        IDictionary mapScaleDict = _mapScaleItems as IDictionary;
                        foreach(DictionaryEntry de in mapScaleDict)
                        {
                              AddItemToList(Convert.ToString(de.Key), false);
                        }
                        _buildList = false;
                  }
                  catch (Exception exc) 
                  {
                        _buildList = true;
                        MessageBox.Show("MapZoomControl::BuildDropDownList" + exc.ToString());
                        return;
                  }
            }
 
            private void AddItemToList(string itemName, bool checkExists) 
            {
                  try 
                  {     
                        if (_addToList == false) return;
                        string formattedItemName = FormatScaleItemString(itemName);
                        if (formattedItemName.Length <= 0) return;
                        if (checkExists == true) 
                        {
                              if (IsInList(formattedItemName) == true) return;
                        }
                        if (this.Items.Contains(formattedItemName) == false) 
                        {
                              this.Items.Add(formattedItemName, formattedItemName);
                        }
                        if (null == _mapScaleItems) 
                        {
                              _mapScaleItems = new ListDictionary();
                        }
                        if (_mapScaleItems.Contains(itemName) == false) 
                        {
                              _mapScaleItems.Add(formattedItemName, formattedItemName);
                        }
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::AddItemToList" + exc.ToString());
                        return;
                  }
            }
 
            private bool IsInList(string itemName) 
            {
                  if (itemName.Length <= 0) return false;
                  if (null == _mapScaleItems) return false;
                  
                  try 
                  {
                        bool isInList = _mapScaleItems.Contains(itemName);
                        return isInList;
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::IsInList" + exc.ToString());
                        return false;
                  }
            }
 
            private bool IsEnabled(IMap pMap) 
            {
                  try 
                  {
                        bool bEnabled = false;
                  
                        if (null == pMap) return bEnabled;
                  
                        //now do the selecting
                        UID pUID = new UIDClass();
                        if (null == pUID) return bEnabled;
                        //UID for IGeoFeatureLayer
                        pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
                  
                        IEnumLayer pEnumLayers = null;
                        try 
                        {
                              pEnumLayers = pMap.get_Layers(pUID, true);
                        }
                        catch 
                        {
                              return bEnabled;
                        }
                        if (null == pEnumLayers) return bEnabled;
                        pEnumLayers.Reset();
 
                        IFeatureLayer pFLayer = null;             
                        ILayer pLayer = pEnumLayers.Next();
                        while (null != pLayer) 
                        {
                              pFLayer = pLayer as IFeatureLayer;
                              if (null != pFLayer) 
                              {
                                    bEnabled = true;
                                    break;
                              }
 
                              pLayer = pEnumLayers.Next();
                        }
 
                        return bEnabled;
                  }
                  catch(Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::IsEnabled" + exc.ToString());
                        return false;
                  }
            }
 
            private double FormatScaleItemDouble(string itemName)
            {
                  if (itemName.Length <= 0) return -1;
 
                  try 
                  {
                        string formattedString = "";
 
                        string delimStr = ":";
                        char [] delimiter = delimStr.ToCharArray();
                        string [] splitItemName = null;
                        splitItemName = itemName.Split(delimiter);
                        if (splitItemName.Length == 1) 
                        {
                              formattedString = itemName;
                        }
                        else 
                        {
                              formattedString = splitItemName[1];
                        }
 
                        double formattedScale = -1.0;
 
                        try 
                        {
                              formattedScale = Convert.ToDouble(formattedString);
                        }
                        catch 
                        {
                              formattedScale = -1.0;
                        }
 
                        return formattedScale;
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::FormatScaleItemDouble" + exc.ToString());
                        return -1;
                  }
            }
 
            private string FormatScaleItemString(string itemName)
            {
                  if (itemName.Length <= 0) return "";
 
                  try 
                  {
                        double testScale = FormatScaleItemDouble(itemName);
                        if (testScale <= 0) return "";
 
                        string formattedString = "";
 
                        string delimStr = ":";
                        char [] delimiter = delimStr.ToCharArray();
                        string [] splitItemName = null;
                        splitItemName = itemName.Split(delimiter);
                        if (splitItemName.Length == 2) 
                        {
                              if (splitItemName[0].Length > 0) 
                              {
                                    formattedString = itemName;
                              }
                              else 
                              {
                                    formattedString = "1:" + splitItemName[1];
                              }
                        }
                        else 
                        {
                              formattedString = "1:" + itemName;
                        }
 
                        return formattedString;
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::FormatScaleItemString" + exc.ToString());
                        return "";
                  }
            }
                        #endregion Private Methods
 
            #region Event Handlers
//14
            private void MainArcEngineApp_CurrentMapChanged(object send, Miner.Windows.ArcGIS.CurrentMapChangedEventArgs e)
            {
                  //_buildList = true;
                  this.Text = "";
                  bool checkEnabledStatus = Enabled;
                  
                  if (checkEnabledStatus == false) return;
 
                  IMap currentMap = MainArcEngineApp.FocusMap();
                  if (null == currentMap) return;
                        
                  IActiveView activeView = currentMap as IActiveView;
                  if (null == activeView) return;
 
 
                  IActiveViewEvents_Event _activeViewEvents = activeView as IActiveViewEvents_Event;
                  _activeViewEvents.AfterDraw +=new IActiveViewEvents_AfterDrawEventHandler(activeViewEvents_AfterDraw);
            }
//15
            private void activeViewEvents_AfterDraw(Esri.ArcGIS.Display.IDisplay Display, esriViewDrawPhase phase)
            {
                  try 
                  {
                        _addToList = false;
                        IMap currentMap = MainArcEngineApp.FocusMap();
                        if (null == currentMap) return;
                        try //this is necessary when opening a new tab
                        {
                              double dummy = currentMap.MapScale;
                        }
                        catch 
                        {
                              return;
                        }
                        string formattedMapScale = FormatScaleItemString(Convert.ToString(Convert.ToInt32(currentMap.MapScale)));
                        try 
                        {
                              this.Text = formattedMapScale;
                        }
                        catch 
                        {
                              return;
                        }
                  }
                  catch (Exception exc) 
                  {
                        MessageBox.Show("MapZoomControl::activeViewEvents_AfterDraw" + exc.ToString());
                        return;
                  }
                  finally 
                  {
                        _addToList = true;
                  }
            }
 
 
            #endregion
      }
}
 

 

Return to Custom Commands and Controls

 

 


Send Comment to ArcFMdocumentation@schneider-electric.com