VSIP - More on command bars and how to use them

A Solution object has property named Projects that contains collection of all projects opened in that solution. The Projects collection contains "Project objects"; one for each open project in VS.NET. Each project has property named ProjectItems which is all the project items like the files, folders, sub projects, resources, etc.

Each project item is of one of the following (There are more).

  • Misc
  • Physical File
  • Physical Folder
  • Sub Project
  • Web Folder
  • Web Item

In a previous posting I mentioned how to get the path to a Folder for example, and how to access from the project level working up. However, if you look at the code I wrote, it works for any item, directly, so you don't have to traverse.

Full Path To Any Project Item:

UIHierarchyItem _item = firstSelectedUIHierarchyItem();
ProjectItem prjItem = item.Object as ProjectItem;
string strPath = prjItem.Properties.Item("FullPath").Value.ToString();

Finding A Command / Command Bar to place your commands on:

foreach (CommandBarControl oControl in vsCmdBarWebFolder.Controls)
{
    if (oControl.Caption.Trim().ToLower() == "add asp.net fol&der")
    {
        idx = (oControl.Index + 1);
        break
;
    }
}
 
// This piece of code, finds the command "Add Asp.Net Folder" from the Web Folder Command Bar List.
// Then I retrieve the index, so that I can place my command one button lower on the context menu.

Common things you will probably use:

The following all probaly goes in you OnConnection, so that you have a couple of reusable helper objects. really this would only be nessasary if your addin involves a number of objects to add. If you are simply adding one basic command, there is probably no need for this, except that in my opnion local vars make the code a little more readable and step through friendly.

_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
Commands2
commands = _applicationObject.Commands as Commands2;
CommandBars cmdBars = _applicationObject.CommandBars as CommandBars
;
object oMissing = System.Type.Missing
;

// oMissing is useful, because when adding Controls to command bars you can't use null for some reason I don't really understand yet.
CommandBarPopup _PopUp = (CommandBarPopup)vsCmdBarMainMenuBar.Controls.Add(MsoControlType.msoControlPopup,
         oMissing, oMissing,  (vsCmdBarMainMenuBar.Controls.Count + 1),  oMissing);
_PopUp.Caption = "GBM Addins";
CommandBar _topLevelCommandBar = _PopUp.CommandBar;

What we did in the last three lines, was create a new Command that will appear on the Top Level Main Menu in the final position. So generally after the Help command. You can then add commands and things to _topLevelCommandBar and have your own top level menu.

Published Tuesday, June 27, 2006 4:39 PM by jminond
Filed under: , ,

Comments

# DNS - .NET Blog

Tuesday, June 27, 2006 3:25 PM by Jonathan's Blog

I have a new additional blog, that will be focused more on .NET specifics, as well as Extensibility and automation as opposed to this blog that is more Rainbow and Application geared.

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.