Created
August 11, 2014 07:26
-
-
Save jjokela/c0cf2d604adb964edca4 to your computer and use it in GitHub Desktop.
C# Delegates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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