Visual studio extension in code editor to have menu and submenu.

Vishal Suravase 21 Reputation points
2025-07-09T09:42:04.36+00:00

I want to develop an extension for VS. When I select some code in the cs file or any file there should be some option in code editor like Mymenu and submenu Please guide. how to achieve it.

I have one comand class which actually shows Mymenu when I select some code in the code editor. Im trying to show submenu.

Something similar like Snippet option is there and when we select some code it shows insert comment sub menu populated

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-18T07:55:38.7133333+00:00

    Thank you for reaching out. Please find the answer below.

              1. Defining Your Menu: Use the Visual Studio Extensibility Template:

    • Open Visual Studio.
    • Create a new project using the VSIX Project template.
    • Add a Command item to your project (this will be your main menu item).
      1. Creating the Submenu: The .vsct file (Visual Studio Command Table) is where you declare menus, groups, and commands.
        For each command you want to add to your submenu, you'll need to define them under the Buttons section in the .vsct file. Set their parent to your submenu.

    Here's is the basic example of how to define a menu with a submenu:
    <!-- Define the main menu -->
    <Menu guid="guidMyPackageCmdSet" id="MyMenu" type="Menu">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_CODEWIN" /> <CommandFlag>DynamicVisibility</CommandFlag>
    <Strings> <ButtonText>Mymenu</ButtonText>
    </Strings>
    </Menu>

    <!-- Define the submenu -->
    <Menu guid="guidMyPackageCmdSet" id="MySubMenu" type="Menu">
    <Parent guid="guidMyPackageCmdSet" id="MyMenu" />
    <Strings> <ButtonText>Insert Comment</ButtonText>
    </Strings>
    </Menu>

    <!-- Define commands inside submenu -->
    <Button guid="guidMyPackageCmdSet" id="cmdidInsertComment" priority="0x0100" type="Button">
    <Parent guid="guidMyPackageCmdSet" id="MySubMenu" />
    <Strings> <ButtonText>Insert TODO Comment</ButtonText>
    </Strings>
    </Button>

          3. Implement Command Logic: In your Command.cs file, implement the logic for what happens when the user selects code and clicks your menu item.

    To show your menu when some code is selected, you'll need to handle the appropriate events in your command logic. You can tie the visibility of your commands to whether there is a selection in the editor.

    Example:
     private void Execute(object sender, EventArgs e)
    {    
    var dte = (DTE)Package.GetGlobalService(typeof(DTE));     var selection = dte.ActiveDocument.Selection as TextSelection;     if (selection != null)
        {        
    string selectedText = selection.Text;   
          // Do something with selectedText       
      selection.Insert("// TODO: " + selectedText) ;
       }
    }

         4. Testing Your Extension: After you define your menu and commands, build your extension and run it in an Experimental Instance of Visual Studio to see it in action. Make sure to check the Extensions menu to find "MyMenu" and test out the commands.

    If this doesn't resolve your issue, here are a few follow-up questions that could help narrow down the problem:
    1.What version of Visual Studio are you developing for?

       2.Are there any specific errors or issues you’ve encountered while trying to implement this?
    

    If issue still persist after following all the steps, kindly share the above information with us, we’ll be happy to assist further if needed.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.