locked
Invoke vs menu command from code RRS feed

  • Question

  • Hi,

    I have a VSPackage in which some custom code need to invoke the "add new item" dialog of visual studio (the one shown when user click on add new item menu command).

     

    How do I fire that IDE menu command from code.

    How do I get its instance and fire it?

    is it possible ?

     

    a code sample will be great

     

    Thanks,

    Tidhar

    Thursday, May 1, 2008 1:36 PM

Answers

  • The command name is Project.AddNewItem.

     

    DTE dte = GetService(typeof(DTE));

    dte.ExecuteCommand("Project.AddNewItem", "");

     

    Thursday, May 1, 2008 1:51 PM
  • I originally had the second call as null, incorrectly.  I edited the post to make the second parm to ExecuteCommand an empty string.  Sorry for the confusion.  You are correct, I was missing the cast.  I just typed the code and didn't run it.

     

    Thursday, May 1, 2008 2:07 PM
  • hi,

    Thanks for the help.

    it worked.

    this is the code I used :

    Code Snippet

    DTE dte = GetService(typeof(DTE)) as DTE;

    dte.ExecuteCommand("Project.AddNewItem","");

     

     

     

    One note : to test it you must open a project in the debug instance of visual studio (experimental hive).

     

    thanks,

    Tidhar.

    Thursday, May 1, 2008 2:20 PM

All replies

  • The command name is Project.AddNewItem.

     

    DTE dte = GetService(typeof(DTE));

    dte.ExecuteCommand("Project.AddNewItem", "");

     

    Thursday, May 1, 2008 1:51 PM
  • Thanks.

    That what i was looking for.

    I think you are missing a cast :

    Code Snippet

    DTE dte = (DTE)GetService(typeof(DTE));

    dte.ExecuteCommand("Project.AddNewItem", null);

     

     

    however, this is not working. I added the needed reference (as far as i know) but visual studio crash when i call this code.

     

    Thursday, May 1, 2008 2:02 PM
  • I originally had the second call as null, incorrectly.  I edited the post to make the second parm to ExecuteCommand an empty string.  Sorry for the confusion.  You are correct, I was missing the cast.  I just typed the code and didn't run it.

     

    Thursday, May 1, 2008 2:07 PM
  • hi,

    Thanks for the help.

    it worked.

    this is the code I used :

    Code Snippet

    DTE dte = GetService(typeof(DTE)) as DTE;

    dte.ExecuteCommand("Project.AddNewItem","");

     

     

     

    One note : to test it you must open a project in the debug instance of visual studio (experimental hive).

     

    thanks,

    Tidhar.

    Thursday, May 1, 2008 2:20 PM