C# Tutorial - EventsWindows applications in general are event driven. What this means is that the application does nothing until the user interacts with it in some manner. Once the user has interacted, an event occurs that can be handled by the application in order to achieve some goal. One of the simplest events to understand is a button being pressed and released on a form. This causes the button's Click event to occur (to be raised). In C#, an event enables a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers. Subscribing to EventsIn our application, the Form subscribes to the Click event of the Button class. We did this simply by double-clicking on the button, but this only worked because that is the default action performed by the IDE. To subscribe to any event from a control use the following method:
Alternatively, you can create the code manually by creating an event handler that matches the delegate signature for the event (as shown in step 3 above) in the subscriber class (in our case the form). Then we pass the name of this method to the Click event of the button class, so that when the event is raised, the correct method is performed. this.button1.Click += new System.EventHandler(button1_Click);
|