0

While writing the code for adding all the layers in arcmap into combobox of a form, I am getting the error:

'Bestaurents.frmRestaurentviewer' does not contain a definition for cmblayer and no extention method 'cmblayer' accepting a first argument of type Bestaurents.frmRestaurentviewe could not be found (are you missing a using directive or an assembly)

at last line:

frmRestaurentViewer prestViewer = new frmRestaurentViewer();
prestViewer.Show();

IMxDocument doc = m_application.Document as IMxDocument;
IMap pMap = doc.FocusMap;

ILayer play = pMap.get_Layer(0);
prestViewer.cmblayer.Items.Add(play.Name);
  • 1
    I think we're going to need to see more code than this, such as frmRestaurentViewer(). cmblayer is apparently not being set in the construction of the object. – Thomas Oct 6 '15 at 16:21
1

When your form is opened and then closed then your combo box cmblayer is disposed. so it is null.

To solve this situation, handle your from load event (create a method and event listener) and put your codes there:

private void frmRestaurentViewer_OnFormLoadEvent()
{
IMxDocument doc = m_application.Document as IMxDocument;
IMap pMap = doc.FocusMap;

ILayer play = pMap.get_Layer(0);
prestViewer.cmblayer.Items.Add(play.Name);
}
1

This code block will only pull items that are currently in your table of contents:

/// <summary> Returns a list of feature layers based on the geometry type </summary>
    /// <param name="geometryType">Geometry type.  Expected values "Point", "Line" or "Polygon".</param>
    /// <returns>Arraylist</returns>
    public ArrayList FeatureLayers(string geometryType)
    {
        ArrayList pList = new ArrayList();
        if (FocusMap == null)
            return pList;
        if (FocusMap.LayerCount == 0)
            return pList;

        try
        {
            UID pID = new UIDClass();
            pID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; //GeoFeatureLayer
            IEnumLayer pEnumLayer = FocusMap.get_Layers(pID, true);
            pEnumLayer.Reset();
            ILayer pLayer = pEnumLayer.Next();
            while (!(pLayer == null))
            {
                if (pLayer is IFeatureLayer)
                {
                    IFeatureLayer featurelayer = (IFeatureLayer)pLayer;
                    if (string.Compare(geometryType, "Point", true) == 0)
                    {
                        if ((featurelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint) || (featurelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint))
                            pList.Add(pLayer.Name);
                    }
                    if (string.Compare(geometryType, "Line", true) == 0)
                    {
                        if ((featurelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline) || (featurelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryLine))
                            pList.Add(pLayer.Name);
                    }
                    if (string.Compare(geometryType, "Polygon", true) == 0)
                    {
                        if (featurelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                            pList.Add(pLayer.Name);
                    }
                }
                pLayer = pEnumLayer.Next();
            }
            return pList;
        }
        catch (Exception ex)
        {
            if (SupressMessaging == false)
                MessageBox.Show(ex.Message, "All Layers", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        return pList;
    }

Here is an example of the method being implemented

private void LoadComboBox()
    {
        _application = this.Hook as IApplication;
        _mxdocument = (IMxDocument)_application.Document;
        _map = _mxdocument.FocusMap;
        _activeView = _mxdocument.ActivatedView;

        comboBox.Items.Clear();

        this.comboBox.Items.AddRange(FeatureLayers("Polygon").ToArray());
        if (this.comboBox.Items.Count > -1) this.comboBox.SelectedIndex = 0;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.