Search code examples
c#visual-studio-2017intellisense

Is there a way to modify the code entered by IntelliSense when using auto code completion in Visual Studio?


I want to modify the auto-fill code that Visual Studio/Intellisense enters in when I press tab or tab tab.

I have already tried searching in online forums and in the Microsoft documentation and I have not found a solution to what I am trying to achieve.

For example, when I type in "MessageBox.s" and press tab, it will auto complete with

MessageBox.Show

I would like to change it so that it will instead auto complete to

MessageBox.Show("");

This way I can save time by not having to type in the parenthesis, quotation marks and semicolon, which I am always going to use. Is there any way to do this in Visual Studio? I am using Visual Studio Community 2017. Or is there a third party add in I can use to customize the behavior?


Solution

  • The short answer: Yes... and no.

    As I understand it, there are two different categories of auto-fill code in Visual Studio:

    • Code snippets. Blocks of code that are stored in files ending in .snippet.
    • Classes and their methods

    The code snippets are in various places in the VS files (mine are in a quite different place than Schneider's). These are things like If statements, and the files can be opened and edited. You can find the folder/file locations in VS by going to Tools >Code Snippets Manager >CSharp >Visual C#. For me it was C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC#\Snippets\1033\Visual C#. If you double click on one of the files you can open it in VS and edit the code, which is all XML. If you want to edit the code that fills in, look for the part of the code with "CDATA":

    <Code Language="csharp"><![CDATA[foreach ($type$ $identifier$ in $collection$)
    {
        $selected$ $end$
    }]]>
    

    You can read more into the details of the syntax in the Microsoft Documentation. But if you want to modify the other type of auto-fill stuff - classes and their methods from the built in libraries - as best as I can gather there is no way to do it; however, there is a workaround in creating your own custom snippets with "shortcuts". I found this page from Microsoft to be helpful for figuring out how to do it. There were a couple of things I had to do differently though compared to the documentation, so I will provide an explanation here using the MessageBox.Show example in C#.

    Go to File >New >File:

    file new file

    When the new file window pops up, select XML file:

    New XML file

    Get rid of the default line of code and paste in the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>MessageBox.Show</Title>
          <Shortcut>mess</Shortcut>
        </Header>
        <Snippet>
          <References>
            <Reference>
              <Assembly>System.Windows.Forms.dll</Assembly>
            </Reference>
          </References>
          <Imports>
            <Import>
              <Namespace>System.Windows.Forms</Namespace>
            </Import>
          </Imports>
          <Code Language="CSharp">
            <![CDATA[MessageBox.Show("");]]>
          </Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    The shortcut tag is what you will type in in your code project to trigger this snippet to come up as a suggestion. And what's in between the inner brackets after CDATA will be the code that auto-fills for you.

    Save the file into your Documents folder (this is the default folder where VS will look later). Name the file MessageBoxShow.snippet

    Now go into Tools >Code Snippets Manager

    Go into snippets manager

    Select your language (Csharp), then click import

    select language

    Navigate to the MessageBox.Snippet file you just saved a moment ago and double click it. The Import Code Snippet dialog opens, asking you to choose where to add the snippet from the choices in the right pane. One of the choices should be My Code Snippets. Select it and click Finish:

    import your file

    Click OK to close the code snippets manager.

    You should now be able to type in "mess" and have your custom snippet code fill in. If not, you may try closing and reopening your project or VS. The Microsoft tutorial said you can add in the shortcut tag later, but I tried it that way and VS didn't pick it up, so I had to recreate the file with the shortcut tag in place then import it and it worked!

    Of course you can essentially use this same method to create a snippet & shortcut for whatever you want. You just need to change the text in the title, shortcut, and code (CDATA) tags and save it under a different file name.