Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2003, 09:32 PM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default PreDefined Event Handler

Event Subscription:

this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.myEm ptyTextBox_Validating);

Event Handler:
private void myEmptyTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e){ }

Question:

Can someone tell me what, if anything , will be passed to the event handler's second parameter , e ?
 
Old June 5th, 2003, 08:36 PM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shogunofharlem
Default

In event communication, the event sender class does not know which object or method will receive (handle) the events it raises. What is needed is an intermediary (or pointer-like mechanism) between the source and the receiver. The .NET Framework defines a special type (Delegate) that provides the functionality of a function pointer.

A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback. While delegates have other uses, the discussion here focuses on the event handling functionality of delegates. The following example shows an event delegate declaration.

By convention, event delegates in the .NET Framework have two parameters, the source that raised the event and the data for the event.

Custom event delegates are needed only when an event generates event data. Many events, including some user-interface events such as mouse clicks, do not generate event data. In such situations, the event delegate provided in the class library for the no-data event, System.EventHandler, is adequate.

[u]example:</u>

This sample shows how to raise an event from your class and also how to handle the event. It defines the following classes.

AlarmClock is the class that raises the Alarm event
AlarmEventArgs defines the data for the Alarm event.
AlarmEventHandler is the delegate for the alarm event.
WakeMeUp is a class that has a method, AlarmRang, that handles the alarm event.
AlarmDriver is a class that demonstrates how events are wired. It instantiates AlarmClock and WakeMeUp. It then instantiates the AlarmEventHandler delegate with a reference to the AlarmRang method of the WakeMeUp instance. AlarmDriver completes the event wiring by registering the delegate with the instance of AlarmClock, using the += syntax for adding delegates to an event.
To compile and run this sample

Copy the code below to a source file (such as EventSample.cs.or EventSample.vb).
From the directory that contains source file, execute the following command:
[C#]
csc /r:System.dll EventSample.cs

Run the executable that is created, EventSample.exe.

// EventSample.cs.
//
namespace EventSample
{
   using System;
   using System.ComponentModel;

   // Class that contains the data for
   // the alarm event. Derives from System.EventArgs.
   //
   public class AlarmEventArgs : EventArgs
   {
      private readonly bool snoozePressed ;
      private readonly int nrings;

      //Constructor.
      //
      public AlarmEventArgs(bool snoozePressed, int nrings)
      {
         this.snoozePressed = snoozePressed;
         this.nrings = nrings;
      }

      // The NumRings property returns the number of rings
      // that the alarm clock has sounded when the alarm event
      // is generated.
      //
      public int NumRings
      {
         get { return nrings;}
      }

      // The SnoozePressed property indicates whether the snooze
      // button is pressed on the alarm when the alarm event is generated.
      //
      public bool SnoozePressed
      {
         get {return snoozePressed;}
      }

      // The AlarmText property that contains the wake-up message.
      //
      public string AlarmText
      {
         get
         {
            if (snoozePressed)
            {
               return ("Wake Up!!! Snooze time is over.");
            }
            else
            {
               return ("Wake Up!");
            }
         }
      }
   }

   // Delegate declaration.
   //
   public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);

   // The Alarm class that raises the alarm event.
   //
   public class AlarmClock
   {
      private bool snoozePressed = false;
      private int nrings = 0;
      private bool stop = false;

      // The Stop property indicates whether the
      // alarm should be turned off.
      //
      public bool Stop
      {
         get {return stop;}
         set {stop = value;}
      }

      // The SnoozePressed property indicates whether the snooze
      // button is pressed on the alarm when the alarm event is generated.
      //
      public bool SnoozePressed
      {
         get {return snoozePressed;}
         set {snoozePressed = value;}
      }
   // The event member that is of type AlarmEventHandler.
      //
      public event AlarmEventHandler Alarm;

      // The protected OnAlarm method raises the event by invoking
      // the delegates. The sender is always this, the current instance
      // of the class.
      //
      protected virtual void OnAlarm(AlarmEventArgs e)
      {
         if (Alarm != null)
         {
            // Invokes the delegates.
            Alarm(this, e);
         }
      }

      // This alarm clock does not have
      // a user interface.
      // To simulate the alarm mechanism it has a loop
      // that raises the alarm event at every iteration
      // with a time delay of 300 milliseconds,
      // if snooze is not pressed. If snooze is pressed,
      // the time delay is 1000 milliseconds.
      //
      public void Start()
      {
         for (;;)
         {
            nrings++;
            if (stop)
            {
               break;
            }

            else if (snoozePressed)
            {
               System.Threading.Thread.Sleep(1000);
               {
                  AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
                     nrings);
                  OnAlarm(e);
               }
            }
            else
            {
               System.Threading.Thread.Sleep(300);
               AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
                  nrings);
               OnAlarm(e);
            }
         }
      }
   }

   // The WakeMeUp class that has a method AlarmRang that handles the
   // alarm event.
   //
   public class WakeMeUp
   {

      public void AlarmRang(object sender, AlarmEventArgs e)
      {

         Console.WriteLine(e.AlarmText +"\n");

         if (!(e.SnoozePressed))
         {
            if (e.NumRings % 10 == 0)
            {
               Console.WriteLine(" Let alarm ring? Enter Y");
               Console.WriteLine(" Press Snooze? Enter N");
               Console.WriteLine(" Stop Alarm? Enter Q");
               String input = Console.ReadLine();

               if (input.Equals("Y") ||input.Equals("y")) return;

               else if (input.Equals("N") || input.Equals("n"))
               {
                  ((AlarmClock)sender).SnoozePressed = true;
                  return;
               }
               else
               {
                  ((AlarmClock)sender).Stop = true;
                  return;
               }
            }
         }
         else
         {
            Console.WriteLine(" Let alarm ring? Enter Y");
            Console.WriteLine(" Stop Alarm? Enter Q");
            String input = Console.ReadLine();
            if (input.Equals("Y") || input.Equals("y")) return;
            else
            {
               ((AlarmClock)sender).Stop = true;
               return;
            }
         }
      }
   }



   // The driver class that hooks up the event handling method of
   // WakeMeUp to the alarm event of an Alarm object using a delegate.
   // In a forms-based application, the driver class is the
   // form.
   //
   public class AlarmDriver
   {
      public static void Main (string[] args)
      {
         // Instantiates the event receiver.
         WakeMeUp w= new WakeMeUp();

         // Instantiates the event source.
         AlarmClock clock = new AlarmClock();

         // Wires the AlarmRang method to the Alarm event.
         clock.Alarm += new AlarmEventHandler(w.AlarmRang);

         clock.Start();
      }
   }
}
 
Old June 6th, 2003, 07:57 AM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, that is quite a thourough explaination of event handling and I appreciate it.

My guess is that in this case, nothing gets passed to the Event Handler's second parameter, as that parameter is there doing it's job as part of the event signature.

thanks, mark





Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW TO ADD AN EVENT HANDLER pallone .NET Framework 2.0 10 September 16th, 2008 12:28 PM
Mouse Event Handler daniel.mihalcea C# 2008 aka C# 3.0 1 September 8th, 2008 06:30 AM
Event handler samir_katore Pro VB 6 6 June 8th, 2006 01:22 PM
Event handler problem carro123 Javascript How-To 1 May 20th, 2005 03:30 PM
PreDefined Event Handler mark C# 1 June 7th, 2003 04:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.