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 am trying to get the user to draw a line (only 2 vertices) on the screen (just like the georeferencing link/spatial adjustment link).

I got the below snippet from Esri website which does give me a polyline but it requires the user to double click to finish the sketch.

public IPolyline GetPolylineFromMouseClicks(IActiveView activeView)

          IScreenDisplay screenDisplay = activeView.ScreenDisplay;

          IRubberBand rubberBand = new RubberLineClass();
          IGeometry geometry = rubberBand.TrackNew(screenDisplay, null);

          IPolyline polyline = (IPolyline)geometry;

          return polyline;


I would like the tool to finish the drawing as soon as two points are clicked, thus avoiding a double click.

Can somebody help me on this?

share|improve this question
up vote 4 down vote accepted

I don't think you can use the IRubberBand interface. Once you start the TrackNew method you lose control of your calling tool because it requires a double-click to finish it's process.

Here is a solution using a custom tool:

    private bool _isFirstClick;
    private ESRI.ArcGIS.Display.INewLineFeedback _lineFeedback;
    private IActiveView _focusMap;
    private IApplication _application;
    private IMxDocument _mxDoc;

    public override void OnCreate(object hook)
        _application = hook as IApplication;

        //Disable if it is not ArcMap
        if (hook is IMxApplication)
            base.m_enabled = true;
            base.m_enabled = false;

    public override void OnMouseDown(int Button, int Shift, int X, int Y)
        _mxDoc = (IMxDocument) _application.Document;
        _focusMap = mxDoc.FocusMap as IActiveView;

        IPoint point = _focusMap.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y) as IPoint;

        if (_lineFeedback == null)
            _lineFeedback = new ESRI.ArcGIS.Display.NewLineFeedback();
            _lineFeedback.Display = _focusMap.ScreenDisplay;
            _isFirstClick = true;
            _isFirstClick = false;

    public override void OnMouseUp(int Button, int Shift, int X, int Y)

        if (_isFirstClick) return;

        IPolyline polyline = _lineFeedback.Stop();

        //Do something with the polyline here

        _lineFeedback = null;

After the second click you will have a reference to your two-point polyline. You then need to create a function to draw it on the map, create a feature from it, or whatever you are trying to do.

I tested this in ArcGIS 10, not 9.3.1 so let me know if it doesn't work for you.

share|improve this answer

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.