Click here to Skip to main content
14,270,343 members

A Simple Code Snippet to Add an Event

Rate this:
4.98 (34 votes)
Please Sign up or sign in to vote.
4.98 (34 votes)
8 Jun 2012CPOL
Adding an event to your class is simple, but it needs a bit of typing. I'm lazy, so I'd rather Visual Studio did the work. This snippet works in the same way as the "prop" snippet.

Introduction

Adding an event to a class is pretty simple: create your event.

public event EventHandler FilterChange;

Then, create a suitable method to signal it to subscribers:

protected virtual void OnFilterChange(EventArgs e)
    {
    EventHandler eh = FilterChange;
    if (eh != null)
        {
        eh(this, e);
        }
    }

But I'm lazy. And I don't want to type all that each time! So, here we have a simple Visual Studio Snippet which types that lot for you - much the same way that "prop" gives you:

public int MyProperty { get; set; } 

Using the Code

A snippet in Visual Studio is just an XML file, with some specific data fields:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Event Template</Title>
      <Author>Paul (OriginalGriff) Griffin</Author>
      <Description>Creates an Event template - includes the Event and the OnEvent method</Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes />
      <Keywords />
      <Shortcut>evh</Shortcut>
    </Header>
    <Snippet>
      <References />
      <Imports />
      <Declarations>
        <Literal Editable="true">
          <ID>Description</ID>
          <Type></Type>
          <ToolTip></ToolTip>
          <Default>Description</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>Name</ID>
          <Type></Type>
          <ToolTip></ToolTip>
          <Default>Name</Default>
          <Function></Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="method decl" Delimiter="$"><![CDATA[/// <summary>
/// Event to indicate $Description$
/// </summary>
public event EventHandler $Name$;
/// <summary>
/// Called to signal to subscribers that $Description$
/// </summary>
/// <param name="e"></param>
protected virtual void On$Name$(EventArgs e)
    {
    EventHandler eh = $Name$;
    if (eh != null)
        {
        eh(this, e);
        }
    }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

You can create this using Visual Studio from "File...New...File" and selecting XML file, but when you have saved it, you will need to manually rename it to ".snippet" from ".XML", or you can download it from the link. You need to place this in the Visual Studio Snippets folder, which defaults to:

C:\Users\<UserName>\Documents\Visual Studio 2010\Code Snippets\Visual C#

For VS2019 the folder is slightly different:

C:\Users\<UserName>\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets

If you have a "My Code Snippets" folder, then it should go in there.

The simplest route is via Windows file explorer:

%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets

Now, whenever you type the shortcut "evh" and press TAB twice, it will insert:

/// <summary>
/// Event to indicate Description
/// </summary>
public event EventHandler Name;
/// <summary>
/// Called to signal to subscribers that Description
/// </summary>
/// <param name="e"></param>
protected virtual void OnName(EventArgs e)
    {
    EventHandler eh = Name;
    if (eh != null)
        {
        eh(this, e);
        }
    }

And prompt you to enter the description and name of the event.

History

  • First version
  • 2019-06-15 Added Visual Studio 2019.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

OriginalGriff
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
QuestionSnippet Editor Pin
ledtech320-Aug-13 10:29
memberledtech320-Aug-13 10:29 
GeneralMy vote of 5 Pin
Maciej Los11-Aug-13 2:02
protectorMaciej Los11-Aug-13 2:02 
GeneralRe: My vote of 5 Pin
OriginalGriff11-Aug-13 2:12
protectorOriginalGriff11-Aug-13 2:12 
GeneralRe: My vote of 5 Pin
Maciej Los11-Aug-13 2:23
protectorMaciej Los11-Aug-13 2:23 
GeneralMy vote of 3 Pin
Killer Coder25-Sep-12 21:06
memberKiller Coder25-Sep-12 21:06 
GeneralRe: My vote of 3 Pin
OriginalGriff25-Sep-12 21:50
protectorOriginalGriff25-Sep-12 21:50 
QuestionCool tip, thanks! I guess some guys still need to relocate them somewhere else. Pin
bensonxiong12-Jun-12 20:26
memberbensonxiong12-Jun-12 20:26 
GeneralMy vote of 4 Pin
albsilva_m2u11-Jun-12 7:49
memberalbsilva_m2u11-Jun-12 7:49 
GeneralMy vote of 5 Pin
Tim Corey9-Jun-12 17:03
professionalTim Corey9-Jun-12 17:03 
GeneralMy vote of 5 Pin
Member 43208448-Jun-12 10:41
memberMember 43208448-Jun-12 10:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 8 Jun 2012

Stats

25.3K views
244 downloads
12 bookmarked