MapTool Detect Clear Selection Click

1111
7
Jump to solution
07-24-2018 07:59 AM
MKa
by
Occasional Contributor III

I have a MapTool that will only be activated (OnToolActivateAsync) when one, and only one feature is selected.  Then when the user finishes their sketch, the one and only selected item will have some work done to it (OnSketchCompleteAsync).

The problem that I find, is that after you activate the tool, if you click on the Clear Selection button, my map tool doesn't deactivate, so the validation of a selected item is no longer valid.  i would either like my tool to deactivate when the Clear button is pressed, or have a way to catch the button being clicked from my maptool, and have the validation rerun.  I have tried OnSelectionChangedAsync but that doesn't seem to catch the clear selection, as my tool is a sketch tool. 

Here is my Activate Function that has the line to CheckForValidSelection.  But after the tool is activated, I have no way of catching the selection clear button.

protected async override Task OnToolActivateAsync(bool hasMapViewChanged)
        {
            try
            {
                //this checks the map for only one feature selected and warns user if failed
                bool validCheck = await CheckForValidSelection();

                //Exist if one thing and one thing only is not selected
                if (!validCheck) return;

                //Setup the line symbol/snapping/tracing
                if (_lineSymbol == null)
                {
                    _lineSymbol = await CreateLineSymbolAsync();
                }

                SketchSymbol = _lineSymbol.MakeSymbolReference();

                //General Snapping
                Snapping.IsEnabled = true;
                Snapping.SetSnapMode(SnapMode.Edge, true);
                Snapping.SetSnapMode(SnapMode.End, true);
                Snapping.SetSnapMode(SnapMode.Intersection, true);

                //Set the command to Trace, possible bug as we have to set it then set it again
                ICommand _trace = FrameworkApplication.GetPlugInWrapper("esri_editing_TraceConstructor") as ICommand;
                _trace.Execute(null);
                _trace = FrameworkApplication.GetPlugInWrapper("esri_editing_LineConstructor") as ICommand;
                _trace.Execute(null);
                _trace = FrameworkApplication.GetPlugInWrapper("esri_editing_TraceConstructor") as ICommand;
                _trace.Execute(null);
            }
            catch (Exception e)
            {
                LogError("OnToolActivateAsync", e);
            }
        }
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi

The only way to deactivate a tool is by activating another tool. When the tool is active, you can subscribe to the MapSelectionChangedEvent. When the selection changes, check for your tool validity criteria and activate another tool like the default Pro Explore tool. This will deactivate your tool.  Unsubscribe to the MapSelectionChangedEvent in the OnToolDeactivateAsync override of the tool.

Here is a code snippet:

protected override Task OnToolActivateAsync(bool active)
    {
      MapSelectionChangedEvent.Subscribe(OnMapSelectionChanged);

     // do other things here

      return base.OnToolActivateAsync(active);
    }

    protected override Task OnToolDeactivateAsync(bool hasMapViewChanged)
    {
      MapSelectionChangedEvent.Unsubscribe(OnMapSelectionChanged);
      return base.OnToolDeactivateAsync(hasMapViewChanged);
    }

    private void OnMapSelectionChanged(MapSelectionChangedEventArgs args)
    {
      var selection = args.Selection;
      // set explore tool active if selection is cleared
      //   this will force a deactivate of this tool
      if (selection.Count() == 0) //your selection criteria
        FrameworkApplication.SetCurrentToolAsync("esri_mapping_exploreTool"); //deactivates this maptool.
    }

Thanks!

Uma

View solution in original post

7 Replies
UmaHarano
Esri Regular Contributor

Hi

The only way to deactivate a tool is by activating another tool. When the tool is active, you can subscribe to the MapSelectionChangedEvent. When the selection changes, check for your tool validity criteria and activate another tool like the default Pro Explore tool. This will deactivate your tool.  Unsubscribe to the MapSelectionChangedEvent in the OnToolDeactivateAsync override of the tool.

Here is a code snippet:

protected override Task OnToolActivateAsync(bool active)
    {
      MapSelectionChangedEvent.Subscribe(OnMapSelectionChanged);

     // do other things here

      return base.OnToolActivateAsync(active);
    }

    protected override Task OnToolDeactivateAsync(bool hasMapViewChanged)
    {
      MapSelectionChangedEvent.Unsubscribe(OnMapSelectionChanged);
      return base.OnToolDeactivateAsync(hasMapViewChanged);
    }

    private void OnMapSelectionChanged(MapSelectionChangedEventArgs args)
    {
      var selection = args.Selection;
      // set explore tool active if selection is cleared
      //   this will force a deactivate of this tool
      if (selection.Count() == 0) //your selection criteria
        FrameworkApplication.SetCurrentToolAsync("esri_mapping_exploreTool"); //deactivates this maptool.
    }

Thanks!

Uma

MKa
by
Occasional Contributor III

Thank you.  That worked great.  The only change I made to activate the Rectangle Selection tool instead of the explore tool.  Also, why cant I post code in these replies?  We don't have the option to show syntax?

private async void OnMapSelectionChanged(MapSelectionChangedEventArgs args)
{
   //Any Selection change will need you to deactivate the tool and activate the Rectangle select tool
   await FrameworkApplication.SetCurrentToolAsync("esri_mapping_selectByRectangleTool");
}

0 Kudos
KenBuja
MVP Esteemed Contributor

When you use "Reply to original question" in the Inbox, you don't get the option of using the Syntax Highlighter. Click on the title of the discussion and reply there to use the highlighter.

Wolf
by Esri Regular Contributor
Esri Regular Contributor

Actually the 'SyntaxHighlighter' feature is available, but not as a default menu selection.  You first click the "..." menu option on your reply, then you click the 'More' pull-down and select the 'SyntaxHightlighter' from the drop-down menu and you can add you code snippet.  It took me a while to remember this 😉

namespace ProAppModule1
{
  internal class Button1 : Button
  {
    protected override void OnClick()
    {
      MessageBox.Show(@"Hello world!");
    }
  }
}
0 Kudos
KenBuja
MVP Esteemed Contributor

The "three dot" menu item doesn't appear when you reply through the Inbox (shown here)

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I usually use the links I get through my notification emails: I get two 'inbox' links, and yes you are correct only one works as it provides the '...' menu option.  

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I usually don't use that workflow path.  I reported the issue to the Geonet team.

0 Kudos