Events provided by MapBox

The MapBox control supplies a set of custom events in additon to those it derives because of inheritance from System.Windows.Forms.Control. You can create a custom tool by handling any of these events.

Event Delegate Description
MouseMove MouseEventHandler Fires when mouse moves over the map
MouseDown MouseEventHandler Fires when the map receives a mouse click
MouseUp MouseEventHandler Fires when mouse is released
MouseDrag MouseEventHandler Fires when mouse is dragging
MapRefreshed EventHandler Fires when map has been refreshed
MapChanging CancelEventHandler Fires when map is about to change
MapChanged EventHandler Fires when map has changed
MapZoomChanged MapZoomHandler Fires when the map zoom was changed
MapZooming MapZoomHandler Fires when the map zoom is changing
MapQueried MapQueryHandler Fires for each layer selected for query. This is depending on QueryLayerIndex and QuerySettings
MapQueryStarted EventHandler Fires when a query request has started
MapQueryDone EventHandler Fires when a query request has been finished
MapCenterChanged MapCenterChangedHandler Fires when the map's center has changed
ActiveToolChanged ActiveToolChangedHandler Fires when one of MapBox' builtin tools has been selected
GeometryDefined GeometryDefinedHandler Fires when a new geometry has been defined

A handy base class

The following base class provides the initial means for that:
public abstract class CustomMapTool
{
    private MapBox _mapControl;
    private bool _enabled;

    /// <summary>
    /// Event raised when the <see cref="MapControl"/> is about to change.
    /// </summary>
    public event CancelEventHandler MapControlChanging;

    /// <summary>
    /// Event invoker for the <see cref="MapControlChanging"/> event
    /// </summary>
    /// <remarks>Override this method to unwire <see cref="MapBox"/>s events. Don't forget to call <c>base.OnMapControlChanging(cea);</c> to invoke the event.</remarks>
    /// <param name="cea">The cancel event arguments</param>
    protected virtual void OnMapControlChanging(CancelEventArgs cea)
    {
        var h = MapControlChanging;
        if (h != null) h(this, cea);
    }

    /// <summary>
    /// Event raised when the <see cref="MapControl"/> has changed.
    /// </summary>
    public event EventHandler MapControlChanged;

    /// <summary>
    /// The event invoker 
    /// </summary>
    /// <remarks>Override this method to wire to <see cref="MapBox"/>s events. Don't forget to call <c>base.OnMapControlChanged(e);</c> to invoke the event.</remarks>
    /// <param name="e">The event arguments</param>
    protected virtual void OnMapControlChanged(EventArgs e)
    {
        var h = MapControlChanged;
        if (h != null) h(this, e);
    }

    /// <summary>
    /// Gets or sets a value indicating the map control
    /// </summary>
    public MapBox MapControl
    {
        get { return _mapControl; }
        set
        {
            if (value == _mapControl)
                return;

            var cea = new CancelEventArgs(false);
            OnMapControlChanging(cea);
            if (cea.Cancel) return;
            _mapControl = value;
            OnMapControlChanged(EventArgs.Empty);
        }
    }

    /// <summary>
    /// Event raised when <see cref="Enabled"/> changed.
    /// </summary>
    public event EventHandler EnabledChanged;

    /// <summary>
    /// Event invoker for the <see cref="EnabledChanged"/> event.
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnEnabledChanged(EventArgs e)
    {
        var h = EnabledChanged;
        if (h != null) h(this, e);
    }

    /// <summary>
    /// Gets or sets a value indicating if the tool is enabled or not
    /// </summary>
    public bool Enabled
    {
        get { return _mapControl != null && _enabled; }
        set
        {
            if (value == _enabled)
                return;
            _enabled = value;
            OnEnabledChanged(EventArgs.Empty);
        }
    }
}

This abstract map tool does nothing but expose
  • the MapControl it belongs to
  • a value indicating if the tool is enabled or not.
and raises the apropriate events if any of these properties change.

A simple popup tool

The following piece of code defines a simple popup tool:
public class PopupTool : CustomMapTool
{
    protected override void OnMapControlChanging(CancelEventArgs cea)
    {
        base.OnMapControlChanging(cea);
        if (cea.Cancel) return;

        if (MapControl == null) return;
        MapControl.MouseDown -= HandleMouseDown;
    }

    protected override void OnMapControlChanged(EventArgs e)
    {
        base.OnMapControlChanged(e);
        if (MapControl == null) return;
        MapControl.MouseDown += HandleMouseDown;
    }

    private void HandleMouseDown(Coordinate worldpos, MouseEventArgs imagepos)
    {
        if (!Enabled)
            return;

        MessageBox.Show(string.Format("You clicked at {0}!", worldpos));
        Enabled = false;
    }
}

Wiring it all together

To wire the popup control to the mapbox control you can override the Forms OnLoad method and add the following lines of code:
protected override void OnLoad(EventArgs e)
{
    var pt = new PopupTool();
    pt.MapControl = _mapControl;
    pt.Enabled = true;
}

Last edited Jan 8, 2015 at 7:44 AM by FObermaier, version 4