visual studio 2003 can be extended by using addins. By saying extending visual studio, i mean adding some new features (like customized Tool bar, menus etc having some fucntionality) to the visual studio development environment.
addins can also be used to extend many of the MS office products like MS word, Ms outlook etc.
i have used addins to extend Visual studio, MS word and also MS outlook.
Addins can be developed in any language - vb.net or C#.
Addins have to be registered, once they are registered, they can be launched by using the addin manager(we will discuss all these in detail in the later sections).
let me discuss how to develop a sample addin for visual studio 2003. This addin, will add a menu called "SmartCoder ".This menu will have a menu item called "Insert try catch" which will insert try catch block into the code window.
launch Visual studio 2003 and select file->new project, a window(as in figure below) opens up
expand "other projects" node and select "extensibility projects" in the project types.
on the right side we get the templates, since we are developing a sample addin for Visual studio,we have selected VS.net addin template. But if the addin is for other MS office products like word, excel, outlook etc, we have to select "shared addin".let me name my addin as "MyAddin1".
MyAddin1 project will have a class called "Connect.cs". This is how the class will appear
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
using System.Windows.Forms;
[GuidAttribute("259C3898-1FF7-4323-BECD-2AD5DD02103E"), ProgId("MyAddin1.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;
}
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 == "MyAddin1.Connect.MyAddin1")
{
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 == "MyAddin1.Connect.MyAddin1")
{
handled = true;
return;
}
}
}
private _DTE applicationObject;
private AddIn addInInstance;
}
the above given code is the most basic one. this is got when a new addin is created.
but addins are created for the purpose of extending visual studio,so the next question is how/where are we extending VS? well to be honest we have still not extended VS, but we will now. we will just add a commandbar and then add some buttons to that command bar.
to do so, we will have to add some code to OnStartupComplete, so that after modification,the function looks like this
public void OnStartupComplete(ref Array custom)
{
CommandBar cmd = (CommandBar)((CommandBars)_applicationObject.CommandBars).Add("test",System.Reflection.Missing.Value,
System.Reflection.Missing.Value,System.Reflection.Missing.Value);
cmd.Name = "Test";
CommandBarButton BtnCtrl = (CommandBarButton) cmd.Controls.Add(MsoControlType.msoControlButton, "addin", "Button1", System.Reflection.Missing.Value,System.Reflection.Missing.Value);
BtnCtrl.Caption = "Test";
//adding event handler
BtnCtrl.Click+=new _CommandBarButtonEvents_ClickEventHandler(BtnCtrl_Click);
}
commandbars.add is function which expect the following parameters
object name, object position, object menubar, object temporary
name is very much need as for the rest they can have reflection.missing.value.
then we can add a control to the new commandbar which we have created.
commandbar.controls.add is a function which expect the following parameters
object type,object id,object parameter, object before, object temporary
"type" will be specified by the enum MSOControlType.
"id" and "parameter" are used to uniquely identify the control.
if two controls have the same parameter, then their associated eventhandlers may be interchanged at runtime,. this can lead to unexpected behaviour.so its advisable to have parameter unique for each control.
"before" is the control which is before this control in command bar.This is useful when arranging controls in the bar in an order.
"temporary" can have a value of system.reflection.missing.value.
adding eventhandler for these controls is very much same as the normal event handler adding mechanism.
in dot net 2003, there will be an installer along with the addin.
so just run the addin using the F5 and addin installs itself and runs a new instance of VS.
open some project in VS and then click on view->Toolbars.
in the list of toolbars shown, the new toolbar will be present in teh last with the name of "test".
click on it and then the commandbar opens up.
lick on the button and it pops up a message "button clicked".
this is just the basic commandbar example.
we can do the same for menu and menubars too.
addins are not meant only for visual Studio IDE, they can be used to extend word, powerpoint, access, outlook, excel, frontpage, visio etc.
so happy coding .
any discussions with me on any technical topics in dot net are welcome.
you can mail me at nandank81@gmail.com