I am trying to extend the ButtonTool in my winforms mdi child form however, I keep getting a Key not found error? This only happens when I extend the ButtonTool. If I do not extend the ButtonTool then things work as expected. I am attempting to implement a command pattern with the IG toolbars etc. however, it is proving to be MUCH more of a headache than I expected. Can someone please help?
// ButtonTool btool = new ButtonTool("Test button"); // this will work...
btool.SharedProps.Caption =
toolMgr.Tools.Add(btool);
toolMgr.ToolClick +=
...
</code>
this is the error I receive when the child form loads...
ystem.ArgumentException: Key not foundParameter name: key at Infragistics.Shared.KeyedSubObjectsCollectionBase.GetItem(String key) at Infragistics.Win.UltraWinToolbars.ToolsCollectionBase.get_Item(String key) at Infragistics.Win.UltraWinToolbars.ToolBase.InternalSetUnderlyingTool(ToolBase underlyingMdiChildTool) at Infragistics.Win.UltraWinToolbars.ToolsCollectionBase.MergeMdiChildTools(ToolsCollection childToolsCollection) at Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.MergeMdiChildToolbarTools() at Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.MergeMdiChild(UltraToolbarsManager childToolbarsManager) at Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.InternalSetActiveMdiChildManager(UltraToolbarsManager newActiveMdiChildManager, Boolean calledFromMdiChildActivate, Boolean remergeIfUnchanged) at Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.OnFormMdiChildActivated(Object sender, EventArgs e) at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Windows.Forms.Form.OnMdiChildActivate(EventArgs e) at System.Windows.Forms.Form.ActivateMdiChildInternal(Form form) at System.Windows.Forms.Form.WmMdiActivate(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Form.DefWndProc(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.MdiClient.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Can you post some of the code for your derived button? I only need to see the constructor and overridden members.
}
#endregion
The error seems to occur only when attempt to show my MDI Child Form after I add the derived ButtonTool directly to my UltraToolbar, If I add this button to a PopupButtonTool's Tool collection then all seems to work.??
Ok, I see two problems here. The first is in your call to AddTool in the originally posted code. You are passing "Test Button" as the key, but that is the caption of the tool. Pass in "test" instead. The second problem is that when you use the AddTool method, the root tool is cloned to create the tool instance. In the case of your derived tool, it is going to create a new ButtonTool, not a new RemoveDocumentButton. Add the following override to your derived button:
protected override ToolBase Clone( bool cloneNewInstance ){ RemoveDocumentButton newTool = new RemoveDocumentButton( this.Key ); newTool.InitializeFrom( this, cloneNewInstance ); return newTool;}
Mike, Overriding Clone did the trick everything now works as I expect. Sorry, I did have a bug in the originally posted code that I must have introduced when I transcriped it to the forum. Thanks for you response and tell the boss that I said you deserve a raise.
Haha, thanks. I won't forget to pass on the message :)