Following is my Connect.cs class that I used to add code of my addin. At the moment it does two main tasks. that are adding a button to the solution explorer's 'Web Item' menu (that is to execute the addin). and the other task is adding a button to standard toolbar to view a dockable tool window where i can add my own error/warning etc later.
public class Connect : IDTExtensibility2, IDTCommandTarget
{
private DTE2 _applicationObject;
private AddIn _addInInstance;
private CommandBar standardToolBar2;
private static CommandBarControl ctrl2;
private static string selectedFile;
private const int TOOLWINDOW_FOR_BUTTON_INVISIBLE = 0;
private const int TOOLWINDOW_FOR_BUTTON_VISIBLE = 1;
private const string BUTTON_COMMAND_NAME = "TaskListCommand";
private const string BUTTON_COMMAND_CAPTION = "Check Usability";
private const string BUTTON_COMMAND_TOOLTIP = "Click to open Usablity tool Window";
private CommandBarButton StandardCommandBarButton;
private EnvDTE.Window toolWindow;
/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
//just make sure you also update the QueryStatus/Exec method to include the new command names.
try
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
switch (connectMode)
{
case ext_ConnectMode.ext_cm_UISetup:
// Do nothing for this add-in with temporary user interface
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
//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:
standardToolBar2 = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["Web Item"];
//Add a command to the Commands collection:
Command command = commands.AddNamedCommand2(_addInInstance, "UsabilityEvaluationAddIn", "Check Usability", "Click to start evaluating this page for Usablity", true, 2131, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
//Add a control for the command to the tools menu:
if ((command != null) && (standardToolBar2 != null))
{
ctrl2 = (CommandBarControl)command.AddControl(standardToolBar2, 1);
ctrl2.TooltipText = "Click to start evaluating this page for Usablity";
}
break;
case ext_ConnectMode.ext_cm_Startup:
// The add-in was marked to load on startup
// Do nothing at this point because the IDE may not be fully initialized
// Visual Studio will call OnStartupComplete when fully initialized
break;
case ext_ConnectMode.ext_cm_AfterStartup:
// The add-in was loaded by hand after startup using the Add-In Manager
// Initialize it in the same way that when is loaded on startup
AddTemporaryUI();
break;
}
}
catch (System.ArgumentException e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
catch (System.Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
Microsoft.Win32.RegistryKey registryKey;
int myToolWindowVisible;
try
{
switch (disconnectMode)
{
case ext_DisconnectMode.ext_dm_HostShutdown:
case ext_DisconnectMode.ext_dm_UserClosed:
if ((StandardCommandBarButton != null))
{
StandardCommandBarButton.Delete(true);
}
// Store in the Windows Registry if the toolwindow was visible when unloading the add-in
myToolWindowVisible = TOOLWINDOW_FOR_BUTTON_INVISIBLE;
if (toolWindow != null)
{
if (toolWindow.Visible)
{
myToolWindowVisible = TOOLWINDOW_FOR_BUTTON_VISIBLE;
}
}
registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\MyTaskList");
registryKey.SetValue("MyTaskListVisible", myToolWindowVisible);
registryKey.Close();
break;
}
}
catch (System.Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
AddTemporaryUI();
}
public void AddTemporaryUI()
{
const string VS_STANDARD_COMMANDBAR_NAME = "Standard";
Command myCommand = null;
CommandBar standardCommandBar = null;
CommandBars commandBars = null;
Microsoft.Win32.RegistryKey registryKey;
object[] contextUIGuids = new object[] { };
try
{
// Try to retrieve the command, just in case it was already created, ignoring the
// exception that would happen if the command was not created yet.
try
{
myCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + BUTTON_COMMAND_NAME, -1);
}
catch
{
}
// Add the command if it does not exist
if (myCommand == null)
{
myCommand = _applicationObject.Commands.AddNamedCommand(_addInInstance,
BUTTON_COMMAND_NAME, BUTTON_COMMAND_CAPTION, BUTTON_COMMAND_TOOLTIP, true, 2131, ref contextUIGuids,
(int)(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
}
// Retrieve the collection of commandbars
commandBars = (CommandBars)_applicationObject.CommandBars;
// Retrieve some built-in commandbars
standardCommandBar = commandBars[VS_STANDARD_COMMANDBAR_NAME];
// Add a button on the "Standard" toolbar
StandardCommandBarButton = (CommandBarButton)myCommand.AddControl(standardCommandBar,
standardCommandBar.Controls.Count + 1);
// Change some button properties
StandardCommandBarButton.Caption = BUTTON_COMMAND_CAPTION;
StandardCommandBarButton.BeginGroup = true;
// Get if the toolwindow was visible when the add-in was unloaded last time to show it
registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\MyTaskList");
if (registryKey != null)
{
if ((int)registryKey.GetValue("MyTaskListVisible") == TOOLWINDOW_FOR_BUTTON_VISIBLE)
{
ShowToolWindow();
}
registryKey.Close();
}
}
catch (System.Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
public void OnBeginShutdown(ref Array custom)
{
}
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName == _addInInstance.ProgID + "." + BUTTON_COMMAND_NAME)
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
}
else
{
status = vsCommandStatus.vsCommandStatusUnsupported;
}
}
}
private void ShowToolWindow()
{
const string TOOLWINDOW_GUID = "{6CCD0EE9-20DB-4636-9149-665A958D8A9A}";
EnvDTE80.Windows2 windows2;
string assembly;
object myUserControlObject = null;
DeviationsTaskList myUserControl;
try
{
if (toolWindow == null) // First time, create it
{
windows2 = (EnvDTE80.Windows2)_applicationObject.Windows;
assembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
toolWindow = windows2.CreateToolWindow2(_addInInstance, assembly,
typeof(DeviationsTaskList).FullName, "Check Usability", TOOLWINDOW_GUID, ref myUserControlObject);
myUserControl = (DeviationsTaskList)myUserControlObject;
// Now you can pass values to the instance of the usercontrol
// myUserControl.Initialize(value1, value2)
}
toolWindow.Visible = true;
}
catch (System.Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == "UsabilityEvaluationAddIn.Connect.UsabilityEvaluationAddIn")
{
handled = true;
ShowToolWindow();
selectedFile = GetSourceFilePath();
String message = "";
//if the selected file in a web page
//then, allow the user with the usability evaluation process
if (GetSourceFilePath().EndsWith(".aspx"))
{
Form messageDialog=new Form();
message = "Are you Sure to start Usability Evaluation on this page";
DialogResult result = System.Windows.Forms.MessageBox.Show(message, "Usabilty Tool Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
//System.Windows.Forms.MessageBox.Show(selectedFile);
GetSourceCodeOfWebPage();// call this method to get the source code of the selected file
}
}
//if not, display an error message
else
{
message = "You cannot Evaluate this page for Usability. Please select web pages only";
System.Windows.Forms.MessageBox.Show(message, "Usabilty Tool Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ctrl2.Enabled = false;
}
return;
}
}
}
}I could build it but when i run visual studio, I am getting this exception "System.ArgumentException:A command with that name already exists at EnvDTE80.Commands2.AddNamedCommand2(AddIn AddInInstance, String Name, String ButtonText,String tooltip,Boolean MSOButton,Object Bitmap, Object[]& ContextUIGUIDs,Int32 vs...........) and it points to my OnConnection() method"
PLEASE help me telling what's wrong with this.
I also have to mention that I made a sample add-in to see the above code working and giving me a toolwindow as expected. It worked fine and i could click the newly created button and view a toolwindow. Later, I deleted the Add-in project folder and the AddIn file both and started trying this in the AddIn project that I am suppose to work.
I got the source details from here,