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?
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.
Hello Mike,
Thanks for the reply. I'm curious. If you can share, what is the reason behind cloning the tool when it is added rather than just adding it to the collection?
Good question. I never thought about that because it has always been in the code since the beginning of toolbars manager. I asked around and no one who worked on it could remember why the tools were cloned. We all agreed a tool would have to be cloned if it already existed on another toolbar or toolbars manager, but newly created tools shouldn't have to be cloned. So I looked back to older versions of the code and it seems originally, you were required to specify a root tool which was already in the toolbar manager's Tools collection to the Add method. That tool was then cloned and added as an instance tool on the toolbar or menu. It seems that restriction was later removed so that you could create your own instance tools and add them directly and as long as they had the same key and type as an existing root tool, everything would be hooked up fine. However, we were still cloning the tools passed to the Add method. So it seems this is actually a bug. You can report it to the support group if you'd like to be notified when it is fixed: http://ko.infragistics.com/gethelp.