ArcObjects Library Reference  

CreateNewDocument

About the Save a layer file in a MapControl application Sample

[C#]

CreateNewDocument.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SystemUI;

namespace MapControlSaveLayerFile
{
  /// <summary>
  /// Summary description for CreateNewDocument.
  /// </summary>
  public class CreateNewDocument : BaseCommand
  {
    private IHookHelper m_hookHelper = null;

    //Ctor
    public CreateNewDocument()
    {
      //update the base properties
      base.m_category = ".NET Samples";
      base.m_caption = "NewDocument";
      base.m_message = "Create a new map";
      base.m_toolTip = "Create a new map";
      base.m_name = base.m_category + "_" + base.m_caption;
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this command is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      if (m_hookHelper == null)
        m_hookHelper = new HookHelperClass();

      m_hookHelper.Hook = hook;
    }

    /// <summary>
    /// Occurs when this command is clicked
    /// </summary>
    public override void OnClick()
    {
      IMapControl3 mapControl = null;

      //get the MapControl from the hook in case the container is a ToolbatControl
      if (m_hookHelper.Hook is IToolbarControl)
      {
        mapControl = (IMapControl3)((IToolbarControl)m_hookHelper.Hook).Buddy;
      }
      //In case the container is MapControl
      else if (m_hookHelper.Hook is IMapControl3)
      {
        mapControl = (IMapControl3)m_hookHelper.Hook;
      }
      else
      {
        MessageBox.Show("Active control must be MapControl!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
      }

      //allow the user to save the current document
      DialogResult res = MessageBox.Show("Would you like to save the current document?", "AoView", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
      if (res == DialogResult.Yes)
      {
        //launch the save command (why work hard!?)
        ICommand command = new ControlsSaveAsDocCommandClass();
        command.OnCreate(m_hookHelper.Hook);
        command.OnClick();
      }

      //create a new Map
      IMap map = new MapClass();
      map.Name = "Map";

      //assign the new map to the MapControl
      mapControl.DocumentFilename = string.Empty;
      mapControl.Map = map;
    }

    #endregion
  }
}

[Visual Basic .NET]

CreateNewDocument.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.SystemUI

  ''' <summary>
  ''' Summary description for CreateNewDocument.
  ''' </summary>
  Public Class CreateNewDocument : Inherits BaseCommand
	Private m_hookHelper As IHookHelper = Nothing

	'Ctor
	Public Sub New()
	  'update the base properties
	  MyBase.m_category = ".NET Samples"
	  MyBase.m_caption = "NewDocument"
	  MyBase.m_message = "Create a new map"
	  MyBase.m_toolTip = "Create a new map"
	  MyBase.m_name = MyBase.m_category & "_" & MyBase.m_caption
	End Sub

#Region "Overridden Class Methods"

    ''' <summary>
    ''' Occurs when this command is created
    ''' </summary>
    ''' <param name="hook">Instance of the application</param>
    Public Overrides Sub OnCreate(ByVal hook As Object)
        If m_hookHelper Is Nothing Then
            m_hookHelper = New HookHelperClass()
        End If

        m_hookHelper.Hook = hook
    End Sub

    ''' <summary>
    ''' Occurs when this command is clicked
    ''' </summary>
    Public Overrides Sub OnClick()
        Dim mapControl As IMapControl3 = Nothing

        'get the MapControl from the hook in case the container is a ToolbatControl
        If TypeOf m_hookHelper.Hook Is IToolbarControl Then
            mapControl = CType((CType(m_hookHelper.Hook, IToolbarControl)).Buddy, IMapControl3)
            'In case the container is MapControl
        ElseIf TypeOf m_hookHelper.Hook Is IMapControl3 Then
            mapControl = CType(m_hookHelper.Hook, IMapControl3)
        Else
            MessageBox.Show("Active control must be MapControl!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        'allow the user to save the current document
        Dim res As DialogResult = MessageBox.Show("Would you like to save the current document?", "AoView", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If res = DialogResult.Yes Then
            'launch the save command (why work hard!?)
            Dim command As ICommand = New ControlsSaveAsDocCommandClass()
            command.OnCreate(m_hookHelper.Hook)
            command.OnClick()
        End If

        'create a new Map
        Dim map As IMap = New MapClass()
        map.Name = "Map"

        'assign the new map to the MapControl
        mapControl.DocumentFilename = String.Empty
        mapControl.Map = map
    End Sub

#End Region
  End Class