unable to deal with hook connection

429
6
08-11-2010 10:10 PM
YashikaSareen
New Contributor
Hi every1,
I am stuck up with a problem.I want to create an application in which on the click event of the tool a form should be displayed which will be having a button.On the click event of this button,it should print the name of the first layer in the arcmap.The problem is the file through which we hook up the application returns null when the click event of button is called.It shows an exception of unreferenced object instance.below is the code.Please rectify whereever wrong.

Tool.cs
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;



namespace Toolforselection
{
    [Guid("cbf31922-bf3c-487d-891f-1661c078e853")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Toolforselection.Tool")]
    public class Tool : ICommand
    {
        private IHookHelper m_hookHelper = null;
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        static extern bool DeleteObject(IntPtr hObject);

        #region "Component Category Registration"
        [ComRegisterFunction()]
        static void Reg(string regKey)
        {
            MxCommands.Register(regKey);
        }

        [ComUnregisterFunction()]
        static void Unreg(string regKey)
        {
            MxCommands.Unregister(regKey);
        }
        #endregion

        public ILayer l1;
        public ILayer l2;
        public IMxApplication mxApplication;  //to create application display object

        IApplication m_application;

        private IntPtr m_hBitmap;
        public IApplication m_app1;
        public IApplication m_app;
        public IMxDocument m_mxDoc;
        public MxDocument m_mxDocument;
        public Map m_map;
        public PageLayout m_pageLayout;
        private System.Windows.Forms.Button button1;
        public IFeatureClass featureClass;
        private bool m_enabled;
       
            #region ICommand Members

        public string Message
        {
            get
            {
                // Set the message string that appears in the statusbar of the
                // application when the mouse passes over the command.
                return "secondtool";
                //return null;
            }

        }
        public int HelpContextID
        {
            get
            {
                return 0;
            }
        }

        public string Name
        {
            get
            {

                return "sample tool";
                //return null;
            }
        }


        public void OnClick()
        {
            Form1 f = new Form1();
            f.Show();
        }
          public int Bitmap
        {
            get
            {
                return m_hBitmap.ToInt32();

            }
        }
        public void OnCreate(object hook)
        {
            m_app = hook as IApplication;
            m_mxDoc = m_app.Document as IMxDocument;
            m_mxDocument = m_app.Document as MxDocument;
            m_map = m_mxDoc.FocusMap as Map;
        //    try
        //    {
        //        m_hookHelper = new HookHelperClass();
        //        m_hookHelper.Hook = hook;
 
        //        if (m_hookHelper.ActiveView == null)
        //            m_hookHelper = null;
        //    }
        //    catch
        //    {
        //        m_hookHelper = null;
        //    }
        //    if (m_hookHelper == null)
        //       m_enabled = false;
        //    else
        //       m_enabled = true;


        }


        public string Caption
        {
            get
            {

                return "ToolForSelection";
                //return null;
            }
        }

        public string Tooltip
        {
            get
            {
                // Set the string that appears in the screen tip.
                return "ToolForSelection";
                //return null;
            }
        }
        public bool Checked
        {
            get
            {
                return false;
            }
        }

        public bool Enabled
        {
            get
            {
                // Add some logic here to specify when the command should
                // be enabled. In this example, the command is always enabled.
                return true;
            }
        }
        public string HelpFile
        {
            get
            {
                return null;
            }
        }

        public string Category
        {
            get
            {
                // Set the category of this command. This determines where the
                // command appears in the Commands panel of the Customize dialog.
                return "test tool";
                //return null;
            }
        }

        #endregion

      

    }
}




Form1.cs                                   //to add form



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;

namespace Toolforselection
{
    public partial class Form1 : Form
    {
       // private IHookHelper hookHelper;
        public ILayer l1;
        public ILayer l2;
        public IMxApplication mxApplication;  //to create application display object
       // IApplication m_application;
        private IntPtr m_hBitmap;
        public IApplication m_app;
        public IMxDocument m_mxDoc;
        public MxDocument m_mxDocument;
        public Map m_map;
        public PageLayout m_pageLayout;
        public IFeatureClass featureClass;


        public Form1()
        {
           // this.hookHelper = hookHelper;
            //MessageBox.Show(m_app.Name);

            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(m_app.Name);
            //MessageBox.Show("abc");

            l1 = m_map.get_Layer(0);              //here m_map has value null(exception)
            MessageBox.Show(l1.Name);
        }
     
   

    }
}
0 Kudos
6 Replies
JanDuske
New Contributor
You are creating an instance of your Form class without passing any reference to the calling ArcMap application. You declare member variables in the Form class for the MxDocument and Application, but you never set them (m_map, m_app...). If you had declared the member variables of your form private instead of public (as it would be correct in an object oriented design approach) you would have gotten a compiler warning telling you that those variables never get a value assigned.
You should try to pass the IApplication object as parameter of your Forms constructor, from that object you can derive all the other references you might need. Set the m_app variable to the Application reference you pass to the constructor, and set all other member variables by using the application's attributes (see documentation for IApplication (ArcMap), IMxDocument etc)
0 Kudos
BradChappell
New Contributor
Here is all you need to do: 

First get rid of all your current code.

Add a public variable of type IMap to your form.

So somewhere in the form do this: 

public IMap TheMap;

In the OnCreate Method of your Tool add this code:

try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                {
                    m_hookHelper = null;
                }
            }
            catch
            {
                m_hookHelper = null;
            }
For some reason you have the above code commented out...

You DO NOT need an IApplication or IMxApplication variables...

On the OnClick event of the Tool put this Code:
YourForm TheForm = new YourForm();
YourForm.TheMap = m_hookHelper.FocusMap;

TheForm.Show();

The code for the Form Load Event within the form should be this:

MessageBox.Show(this.TheMap.Layer(0).Name)
0 Kudos
YashikaSareen
New Contributor
thanks Brad and Jam.Igot it.Its working now.Thanks for your help.
byee.
0 Kudos
GaryBushek
New Contributor III
Is there another way to detect the ActiveView for the map without using IHookHelper?  IHookHelper is available for ArcEngine so you can use it with Arc Desktop development if you have ArcEngine SDK installed and the ESRI.ArcGIS.Controls dll is available.  This was my case with 9.3 but now that I've migrated to ArcGIS 10 I only have Arc Desktop SDK available.

Thanks
0 Kudos
Venkata_RaoTammineni
Occasional Contributor
Is there another way to detect the ActiveView for the map without using IHookHelper?  IHookHelper is available for ArcEngine so you can use it with Arc Desktop development if you have ArcEngine SDK installed and the ESRI.ArcGIS.Controls dll is available.  This was my case with 9.3 but now that I've migrated to ArcGIS 10 I only have Arc Desktop SDK available.

Thanks


Use IApplication for hooking Desktop..
0 Kudos
AlexanderGray
Occasional Contributor III
The ArcGIS Engine SDK and the ArcGIS desktop SDK are identical.  If you look at the installer, it is the exact same file.  In ArcGIS 10 I looks like esri combined all the SDKs together.  I am not sure why the help says it is ArcGIS Engine only.  In VS2008 if you add a universal command (base command + universal), it adds the references to ESRI.ArcGIS.Controls and instantiates the hookhelperclass.
0 Kudos