How to create and load add-ins in Visual Studio .NET by using Visual C# .NET or in Visual Studio 2005 by using Visual C# 2005 (816167)
The information in this article applies to:
- Microsoft Visual C# 2005, Express Edition
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
For a Microsoft Visual Basic .NET version of this
article, see
317345. IN THIS TASKSUMMARYThis article describes how to create a simple, compiled
add-in that inserts the current date and time at the insertion point. In this
article, you create and install an add-in project, load the add-in, and
integrate the add-in in the Visual Studio .NET or Visual Studio 2005 interface. back to the topRequirementsThe following list outlines the recommended hardware,
software, network infrastructure, and service packs that you must have:
- Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
- Microsoft Windows 2000 Professional, Microsoft Windows Server 2003, Microsoft Windows 2000
Server, Microsoft Windows XP Professional, Microsoft Windows XP Server with
the Microsoft .NET Framework
back to the
topCreate and Load Add-ins in Visual Studio .NET- Start Visual Studio .NET or Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- Under Project Types, expand
Other Projects, and then click Extensibility
Projects.
Note In Visual Studio 2005, expand Other Projects Types under Project Types, and then click Extensibility. - Under Templates, click Visual
Studio .NET Add-in or Visual Studio Add-in.
- In the Name text box, type
InsertDateTime.
- In the Location text box, type
C:\, and then click OK. This starts the
Extensibility Wizard.
back to the
topComplete the Steps in the Extensibility Wizard- On the first page of the Extensibility Wizard, click
Next.
- On the Select a Programming Language page,
click Create an Add-in using Visual C#, and then click
Next.
- On the Select a Application Host page,
clear the Microsoft VSMacros IDE check box, and then
click Next.
Note In Visual Studio 2005, you do not have to click to clear the Microsoft VSMacros IDE check box. - On the Enter a Name and Description page,
type Insert Date and Time in the Name
text box. In the Description text box, type Inserts
the current date and time, and then click
Next.
- On the Choose Add-in Options page, click
to select the following check box to create an item on the
Tools menu:
Yes, create a 'Tools' menu item. By default this will cause the Add-in to load when the button is clicked unless the Add-in is set to load on startup of the host application. - On the Choose Add-in Options page, click
to select the following check box so that the add-in loads when the host
application loads:
I would like my Add-in to load when the host application starts. Click Next. - On the Choosing 'Help About' Information
page, click Next.
- On the Summary page, click
Finish. This creates a solution with both an add-in project
and an add-in setup project.
back to the
topUpdate the
Connect.cs File and Add Custom Actions- In Solution Explorer, double-click
Connect.cs, and then scroll through the file. Notice that
by default, Visual Studio .NET and Visual Studio 2005 insert the necessary code templates. For this
article, you only need to do the following:
- Add a method that writes the date and time.
- Modify the Exec method.
- Add the following code after the Exec method:
public bool InsertDateTime()
{
if(applicationObject.ActiveDocument != null)
((TextSelection)applicationObject.ActiveDocument.Selection).Text = DateTime.Now.ToString();
return true;
}
- In the Exec method, change the following code:
handled = true; to the following:handled = InsertDateTime(); - In Solution Explorer, right-click
InsertDateTimeSetup, point to View, and then
click Custom Actions.
- Right-click Custom Actions, and then click
Add Custom Action.
- Click Application Folder, and then click
OK.
- Click Primary output from
InsertDateTime(Active), and then click OK. Notice that
primary output appears in the Install, the
Commit, the Rollback, and the
Uninstall nodes under the Custom Actions
node.
back to the
topBuild and Install the Add-in Project- Because setup projects are not included in the build
configuration by default, you must use one of the following methods to build
the solution:
- Method 1: Right-click InsertDate, and then click
Build. Similarly, right-click
InsertDateTimeSetup, and then click
Build.
- Method 2: To build the whole solution at once, click
Configuration Manager on the Build menu.
Click to select the Build check box for
InsertDateTimeSetup.
- Press the CTRL+SHIFT+B key combination to build the
whole solution. A complete installation package is now available for InsertDateTime.
- Install the add-in that you just built. To do this, follow these
steps:
- Close all instances of Visual Studio .NET or Visual Studio 2005, and then
save any changes if you are prompted.
- Open Windows Explorer, and then locate the following
folder:
C:\InsertDateTime\InsertDateTimeSetup\Debug - Right-click InsertDateTimeSetup.msi,
and then click Install.
- In the InsertDateTimeSetup dialog box,
click Next three times. Notice that a progress bar appears
while the service installs.
- After the add-in is installed, click
Close.
- Restart Visual Studio .NET or Visual Studio 2005.
Note After you restart Visual Studio .NET or Visual Studio 2005, the add-in is always loaded
until you remove it.
back to the
topAdd an Icon to the ToolbarIt is helpful, and also instructive, to integrate your
add-in more fully in the Visual Studio IDE. To do this, add an icon to the toolbar, and then associate the add-in
with a keyboard shortcut.
- On the Tools menu, click
Customize.
- On the Commands tab, click Addins in the
Categories list.
- Drag InsertDateTime to the active
toolbar, and then click Keyboard.
- In the Show commands containing text box,
type insertdatetime. Notice that your add-in (InsertDateTime.Connect.InsertDateTime) appears in the
list.
- In the Press shortcut key(s) text box,
press the CTRL+SHIFT+RIGHT ARROW key combination. Notice that the
Shortcut currently used by text box notifies you that the
Edit.SizeControlRight command already uses this keyboard
shortcut.
- Press BACKSPACE to delete the key
combination. Press the CTRL+SHIFT+BACKSPACE key combination. Because no
other command uses this keyboard shortcut, click
Assign.
- In the Options dialog box, click
OK.
- In the Customize dialog box, click
Close.
- In a text file, click InsertDateTime on
the toolbar. Notice that the text is inserted. Alternatively, you can also press
the CTRL+SHIFT+BACKSPACE key combination to insert the text.
back to the
topComplete Code Listing Connect.csnamespace InsertDateTime
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
[GuidAttribute("65EA989D-DFCD-4D36-9E00-A9D928F8B9A2"), ProgId("InsertDateTime.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2, IDTCommandTarget
{
public Connect()
{
}
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands commands = applicationObject.Commands;
_CommandBars commandBars = applicationObject.CommandBars;
try
{
Command command = commands.AddNamedCommand(addInInstance, "InsertDateTime", "InsertDateTime", "Executes the command for InsertDateTime", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
CommandBar commandBar = (CommandBar)commandBars["Tools"];
CommandBarControl commandBarControl = command.AddControl(commandBar, 1);
}
catch(System.Exception /*e*/)
{
}
}
}
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
public void OnStartupComplete(ref System.Array custom)
{
}
public void OnBeginShutdown(ref System.Array custom)
{
}
public void QueryStatus(string commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
{
if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "InsertDateTime.Connect.InsertDateTime")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
}
}
}
public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "InsertDateTime.Connect.InsertDateTime")
{
handled = InsertDateTime();
return;
}
}
}
public bool InsertDateTime()
{
if(applicationObject.ActiveDocument != null)
((TextSelection)applicationObject.ActiveDocument.Selection).Text = DateTime.Now.ToString();
return true;
}
private _DTE applicationObject;
private AddIn addInInstance;
}
} back to the
topVerify That It Works- Press CTRL+N.
- In the New File dialog box, click
General under Categories, click Text
File under Templates, and then click
Open.
- On the Tools menu, click
InsertDateTime. The current date and time are written to the
text file.
back to the
topRemove the Add-in- Close all instances of Visual Studio .NET or Visual Studio 2005.
- Open Windows Explorer, and then locate the following
folder:
C:\InsertDateTime\InsertDateTimeSetup\Debug - Right-click InsertDateTimeSetup.msi, and
then click Uninstall.
- Click Yes when you are prompted to
uninstall.
back to the
topTroubleshootback to the
topREFERENCESFor more information, see the following Microsoft Web
sites: back to the top
Modification Type: | Major | Last Reviewed: | 1/17/2006 |
---|
Keywords: | kbide kbHOWTOmaster KB816167 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|