Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio autocomplete event handler with lambda format

I'm on VS2012 and want to use the lambda format for event handling, however VS does autocomplete with the tab key whenever you type an event subscrition via +=, e.g.:

VS autocompleted with a reference to a function an inserts the function:

txtTitle.TextChanged += txtTitle_TextChanged;

void txtTitle_TextChanged(object sender, TextChangedEventArgs e)
{
    ....
}

Is there any way to force autocomplete with Lambda format of:

txtTitle.TextChanged += (object sender, TextChangedEventArgs e) =>
{
    ....
}

Its a huge pain to have to copy and paste from the autocompleted non-lambda to the tighter lambda format.

like image 200
frigon Avatar asked Oct 05 '22 02:10

frigon


1 Answers

You can just create a code snippet, I have one for creating Lambda events.

here is the snippet if you want to try (just save as whatever.snippet) and import in VS (Tools -> Code Snippet Manager)

Snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>SnippetFile1</Title>
      <Author>sa_ddam213</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>le</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>s</ID>
          <ToolTip>s</ToolTip>
          <Default>s</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>e</ID>
          <ToolTip>e</ToolTip>
          <Default>e</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="method body"><![CDATA[($s$,$e$) => { };]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Then to use just type the eventname += le Tab

Example

Loaded += le Tab

Result

Loaded += (s, e) => { };
like image 57
sa_ddam213 Avatar answered Oct 11 '22 13:10

sa_ddam213