Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1834
How to programmatically fire toolbarbutton click?
posted

Hello,

I am attempting to click a StateButtonTool via code.  I am in a CAB WorkItem object, and I have access to the tool through the object's ToolbarsManager.  I don't see anything like PerformAction or Click--can someone point me in the right direction?

Thanks,

J

  • 2077
    Offline posted

    Hello,

    My answer is related to CAB: you should add a CommandHandler for the object in the ToolbarsManager. The command handler should call the helper method that performs the indicated actions. So when you want to "programmatically fire ToolbarButton click" you only have to call the helper method.

    When you add an invoker for a command, the CAB creates an event handler for you, under the hood. When the event is fired by the control, the CAB searches for the command handler with the same name you have provided when you have added the invoker. This is why the command handler method has to be public and has to have the signature of a standard event handler. The beauty of this is that you can place your command handler anywhere in the WorkItem. For example you can add the invoker in a presenter and the command hanler in a service or the module controller.

    This is how I see the code:

    public void AnyInitializationMehod()
    {
        // somehow get the reference to the ToolbarButton instance
        ToolbarButton button1 = ...;
        WorkItem.Commands["Button1CommandName"].AddInvoker(button1, "ToolClick");
    }

    [CommandHandler("Button1CommandName")]
    public void OnButton1CommandHandler(object sender, EventArgs e)
    {
        Button1CommandHelperMethod();
    }

    private void Button1CommandHelperMethod()
    {
        // code to run when the button 1 is invoked by user click or programmatically
    }

    HTH,
    Emanuel

  • 45049
    Verified Answer
    posted

    J,

    There is no way to programmatically "click" a tool in a WinToolbarsManager.  Instead, you should programmatically take whatever action that you'd take in response to a click through the UI.  For a StateButtonTool, you would toggle the Checked property of the tool, and might also call the same method you'd call in response to a ToolClick event raised through the UI for that tool.

    You may be able to use a command to achieve the result you're after.  I'm not very familiar with CAB, so I can't provide much more detail than this.