OKIEWARDOYO

C#.NET No.9-5

Dec 19th, 2012
5,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. //Eventdemo.cs
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. // custom delegate
  7. public delegate void Startdelegate();
  8.  
  9. class Eventdemo : Form
  10. {
  11.     // custom event
  12.     public event Startdelegate StartEvent;
  13.  
  14.     public Eventdemo()
  15.     {
  16.         Button clickMe = new Button();
  17.  
  18.         clickMe.Parent = this;
  19.         clickMe.Text = "Click Me";
  20.         clickMe.Location = new Point(
  21.             (ClientSize.Width - clickMe.Width) /2,
  22.             (ClientSize.Height - clickMe.Height)/2);
  23.  
  24.         // an EventHandler delegate is assigned
  25.         // to the button's Click event
  26.         clickMe.Click += new EventHandler(OnClickMeClicked);
  27.  
  28.         // our custom "Startdelegate" delegate is assigned
  29.         // to our custom "StartEvent" event.
  30.         StartEvent += new Startdelegate(OnStartEvent);
  31.  
  32.         // fire our custom event
  33.         StartEvent();
  34.     }
  35.  
  36.     // this method is called when the "clickMe" button is pressed
  37.     public void OnClickMeClicked(object sender, EventArgs ea)
  38.     {
  39.         MessageBox.Show("You Clicked My Button!");
  40.     }
  41.  
  42.     // this method is called when the "StartEvent" Event is fired
  43.     public void OnStartEvent()
  44.     {
  45.         MessageBox.Show("I Just Started!");
  46.     }
  47.  
  48.     static void Main(string[] args)
  49.     {
  50.         Application.Run(new Eventdemo());
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment