Click here to Skip to main content
Click here to Skip to main content

A Simple Code Snippet to Add an Event

, 8 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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="">
  <CodeSnippet Format="1.0.0">
      <Title>Event Template</Title>
      <Author>Paul (OriginalGriff) Griffin</Author>
      <Description>Creates an Event template - includes the Event and the OnEvent method</Description>
      <SnippetTypes />
      <Keywords />
      <References />
      <Imports />
        <Literal Editable="true">
        <Literal Editable="true">
      <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);

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#

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

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.


  • First version


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


About the Author

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?
Follow on   Google+

Comments and Discussions

GeneralMessage Automatically Removed PinmemberMember 1067839717-Mar-14 22:10 
GeneralMessage Automatically Removed PinmemberMember 1067834517-Mar-14 21:52 
GeneralMessage Automatically Removed Pinmemberajk81213-Mar-14 1:22 
QuestionSnippet Editor Pinmemberledtech320-Aug-13 11:29 
GeneralMy vote of 5 PinmvpMaciej Los11-Aug-13 3:02 
GeneralRe: My vote of 5 PinprotectorOriginalGriff11-Aug-13 3:12 
GeneralRe: My vote of 5 PinmvpMaciej Los11-Aug-13 3:23 
GeneralMy vote of 3 PinmemberKapil Manspure25-Sep-12 22:06 
GeneralRe: My vote of 3 PinmvpOriginalGriff25-Sep-12 22:50 
QuestionCool tip, thanks! I guess some guys still need to relocate them somewhere else. Pinmemberbensonxiong12-Jun-12 21:26 
GeneralMy vote of 4 Pinmemberalbsilva_m2u11-Jun-12 8:49 
GeneralMy vote of 5 PinmemberTim Corey9-Jun-12 18:03 
GeneralMy vote of 5 PinmemberMember 43208448-Jun-12 11:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 8 Jun 2012
Article Copyright 2012 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid