I've been through this hell once before with VS2003, now it looks in VS2005
the whole Command bar mess has been changed and none for the better - it
still has the same convoluted hierarchy.
Anyway, I can't seem to create custom menu popups and see them visible. THey
seem to create fine - I can see the object created and they show up as named
commands in the Options dialog, but they don't appear to be visible.
I've used both permanent named commands and temporary control additions, but
neither seems to want to display my menu.
Anyone have an example on how to create a new popup, name it and then add a
couple of CommandBarControls to it?
Here's the non-permanent way that worked in VS.NET 2003:
// *** Context Menu Pad for Help menu Popup
CommandBarPopup commandBarPopup =
commandBarHelp.Controls.Add(MsoControlType.msoControlPopup,1,"",
StartIndex, true) as CommandBarPopup;
commandBarPopup.Caption = AddinVs2005.WWHELP_APPNAME;
commandBarPopup.BeginGroup = true;
commandBarPopup.Visible = true;
CommandBar HelpMenuBar = commandBarPopup.CommandBar;
//
// *** ADDING COMMANDS TO THE COMMAND BARS
//
int HelpPopupInsertionIndex = 1;
int ContextInsertionIndex = 1;
int ContainerInsertionIndex = 1;
int CodeWindowInsertionIndex = 1;
// *** Start adding the Commands and CommandControls
this.AddInHelper.AddCommand("ShowHelpBuilder", "Show Help Builder",
"Brings up help builder for the current control",
0, HelpMenuBar, HelpPopupInsertionIndex, true, "Global::alt+f1"); // 101
Here's the helper I use for adding the buttons:
public AddCommandReturn AddCommand(string Name,string Caption, string
Description,
int IconId,CommandBar commandBar, int InsertionIndex,
bool BeginGroup, string HotKey)
{
object []contextGUIDS = new object[] { };
// *** Check to see if the Command exists already to be safe
string CommandName = this.addInInstance.ProgID + "." + Name;
Command command = null;
try
{
command = this.DTE.Commands.Item(CommandName,-1);
}
catch {;}
// *** If not create it!
if (command == null)
{
command = this.DTE.Commands.AddNamedCommand(
this.addInInstance,
Name,Caption,Description,
IconId == 0 ? true : false, IconId,
ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported+
(int)vsCommandStatus.vsCommandStatusEnabled);
if (HotKey != null && HotKey != "")
{
object [] bindings = (object [])command.Bindings ;
if (bindings != null)
{
bindings = new object[1];
//bindings[0] = (object)"Windows Forms Designer::alt+f1";
bindings[0] = (object) HotKey;
try
{
command.Bindings = (object) bindings;
}
catch(Exception ex) { string t = ex.Message; }
}
}
}
CommandBarControl cb = (CommandBarControl)
command.AddControl(commandBar,InsertionIndex);
cb.BeginGroup = BeginGroup;
return new AddCommandReturn(command,cb);
}

Signature
Rick Strahl
West Wind Technologies
www.west-wind.com
www.west-wind.com/weblog
Niels Berglund - 30 Oct 2005 09:37 GMT
Hello Rick,
[SNIP]
> Anyway, I can't seem to create custom menu popups and see them
> visible. THey seem to create fine - I can see the object created and
[quoted text clipped - 6 lines]
> Anyone have an example on how to create a new popup, name it and then
> add a couple of CommandBarControls to it?
OMG, if the commandbar control guru Rick can't get this to work, what chance
will we the mere mortals stand. <g>!!
Yes, this sux, but however thanks to your blog entries from way back I managed
to get it to work (in a fashion). When I ran my add-in outside of debugging
(F5), i.e normal usage of VS I had similar problems as you; my pop-up menu
showed OK, but the actual controls didn't. For me it turned out it was an
exception thrown, that I caught but didn't show. In my case the actual controls
were alredy created, so when I tried to add them again an exception happened
(I didn't tear them down at dis-connect), and I never managed to get the
Commnds.Item method to work to check for a command. I ended up loading the
commands in a hash table and then check against the hash table.
Anyway, here're some code that works for me.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public void OnConnection(object application, ext_ConnectMode connectMode,
object addInInst, ref Array custom) {
if(connectMode == ext_ConnectMode.ext_cm_Startup) {
_app = (DTE2)application;
_addIn = (AddIn)addInInst;
CommandBarPopup depMainPop = null;
cmdHt = new Hashtable();
commands = (Commands2)_app.Commands;
Hashtable ht = new Hashtable();
//loop through all the commands and if they belong to this proj
//add them into a hash table
foreach (Command c in commands) {
if (c.Name.Contains("DeployAddIn"))
cmdHt.Add(c.Name, c);
}
object []contextGUIDS = new object[] { };
string toolsMenuName;
try {
ResourceManager resourceManager = new ResourceManager("DeployAddIn.CommandBar",
Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new System.Globalization.CultureInfo(_app.LocaleID);
string resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName,
"Tools");
toolsMenuName = resourceManager.GetString(resourceName);
}
catch {
//We tried to find a localized version of the word Tools, but one was
not found.
// Default to the en-US word, which may work for the current culture.
toolsMenuName = "Tools";
}
//Place the command on the tools menu.
//Find the MenuBar command bar, which is the top-level command bar holding
all the main menu items:
Microsoft.VisualStudio.CommandBars.CommandBar menuBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_app.CommandBars)["MenuBar"];
//Find the Tools command bar on the MenuBar command bar:
CommandBarControl toolsControl = menuBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try {
depMainPop = (CommandBarPopup)toolsPopup.Controls.Add(vsCommandBarType.vsCommandBarTypePopup,
1, "", 1, true);
depMainPop.Caption = "SQL CLR Deployment";
depMainPop.BeginGroup = true;
depMainPop.Visible = true;
//Add a command to the Commands collection:
AddCmd(depMainPop.CommandBar, "DepProp", "Deployment Properties", "Allows
you to set properties for the deployment task.", 1);
}
catch(Exception ex1) {
MessageBox.Show(ex1.Message, "Error");
}
}
}
private object[] AddCmd(CommandBar parent, string name, string caption, string
decription, int pos) {
object[] contextGUIDS = new object[] { };
string cmdName = string.Format("{0}.{1}", this.GetType().ToString(), name);
Command cmd = null;
//Add a command to the Commands collection:
try {
//look for the command in the hashtable - I tried to use the Commands.Item
method but it won't work
cmd = (Command)cmdHt[cmdName];
//this is what I tried to do, but I'm getting an invalid cast exception
at runtime with the code below
//and if I just do System.Type.Missing, the compiler complains
//cmd = commands.Item(cmdName, (int)System.Type.Missing);
if (cmd == null) {
cmd = commands.AddNamedCommand2(_addIn, name, caption, decription, true,
59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
cmdHt.Add(cmd.Name, cmd);
}
}
catch (Exception e) {
MessageBox.Show(e.Message, "Error");
}
//Add a control for the command to the menu:
if ((cmd != null) && (parent != null)) {
cmd.AddControl(parent, pos);
}
else
MessageBox.Show("Command or parent == null");
return contextGUIDS;
}
Hope this helps.
Niels
LesSmith - 05 Jun 2006 23:13 GMT
> [B]I've been through this hell once before with VS2003, now it look
> in VS2005
[quoted text clipped - 29 lines]
> HTH
> Les Smit