I’m still struggling with those VS.NET command bar objects for my Help Builder Add-In.
I’ve yet to come up with a clean way to create a menu option in a ‘fixed’ location on the VS.NET menu. Let’s say I want to add my menu after the Technical Support option in the Help menu. It’s easy to get a reference to the VS Help CommandBar, then run through each of the controls and look at the caption. Well, not quite. I suspect in a localized version of VS.NET other than English it’s not going to be ‘Technical Support’ so the search routine won’t find it.
At first I used a routine that tries to add my menu at the bottom of the existing popup by using Controls.Count-2. This worked fine for my setup, but apparently some other folks got no menu bar. I can only guess that the index was somewhere beyond one of the existing menu bars and while the menu was actually live it wasn't visible.
The problem is that there’s no way that I can see to get from a CommandBarControl object and get the underlying Command object so you can get a unique ID for the item. The CommandBarControl object only contains a Caption property and an ID that appears to be auto-generated when the command gets added to the VS.NET toolbar. So at this point I can’t figure out how to get a unique ID for a CommandBarControl. Absolute index positioning also doesn’t work because the menus actually contain many more items that are hidden and these indexes can change randomly based on what Add-Ins are installed and what they have done to the menu. Grrr…
The problem here is that it’s pretty difficult to get references to everything you need and the relationships are not available. Add to that horrible documentation and you have a recipe for hours of wasted time <g>.
Another problem I’ve just struggled through today is adding a new CommandBar and then deleting the command bar object. While I was debugging the above issue, I happened to notice that my Controls collection for the Help Menu where I attach my submenu had like 20 entries for my menu. Ouch. For the moment I have my Add-In add menu items whenever VS starts, then removes them when it's done. This is much easier for debugging and I've been having some problems to get the add-in to register and get the menu items on the menu properly. Without this 'on the fly' installation of Commands and bars VS would apparently randomly loose my menu items.
So I create menu items on the fly everytime VS starts - this appears to be quick, so I don't really see a downside to this.
However one problem is that the CommandBar did not get deleted. So everytime the UI ran a new command bar gets added and never deleted. Figuring out how to delete this command bar took some time as well.
When I create a CommandBar like this:
CommandBar commandBar = applicationObject.Commands.AddCommandBar(HelpBuilderVsAddin.WWHELP_APPNAME,
vsCommandBarType.vsCommandBarTypeMenu,commandBarHelp,InsertionIndex) as CommandBar;
this.cbMenu = commandBarHelp.Controls[InsertionIndex];
this.cbMenu.BeginGroup = true;
InsertionIndex = 1; // Insert Items at the top
I can reference the CommandBar like this (in OnDisconnection):
CommandBar cb = applicationObject.CommandBars[HelpBuilderVsAddin.WWHELP_APPNAME];
However I can’t delete the bar itself by calling the Delete method:
cb.Delete();
which fails with an unspecified COM exception. Great – Ugh!
In order to get rid of the CommandBar menu item (a Control object) is to hang on to a reference of the CommandBarControl (cbMenu above – hence the this.cbMenu) and then call its Delete() method in OnDisconnection(), which correctly deletes the menu definition. If you don’t do this you’ll end up creating a new menu definition everytime you call AddCommandBar, which doesn’t clean up.
How you would do this with an Add-In that doesn't install/uninstall on each run - I'm not sure. You'd have to get the parent Menu then loop through the controls to find yours and delete it that way I suspect.
Please… how confusing can you get with Commands, CommandBarControls and CommandBars? I’ve got everything working the way I want it to now, but why do I have this feeling that it all is standing like a house of cards? The Add-In model is very powerful, but this menuing stuff has cost me an incredible amount of time. All I need is a simple sub-menu and a few items of that. Yeah, it's not difficult to do this once you KNOW, but figuring out how is not a trivial task. Forget MSDN which gives half assed examples. The two Add-In books I have are a little better but they too don't cover much in the way of managing the menus.
I haven’t looked at the Whidbey implementation, but supposedly the Office toolbar implementation is gone – I hope this was completely redone and not just duplicated in managed code…