Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Adding a command for a custom add-in where the command already exists

$
0
0

Hi,

I thought I'd have a go at writing an addin for VS but it is not working as expected. Here is my code for the OnConnection method:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
	_applicationObject = (DTE2)application;
	_addInInstance = (AddIn)addInInst;
	if(connectMode == ext_ConnectMode.ext_cm_UISetup)
	{
		object []contextGUIDS = new object[] { };
		Commands2 commands = (Commands2)_applicationObject.Commands;
		CommandBars commandBars = (CommandBars)_applicationObject.CommandBars;
		string toolsMenuName = "Tools";

		//Find the MenuBar command bar
		Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

		//Find the Tools command bar on the MenuBar command bar:
		CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
		
		CommandBarControl _MyAddinMenu;
		try
		{
			// Get the menu bar if it already exists
			_MyAddinMenu = menuBarCommandBar.Controls["My Custom Addin"];
		}
		catch (System.ArgumentException)
		{
			// Doesn't exist so create a new one.
			_MyAddinMenu = menuBarCommandBar.Controls.Add(Type: MsoControlType.msoControlPopup, Id: 9876543210, Before: toolsControl.Index - 1);
			_MyAddinMenu.Caption = "My Custom Addin";
		}

		CommandBarPopup toolsPopup = (CommandBarPopup)_MyAddinMenu;
	  

		try
		{
			//Add a command to the Commands collection:
			Command command = commands.AddNamedCommand2(_addInInstance, "CMD1_MyCustomAddin", "Do Something 1", "Do Something 1 Tooltip", true, 100, 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) && (_newPopup != null))
			{
				command.AddControl(_newPopup.CommandBar, 1);
			}

		}
		catch (System.ArgumentException)
		{
		}
	   
		try
		{
			//Add a command to the Commands collection:
			Command command = commands.AddNamedCommand2(_addInInstance, "CMD2_MyCustomAddin", "Do Something 2", "Do Something 2 Tooltip", true, 101, 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) && (_newPopup != null))
			{
				command.AddControl(_newPopup.CommandBar, 2);
			}

		}
		catch (System.ArgumentException)
		{
			
		}

	}
}

So, what I have is that I am adding a new menu item before the Tools menu titled 'My Custom Addin', then I want to add two controls to this new menu titled 'Do Something 1' and 'Do Something 2'. 

When I first built the solution and ran it yesterday (multiple times), the menu appeared as did the two controls. Now for some reason when I build it all I see is a disabled menu 'My Custom Addin' and when I hover over it the tooltip reads: "Adding Child Items to this menu will enable it".

Now, the reason that the children aren't being added to this menu is because of the following code:

try
		{
			//Add a command to the Commands collection:
			Command command = commands.AddNamedCommand2(_addInInstance, "CMD1_MyCustomAddin", "Do Something 1", "Do Something 1 Tooltip", true, 100, 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) && (_newPopup != null))
			{
				command.AddControl(_newPopup.CommandBar, 1);
			}

		}
		catch (System.ArgumentException)
		{
		}
 

where it falls over on this line:

Command command = commands.AddNamedCommand2(_addInInstance, "CMD1_MyCustomAddin", "Do Something 1", "Do Something 1 Tooltip", true, 100, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

with the exception: "A Command with that name already exists.".

I did a bit of reading and found this: http://msdn.microsoft.com/en-us/library/envdte80.commands2.addnamedcommand2(v=vs.90).aspx

which states "Creates a named command that is saved by the environment and made available the next time the environment starts, even if the Add-in is not loaded on environment startup."

So, if I understand this correctly I need to try and find the command first, delete it if it exists, and then re-add it?

I tried doing this by looking for the specific name within the commandBars variable and using the delete method, but I was unable to do this. While creating this thread, I came across this : http://social.msdn.microsoft.com/Forums/vstudio/en-US/9a026206-8ef0-4ef2-aa51-d00d8e0efc87/a-command-with-that-name-already-exists-error-?forum=vsx

which says that using Commands.item(Commands.count) .Name would work however I don't see how using commands.Count would return the correct index if the number of commands changed with another addin.



Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>