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
700
Adding derived tools
posted

Hello,

I created a class derived from ButtonTool so that I could add some properties.  There is a hitch however.

When adding to the toolbarmanager and ribbon, no problem.  Shows up fine.  The problem comes when consuming the ToolClick event.  I was hoping to be able to do something like if (e.Tool is MyCustomToolClass) {} and not have to rely on tags or some such error prone method.

However, when the tool gets there it is just a base tool class and cannot even be explicitly cast to be my custom tool class.  This seemed odd.  So I added a form level MyCustomToolClass variable and set it equal to the new instance of it and added it to the toolbar and ribbon as normal.  When we got to the toolclick event though (e.Tool == FormLevelToolVariable) returned false.  So it looks like either when adding the tool to the toolbar or when it is passed through to the event it isn't actually my instance that is available but a newly created tool base object.

 Any thoughts?

Parents
  • 44743
    Verified Answer
    posted

    If you added the tool outside the BeginUpdate()...EndUpdate() calls to the toolbars manager, any tool instances added to a toolbar will be cloned. If you did not override the Clone method in your custom tool, this clone will be created from the base type. Here is the proper way to implement the Clone override for your derived tool type:

    protected override ToolBase Clone(bool cloneNewInstance)
    {
     MyCustomToolClass newTool = new MyCustomToolClass( this.Key );
     newTool.InitializeFrom(this, cloneNewInstance);
     return newTool;
    }

    Also, if your derived tool type defines any member variables that you want copied to new instances of your cloned tool, you should also override InitializeFrom(ToolBase, bool). Here is a sample implementation:

    protected override void InitializeFrom( ToolBase sourceTool, bool cloneNewInstance )
    {
     base.InitializeFrom( sourceTool, cloneNewInstance );

     MyCustomToolClass sourceDerivedTool = sourceTool as MyCustomToolClass;

     if ( sourceDerivedTool == null )
      return;

     this.member1 = sourceDerivedTool.member1;
     this.member2 = sourceDerivedTool.member2;
    }

    By the way, you should override Clone() even if you do add the tool while the manager is updating because tools will also be cloned when they are bumped from a toolbar and the bumped tools are shown or in other situations.

Reply Children
No Data