Select to view content in your preferred language

Setting focus to Tool / Command

2410
7
09-28-2011 03:09 AM
SimonField
Emerging Contributor
Hi,

I have the following function which works ok when setting the focus to a tool:

   Public Sub CorrectFocus(ByVal p_application As IApplication, ByVal toolname As String)
        Dim pCommand As ESRI.ArcGIS.SystemUI.ICommand
        Dim m_application As IApplication
        Dim curr_tool As String
        curr_tool = toolname
        m_application = p_application
        pCommand = p_application.Document.CommandBars.Find(curr_tool, False, False)
        If pCommand Is Nothing Then
            MsgBox("Could not find tool")
        Else
            m_application.CurrentTool = pCommand
        End If
    End Sub

This works fine when the tool in question (curr_tool) inherits from BaseTool, however I have situations where the 'tool' I need to focus on inherits from BaseCommand and when I carry out the above function in this case, I get an ArgumentException 'Value does not fall in expected range'.

Is there any way I could modify or rewrite the above function so it would re-focus on a tool inheriting from BaseCommand?

Any help appreciated!

Thanks
Simon
0 Kudos
7 Replies
DubravkoAntonic
Occasional Contributor
By ICommandItem.Execute you'll have focus on BaseCommand item. It is a bit unusual way to have focus on ICommand object that is meant to be executed without any user interaction.
Maybe it is a better way for you to create your own custom tool that implements functions of some BaseCommand item, that way you can make focus on it.

What is Use Case that you are trying to implement?
0 Kudos
SimonField
Emerging Contributor
By ICommandItem.Execute you'll have focus on BaseCommand item. It is a bit unusual way to have focus on ICommand object that is meant to be executed without any user interaction.
Maybe it is a better way for you to create your own custom tool that implements functions of some BaseCommand item, that way you can make focus on it.

What is Use Case that you are trying to implement?


Thanks for your help, I've tried using ICommandItem.Execute but it still leaves focus on the tool I had previously selected.

Here is the problem I have: The user clicks on the 'tool' (call it 'Tool A' for this example) and this opens a window with various items on. They then click on another tool, for example the pan/zoom tool in ArcMap, then click back on the 'Tool A' window, however when they hover the mouse over a map, the Pan/Zoom tool is still active.

I guess I may have to do what you suggest and create a new custom tool (although this would be a first as I'm new to this!)
0 Kudos
AlexanderGray
Honored Contributor
A command cannot have focus unless it is also a tool.  Commands are one click kind of deals, they don't have any focus.  When you click on your command and the form pops up, the current tool is still the previous one.  Now what you do when the form is open is up to you.  If the users are not to have any interaction with arcmap, make the form modal, this will prevent focus from shifting from the form back to the map.  If that is not your goal, you can also set the current tool to the select elements tool, it is a de facto default tool.
0 Kudos
DubravkoAntonic
Occasional Contributor
Don't be afraid to implement new things, tool is not that complicated at all. Use ArcGisWizard in VS for creating ArcMap Tool. Use OnClick for opening the form. Test how and when this event is triggered.
For this UseCase you can when form receives a focus checks, Form_Activated event,  if Application.CurrentTool is your tool.  If is not, then Find tool by GUID and set it as your current tool.

here are some hints
some general utility class
        public static ICommandItem FindTool(string toolGUID)
        {
            ICommandItem item = null;
            try
            {
                UID pUID = new UIDClass();
                pUID.Value = toolGUID;
                item = Extension.Application.Document.CommandBars.Find(pUID, false, false);
            }
            catch { }

            return item;
        }

tool GUID is string {...} or you can do it by ProgID which is namspace.className

form class
//member
private ICommandItem m_currentTool = null;

//method
// use form activated method for checking and setting current tool
        private void ToolForm_Activated(object sender, EventArgs e)
        {
            if (Extension.Application.CurrentTool == null ||
                Extension.Application.CurrentTool.Caption != currentTool.Caption)
            {
                Extension.UIState = Extension.UIStates.SomeState;
                Extension.Application.CurrentTool = currentTool;
            }
        }
0 Kudos
SimonField
Emerging Contributor
A command cannot have focus unless it is also a tool.  Commands are one click kind of deals, they don't have any focus.  When you click on your command and the form pops up, the current tool is still the previous one.  Now what you do when the form is open is up to you.  If the users are not to have any interaction with arcmap, make the form modal, this will prevent focus from shifting from the form back to the map.  If that is not your goal, you can also set the current tool to the select elements tool, it is a de facto default tool.


Thanks for the answer. I've just started support work on the system we're using and from what I gather, the items that inherit from ArcCommand do so because these don't need to interact with the map, whereas the ones that inherit from ArcTool do. However by changing them all to inherit from ArcTool seems to work ok so I'll try that and see if it works.
0 Kudos
SimonField
Emerging Contributor
Don't be afraid to implement new things, tool is not that complicated at all. Use ArcGisWizard in VS for creating ArcMap Tool. Use OnClick for opening the form. Test how and when this event is triggered.
For this UseCase you can when form receives a focus checks, Form_Activated event,  if Application.CurrentTool is your tool.  If is not, then Find tool by GUID and set it as your current tool.

here are some hints
some general utility class
        public static ICommandItem FindTool(string toolGUID)
        {
            ICommandItem item = null;
            try
            {
                UID pUID = new UIDClass();
                pUID.Value = toolGUID;
                item = Extension.Application.Document.CommandBars.Find(pUID, false, false);
            }
            catch { }

            return item;
        }

tool GUID is string {...} or you can do it by ProgID which is namspace.className

form class
//member
private ICommandItem m_currentTool = null;

//method
// use form activated method for checking and setting current tool
        private void ToolForm_Activated(object sender, EventArgs e)
        {
            if (Extension.Application.CurrentTool == null ||
                Extension.Application.CurrentTool.Caption != currentTool.Caption)
            {
                Extension.UIState = Extension.UIStates.SomeState;
                Extension.Application.CurrentTool = currentTool;
            }
        }


Thanks for the answer, not sure I understand exactly how the code works though, I'll probably try to set all the items to inherit from ArcTool to see if it works.
0 Kudos
DubravkoAntonic
Occasional Contributor
If you get lost while solving problem contact me on my mail

dubravko.antonic@gisdata.com

Maybe I'll manage to help you.
I'm in GMT+1 zone.
0 Kudos