1 Replies - 1551 Views - Last Post: 07 November 2009 - 01:30 AM Rate Topic: -----

#1 Becky.b   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-November 09

Event handler

Post icon  Posted 07 November 2009 - 01:01 AM

Hey guys I am a newbie towards visual c# Do you guys have great explaination on event handler, cause I seems to have difficulty in understanding it! Thanks man!

Hope to hear from you guys soon! You guys help is greatly appreciated!


Is This A Good Question/Topic? 0
  • +

Replies To: Event handler

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: Event handler

Posted 07 November 2009 - 01:30 AM

Think of the EventHandler as the glue between a control, such as a button or textbox, and the event code which is called in response to the user manipulating that control.

this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);

This is a line which adds an EventHandler to a Numeric Up Down control. When the control's value is changed, it passes the action to each event handler registered with the control. This event handler in turn passes the event to a function called "numericUpDown1_ValueChanged". And of course it is that event function is where you write your code to handle the event and manipulate data and other controls.

Notice how it uses the += operator which says take the eventhandlers for ValueChanged and append onto it, then assign it back to ValueChanged. What exactly does this mean? Well it means that you can actually add several handlers in response to one event. Besides just calling numericUpDown1_ValueChanged I could tack on another event. Here I attach two events which are both called when the value changes on the control...

this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
this.numericUpDown1.ValueChanged += new System.EventHandler(this.anotherNumericUpDown1_ValueChanged);

Now both of these functions will be called in response to the single event. At its heart the EventHandler merely delegates the event between the control and the code that is suppose to respond to the event. This is known in programming circles as "Wiring up the Control" or "Wiring up the event to the control"

Hope that helps.

"At DIC we be wiring up code ninjas... lots of sugar and we are totally wired all night!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1