home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Events_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-08-21  |  1.1 KB  |  42 lines

  1. using System;
  2.  
  3.  
  4. class Button {
  5.    // Declare a Click Event allowing other objects to register themselves with this event
  6.    public event EventHandler Click;
  7.  
  8.    public void SimulateClick() {
  9.       // If Click is null, then no objects have registered themselves with the event
  10.       if (Click != null) {
  11.  
  12.          // At least one object is registered with the event, fire the event         
  13.          Click(this, null);
  14.       }
  15.    }
  16. }
  17.  
  18.  
  19. class App {
  20.    // This method is called when the Button object is clicked
  21.    void OnClick(Object sender, EventArgs args) {
  22.       Console.WriteLine("App object received the Click notification from the Button object.");
  23.    }
  24.  
  25.    static void Main() {
  26.       // Create a button object
  27.       Button btn = new Button();
  28.  
  29.       // Create an instance of our App class
  30.       App app = new App();
  31.  
  32.       // Register a callback method with the Button object's Click event
  33.       btn.Click += new EventHandler(app.OnClick);
  34.  
  35.       // Simulate that the button has been clicked
  36.       btn.SimulateClick();
  37.  
  38.       Console.Write("Press Enter to close window...");
  39.       Console.Read();
  40.    }
  41. }
  42.