José Antonio Thomaz

Hi,

When I try to create a menu item inside of Tools menu of Visual Studio 2005, I get the error in the subject of this message.

Follow the OnConnection method.

Any suggest will be accepted!

Thank you!

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)

{

try

{

_applicationObject = (DTE2)application;

_addInInstance = (AddIn)addInInst;

Command _command = null;

if (connectMode == ext_ConnectMode.ext_cm_Startup)

{

object[] contextGUIDS = new object[] { };

Commands2 commands = (Commands2)_applicationObject.Commands;

try

{

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["Tools"];

CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

try

{

//Add a command to the Commands collection:

Command command = commands.AddNamedCommand2(_addInInstance, "UIObjectsGenerator", "Gerador de Controles UI", "Gerador de Controles UI", true, 59, 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) && (toolsPopup != null))

{

object res = command.AddControl(toolsPopup.CommandBar, 1);

((Command)res).AddControl(toolsPopup, 1);

}

}

catch (Exception e)

{

}

}

catch (Exception e)

{

}

}

}

catch (Exception ex)

{

}

}




Re: Visual Studio Extensibility ERROR: A Command with that name already exists. Help me!

Dmitry Pavlov

Hi Jose,

Maybe this code is called twice






Re: Visual Studio Extensibility ERROR: A Command with that name already exists. Help me!

Jose Antonio Thomaz

Hi Dmitry!

Thanks for your answer, very fast!

This code isn't called twice. I put some messagebox in code to monitor the execution and the code is executed ony one time.

I dont know what I do to resolve this error.

Thanks






Re: Visual Studio Extensibility ERROR: A Command with that name already exists. Help me!

Ed Dore

Hi Jose,

I suspect that the command is already present due to a previous loading of the add-in. One way to prevent this problem would be to modify your OnConnection code, to first look for the existing command in the Commands collection, and if already present, just add the existing command to the Tools menu.

Sincerely,






Re: Visual Studio Extensibility ERROR: A Command with that name already exists. Help me!

Jose Antonio Thomaz

Hi Ed,

I'll create a method to verify if the item exists before to create them.

After I post the result.

Thanks!






Re: Visual Studio Extensibility ERROR: A Command with that name already exists. Help me!

Jose Antonio Thomaz

Hi,

I created a loop that iterate by the collection of commands and delete the command if they already exists. The preliminar tests indicates that the problem was solved!

Thanks for Ed and Dmitry!