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);
}
}
}