Hello,
I am trying to add a layer to a shapefile. My code is currently very similar to what is provided by ESRI:
However, I cannot get the "m_application" reference to work. The instructions provided say that this code "assumes that you already have m_application variable as a reference to the Application object in your .NET custom component". What does this mean? How do I make this reference?
Below is my code FWIW. It essentially does the same as above except passes a shapefile chosen from an Add Data dialog that I created from IgxObject.
I hope this has a super simple answer. I'm pretty new at this :)
I am trying to add a layer to a shapefile. My code is currently very similar to what is provided by ESRI:
Code:
private void AddShapeFile()
{
// Create a new ShapefileWorkspaceFactory object and
// open a shapefile folder - the path works with standard 9.3 installation
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)
workspaceFactory.OpenFromFile(
"C:\\Program Files\\ArcGIS\\DeveloperKit\\SamplesNET\\data\\Y2000HurricaneData", 0);
// Create a new FeatureLayer and assign a shapefile to it
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("2000_hrcn");
ILayer layer = (ILayer)featureLayer;
layer.Name = featureLayer.FeatureClass.AliasName;
// Add the Layer to the focus map
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument =
(ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document);
IMap map = mxDocument.FocusMap;
map.AddLayer(layer);
}Below is my code FWIW. It essentially does the same as above except passes a shapefile chosen from an Add Data dialog that I created from IgxObject.
Code:
private void btnOpenShapefile_Click(object sender, EventArgs e)
{
ESRI.ArcGIS.Catalog.IGxObjectFilterCollection pGxFilter; //establish a collection of filters
ESRI.ArcGIS.Catalog.GxFilterShapefiles pfilter2; //create a filter
Boolean notanythingselected;
ESRI.ArcGIS.Catalog.IGxObject gxObj; //declare an instance of GxObjects
ESRI.ArcGIS.CatalogUI.IGxDialog pGxDia; //declare an instance of GxDialogs
pfilter2 = new ESRI.ArcGIS.Catalog.GxFilterShapefiles(); //create a filter instance
pGxDia = new GxDialogClass(); //new dialog box object
pGxDia.Title = "Choose a Shapefile";
pGxFilter = (ESRI.ArcGIS.Catalog.IGxObjectFilterCollection)pGxDia;
pGxFilter.AddFilter(pfilter2, true);
ESRI.ArcGIS.Catalog.IEnumGxObject gxEnum = null;
notanythingselected = pGxDia.DoModalOpen(this.Handle.ToInt32(), out gxEnum);
if (notanythingselected == false)
{
return;
}
else
{
gxEnum.Reset();
gxObj = gxEnum.Next();
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wksFact;
wksFact = new ShapefileWorkspaceFactory();
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featWrk;
featWrk = (IFeatureWorkspace)wksFact.OpenFromFile(gxObj.Parent.FullName, 0);
ESRI.ArcGIS.Geodatabase.IFeatureClass fClass;
fClass = featWrk.OpenFeatureClass(gxObj.Name);
ESRI.ArcGIS.Carto.IFeatureLayer lyr;
lyr = new FeatureLayer();
lyr.FeatureClass = fClass;
lyr.Name = gxObj.Name;
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDoc = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document); //ERROR IS HERE AT M_application
IMap map = mxDoc.FocusMap;
map.AddLayer(lyr);