A long overdue task I had was to revisit the most popular article of the MZ-Tools Articles Series of my web site and provide the samples in C# code:
HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in
http://www.mztools.com/articles/2005/MZ2005003.aspx
It happened that many developers didn’t bother to read or try the samples because they were only in VB.NET, so I have added them in C# too. It has taken me quite a few hours because I have been enhancing the code with better sections, comments, constants for names and captions, etc. so hopefully it is now more clear how to:
- Add a button to a Visual Studio built-in toolbar
- Add a button to a Visual Studio built-in menu
- Add a button to a Visual Studio built-in context menu
- Create a new toolbar with a button
- Create a new menu in the main menu
- Create a new submenu under a menu
The new code layout will help to identify what does what, and what to remove if you are not interested, both with the temporary or permanent approaches.
My feeling about the commandbar model of Visual Studio (borrowed from Office) has increased after all these hours: commands, buttons and commandbars are incredibly tricky because of:
- Two models: permanent or temporary user interface
- The confusing /resetaddin flag that causes many programmers that they will get a ext_ConnectMode.ext_cm_UISetup phase each time that the add-in is loaded
- The half-backed /resetaddin flag that only reset buttons, but not commandbars, so you get duplicated UI if you don’t take care.
- The CommandBarControls, that can be CommandBarButton or CommandBarPopup
- The confusing connectMode of the OnConnection method, and in general, the IDTExtensibility2 interface.
- The difficulty distinguising Name/Caption when creating commandbars or toolbars.
- The lack of helpful message error from Visual Studio when you make a mistake with a parameter of the call that adds a command, button, commandbar, etc. Only enhancing this area would save tons of hours and questions in forums.
- Etc, etc.
The result is that after several years working with add-ins, I am unable to write code to add commandbars, popups, context menus, main menus, etc. by heart. I have always to go to read the code of the article.