Hello,
I have a custom toolwindowpane with a toolbar.
Everything works fine as long as I have only one instance of it.
If I have p.e. two instances I get an exception when CommandHandlers are added to the toolbar in the second instance
Here is what I do ...
I set the Toolbar in Constructor with SetToolBar(PkgCmdIDList.IDM_SQLEditToolBar);
protected void SetToolBar(int pkgCmdId, [Optional, DefaultParameterValue(VSTWT_LOCATION.VSTWT_TOP)] VSTWT_LOCATION location) { // Add the toolbar by specifying the Guid/MenuID pair corresponding to // the toolbar definition in the vsct file. this.ToolBar = new CommandID(GuidList.guidiNEXTCmdSet, pkgCmdId); // Specify that we want the toolbar at the top of the window this.ToolBarLocation = (int)location; }In OnToolWindowCreated I set the command handlers with
DefineCommandHandler(this.RefreshList, newCommandID(GuidList.guidiNEXTCmdSet, PkgCmdIDList.cmdidSQLEditRefresh));
/// <summary> /// Define a command handler. /// When the user presses the button corresponding to the CommandID, /// then the EventHandler will be called. /// </summary> /// <param name="id">The CommandID (Guid/ID pair) as defined in the .vsct file</param> /// <param name="handler">Method that should be called to implement the command</param> /// <returns>The menu command. This can be used to set parameter such as the default visibility once the package is loaded</returns> protected OleMenuCommand DefineCommandHandler(EventHandler handler, CommandID id) { // First add it to the package. This is to keep the visibility // of the command on the toolbar constant when the tool window does // not have focus. In addition, it creates the command object for us. iNEXTPackage package = (iNEXTPackage)this.Package; OleMenuCommand command = package.DefineCommandHandler(handler, id); // Verify that the command was added if (command == null) return null; // Get the OleCommandService object provided by the base window pane class; this object is the one // responsible for handling the collection of commands implemented by the package. OleMenuCommandService menuService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (null != menuService) { // Add the command handler menuService.AddCommand(command); } return command; }the routine in Package looks like
/// <summary> /// Define a command handler. /// When the user press the button corresponding to the CommandID /// the EventHandler will be called. /// </summary> /// <param name="id">The CommandID (Guid/ID pair) as defined in the .vsct file</param> /// <param name="handler">Method that should be called to implement the command</param> /// <returns>The menu command. This can be used to set parameter such as the default visibility once the package is loaded</returns> internal OleMenuCommand DefineCommandHandler(EventHandler handler, CommandID id) { // if the package is zombied, we don't want to add commands if (this.Zombied) return null; // Make sure we have the service if (menuService == null) { // Get the OleCommandService object provided by the MPF; this object is the one // responsible for handling the collection of commands implemented by the package. menuService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; } OleMenuCommand command = null; if (null != menuService) { // Add the command handler command = new OleMenuCommand(handler, id); menuService.AddCommand(command); } return command; }
best regards Stephan Hartmann ML-Software GmbH