Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a custom layer, nothing crazy just press a button and have something show up on the display and also have the layer display in the TOC.

Well, the layer shows in the TOC but nothing shows on the display and the resulting error in my Debug tells me

A first chance exception of type 'System.Runtime.InteropService.ComException' occured in MapControlApplication1.exe

I'm going with fairly straight forward code.

I've created a BaseCommand that ties into the toolbar. When I press the button on the toolbar it successfully goes to Onclick. Here is the meaningful code in the OnClick

CustomLayer2  layer = new CustomLayer2();
layer.Name = "Custom Layer";
mapControl1.AddLayer(layer , 0);

Here is the code for CustomLayer2

public class CustLayer2: BaseCustomLayer 
    private IPolyline m_Polyline = null;

    public override void Draw(esriDrawPhase drawPhase, IDisplay Display, ITrackCancel trackCancel)
        if (drawPhase != esriDrawPhase.esriDPGeography)
        if (m_Polyline == null)
            IProjectedCoordinateSystem pcs = Display.DisplayTransformation.SpatialReference as IProjectedCoordinateSystem;
            if (pcs != null)
                m_Polyline = MakePolyline(pcs.GeographicCoordinateSystem);
                m_Polyline = MakePolyline(new UnknownCoordinateSystemClass());

    public static IPolyline MakePolyline(ISpatialReference sr)
        IPointCollection pc = new PolylineClass();

        object missing = Type.Missing; // (not needed with VB.NET)
        pc.AddPoint(MakePoint(-98.0, 29.0, sr), ref missing, ref missing);
        pc.AddPoint(MakePoint(-97.0, 28.0, sr), ref missing, ref missing);
        pc.AddPoint(MakePoint(-96.0, 27.0, sr), ref missing, ref missing);
        pc.AddPoint(MakePoint(-95.0, 26.0, sr), ref missing, ref missing);

        IPolyline polyline = (IPolyline)pc;
        polyline.SpatialReference = sr;
        return polyline;
    public static IPoint MakePoint(double x, double y, ISpatialReference sr)
        IPoint p = new PointClass();
        p.PutCoords(x, y);
        p.SpatialReference = sr;
        return p;

Any ideas on why this would error out? Note that a new layer is shown in the TOC but obviously the Draw never finishes. The error is occuring on the Display.DrawPolyline((IGeometry)m_Polyline);

share|improve this question
up vote 8 down vote accepted

Before calling Display.DrawPolyline((IGeometry)m_Polyline), call Display.Setsymbol and pass something that implements ILineSymbol, casting it as ISymbol.

share|improve this answer
So now in my TableOfContents I have a CustomLayer Layer. However, there is nothing below it. Shouldn't I be seeing something on the screen? Or am I missing another call? – PSU_Kardi Oct 13 '10 at 19:36
Not sure what you mean by "nothing below it", but if you want any symbols to be visible in the TOC, your custom layer will need to implement ILegendInfo interface. – Petr Krebs Oct 13 '10 at 19:40
That's what I meant...still very green to the ArcGIS stuff. Thanks! – PSU_Kardi Oct 13 '10 at 19:41
Not sure, but I think you'll need to have your custom layer implement ILegendInfo. The easiest way to do that might be to have a member variable with an ISimpleRenderer, and cast it to ILegendInfo and forward all calls to it. – Kirk Kuykendall Oct 13 '10 at 19:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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