Whenever you right click on a file in a VS.NET project, you see a list of
commands available for that file. For example, when you right click on a
WinForm .VB file, you see "View Code" and "View Designer", along with all the
other options...
I would like to be able to add a command to a file extension of my choice.
For example, I may want to add a "Do Something" command to the menu when a
user right clicks on a ".xyz" file in the project.
Are there any articles or examples out there, for doing this with a VS.NET
addin project, or using the VSIP package?
My language of choice to develop this is VB.NET, but I can easily read and
convert any C# examples / articles.
"Ed Dore [MSFT]" - 16 Feb 2005 21:18 GMT
With an add-in, you could do this by adding a CommandBarButton to the
"Item" menu, and then show/hide the CommandBarButton in a
SelectionEvents.OnChange handler.
I did this a while back to display a menu item for files that had a .aspx
extension. Below is the a code snippet from the main add-in class derived
from IDTExtensibility2. This should give you a good place to start. Note
the btnDavid and selectionEvents variables are members of the Connect
class, to ensure they don't get GC'd.
Ed Dore [MSFT]
This post is 'AS IS' with no warranties, and confers no rights.
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
// Add a "David Test" menu item to the Item context menu
if (btnDavid == null)
{
try
{
Object objMissing = System.Reflection.Missing.Value;
CommandBar cmdBarItemPopup = applicationObject.CommandBars["Item"];
btnDavid = (CommandBarButton)
cmdBarItemPopup.Controls.Add(MsoControlType.msoControlButton, objMissing,
objMissing, objMissing, objMissing);
btnDavid.Caption = "David Test";
btnDavid.FaceId = 1845;
btnDavid.Click += new
_CommandBarButtonEvents_ClickEventHandler(btnDavid_Click);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
// Add a selection events handler, to only display the "David Test" menu
item
// when a single .ASPX is selected in Solution Explorer.
selectionEvents = applicationObject.Events.SelectionEvents;
selectionEvents.OnChange += new
_dispSelectionEvents_OnChangeEventHandler(this.OnSelChange);
}
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(selectionEvents != null)
selectionEvents.OnChange -= new
_dispSelectionEvents_OnChangeEventHandler(this.OnSelChange);
if (btnDavid != null)
{
btnDavid.Click -= new
_CommandBarButtonEvents_ClickEventHandler(btnDavid_Click);
btnDavid.Delete(null);
btnDavid = null;
}
}
// only make the menu item visible when one and only one .aspx file is
selected
// in solution explorer pane.
public void OnSelChange()
{
btnDavid.Visible = false;
if (applicationObject.SelectedItems.MultiSelect != true)
{
SelectedItem selItem = applicationObject.SelectedItems.Item(1);
ProjectItem projItem = selItem.ProjectItem;
if (projItem!=null)
{
if (projItem.Name.EndsWith(".aspx") || projItem.Name.EndsWith(".ASPX"))
btnDavid.Visible = true;
}
}
}
private void btnDavid_Click(CommandBarButton ctrlBtn, ref bool Cancel)
{
Debug.WriteLine("David menu item executed");
}
<end of code>