frigon
frigon

Reputation: 5129

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.

Upvotes: 5

Views: 3010

Answers (2)

sa_ddam213
sa_ddam213

Reputation: 43634

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) => { };

Upvotes: 4

szotp
szotp

Reputation: 2622

You can write:

this.txtTitle.TextChanged += (s, e) => {};

Upvotes: 0

Related Questions