ArcFM Engine Developer Guide
MemoEditor.cs Code

Resource Center Home

Field Editor Developer Sample (C#)

MemoEditor.cs

 

Copy Code
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using Miner.ComCategories;
using Miner.Interop;
namespace DotNetCustomFieldEditor
{
 
 [ComponentCategory(ComCategory.MMCustomFieldEditor)]
 [ComponentCategory(ComCategory.MMCustomFieldEditorInPlace)]
 
 [ComVisible(true)]
 [Guid("26CDD370-BBF8-43F0-AA59-4D5BC31C9D03")]
 public class MemoEditCustomFieldEditor : UserControl, IMMCustomFieldEditor
 {
     private const string _NullDisplayText = "<Null>";
     private const string _ErrorDisplayText = "<Error>";
     private IMMFieldAdapter _field;
  private mmDisplayMode _mode;
  private bool _allowSaveInput;
  private TextBox textBox1;
  /// <summary> 
  /// Required designer variable.
  /// </summary>
  private Container components = null;
  public MemoEditCustomFieldEditor()
  {
   // This call is required by the Windows.Forms Form Designer.
   InitializeComponent();
   // TODO: Add any initialization after the InitializeComponent call
  }
  /// <summary> 
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Component Designer generated code
  /// <summary> 
  /// Required method for Designer support - do not modify 
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   // 
   // textBox1
   // 
   this.textBox1.AutoSize = false;
   this.textBox1.BackColor = System.Drawing.SystemColors.Window;
   this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.textBox1.Location = new System.Drawing.Point(0, 0);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(168, 16);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
   this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
   this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
   this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
   // 
   // UserControl1
   // 
   this.Controls.Add(this.textBox1);
   this.Name = "UserControl1";
   this.Size = new System.Drawing.Size(168, 16);
   this.ResumeLayout(false);
  }
  #endregion
  /// <summary>
  /// Save field value from the control.
  /// </summary>
  private void Save()
  {
   if (_allowSaveInput && (_field != null))
   {
    string value = textBox1.Text;
    if (string.IsNullOrEmpty(value) ||
     (string.Compare(value, _NullDisplayText, true) == 0) ||
     (string.Compare(value, _ErrorDisplayText, true) == 0)
     )
    {
     _field.Value = DBNull.Value;
    }
    else
    {
     _field.Value = textBox1.Text;
    }
   }
  }
  /// <summary>
  /// Read field value and display in control.
  /// </summary>
  private void Read()
  {
   textBox1.Text = string.Empty;
   if (_field != null)
   {
    if ((_field.Value == null) || (_field.Value == DBNull.Value))
    {
     textBox1.Text = _NullDisplayText;
    }
    else if (_field.Value != null)
    {
     if (_field.Value is String)
     {
      textBox1.Text = Convert.ToString(_field.Value);
     }
     else
     {
      textBox1.Text = _ErrorDisplayText;
     }
    }
   }
  }
  #region IMMCustomFieldEditor Members
  mmCustomFieldEditorType IMMCustomFieldEditor.EditorType
  {
   get
   {
    return mmCustomFieldEditorType.mmCFEField;
   }
  }
  esriFieldType[] IMMCustomFieldEditor.SupportedFieldTypes
  {
   get
   {
    return new esriFieldType[] { esriFieldType.esriFieldTypeString };
   }
  }
  string IMMCustomFieldEditor.Caption
  {
   get
   {
    return null; // use default
   }
  }
  string IMMCustomFieldEditor.Name
  {
   get
   {
    return "Sample ArcFM Memo Editor";
   }
  }
  void IMMCustomFieldEditor.InitEditor(IMMFieldAdapter pFA, mmDisplayMode eMode)
  {
   _field = pFA;
   _mode = eMode;
  }
  void IMMCustomFieldEditor.ActivateEditor()
  {
   Read();
   _allowSaveInput = true;
  }
  void IMMCustomFieldEditor.DeactivateEditor()
  {
   _field = null;
  }
  void IMMCustomFieldEditor.Commit()
  {
   
  }
  void IMMCustomFieldEditor.Refresh()
  {
   Read();
  }
  #endregion
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  {
  
  }
  private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
            // Note: While you will receive the 'enter' key as expected in the
            // ArcFM attribute editor, this may not be true in the ArcFM locator.
            // This is believed to be a limitation of embedding ActiveX controls 
            // in a WinForm container.
   if (e.KeyCode == Keys.Enter)
   {
    e.Handled = true;
    Save();
       CloseFieldEditor();
   }
  }
     private void textBox1_Leave(object sender, EventArgs e)
  {
   Save();
  }
  private void textBox1_Enter(object sender, EventArgs e)
  {
   Read();
            textBox1.SelectAll();
  }
     /// <summary>
     /// This will force the custom field editor to be closed (hidden).
     /// </summary>
     /// <remarks>
     /// When you press 'enter' on the built-in editors the editor
     /// will be closed. However, for custom editors this will only occur
     /// if the field value actually changes because the OnValueChanged
     /// event will only fire if the value actually changes. In order to
     /// make the custom field editor "go away" when prettying 'enter' you
     /// can manually fire the event.
     /// </remarks>
     private void CloseFieldEditor()
     {
         if (_field != null)
         {
             ID8List containingList = ((ID8ListItem)_field).ContainedBy;
             IMMFieldManagerEvents events = containingList as IMMFieldManagerEvents;
             if (events != null)
             {
                 // Firing the "OnValueChanged" event on the field manager will
                 // cause the object editor to cleanup the active field editor.
                 // This will hide the custom field editor.
                 events.OnValueChanged();
             }
         }
     }
 }
}

 

 


Send Comment to ArcFMdocumentation@schneider-electric.com