Skip to content

Instantly share code, notes, and snippets.

@jjokela
Created August 11, 2014 07:26
Show Gist options
  • Save jjokela/c0cf2d604adb964edca4 to your computer and use it in GitHub Desktop.
Save jjokela/c0cf2d604adb964edca4 to your computer and use it in GitHub Desktop.
C# Delegates
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinFormsDelegateTest
{
public partial class Form1 : Form
{
// delegate for button event handler
public delegate void ButtonClickEventHandler(object sender, EventArgs e);
public Form1()
{
InitializeComponent();
// instantiate handlers
ButtonClickEventHandler firstHandler = new ButtonClickEventHandler(button1_FirstClickHandler);
ButtonClickEventHandler secondHandler = new ButtonClickEventHandler(button1_SecondClickHandler);
// assign handlers
button1.Click += new System.EventHandler(firstHandler);
button1.Click += new System.EventHandler(secondHandler);
// Now button click triggers both events and shows two message boxes
}
private void button1_FirstClickHandler(object sender, EventArgs e)
{
MessageBox.Show("Tere!");
}
private void button1_SecondClickHandler(object sender, EventArgs e)
{
MessageBox.Show("Moro!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment