Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# - Event Handlers, Part I


  • Please log in to reply
No replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 26 August 2011 - 11:35 AM

Hello everyone! Today we'll be covering basic control event handlers, I hope you enjoy it. :)

Event List
Each control has its default event handler that is automaticlly created when its double clicked. The button has its Click() event, the textBox with the TextChanged() event. Sometimes you want to add an event that also pretains to the control but isnt the defualt event. The easiet way to do this is click the control of which you want to create an event for and goto it's properties window. From here you should see a lightining bolt, click this and you should see a large list of events you can create for the control.
events-Tut.png
Once you've found the new event you want to create double click it and you'll be directed back to code view with a newly created event function looking something like this:
events-Tut-New.png

Now you maybe wondering why you cant just save yourself some trouble and just write
private void button1_FontChanged(object sender, EventArgs e)
{

}
Instead of adding it through the events list? Well you can do this, but this only creates the event function, you would still have to create an event handler in the Designer.cs file. Since were at it let's go over how to do this.


Designer.cs & Form1.cs Coded Events.
First open your Designer.cs file and find the control you want to add an event for, then add this.
 this.controlNameHere.EventNameHERE += new System.EventHandler(this.FunctionName);

Here is what mine looks like with the fontChanged event in place.
events-Tut-Design.png

Don't forget to add this to your form code when your done in the designer.
private void FunctionName(object sender, EventArgs e)
{

}

There! Now you know how to use both ways of creating an event, but there is still one other way that is slighlty easier then the Designer.cs method.

This method requires you to create a new eventHandler much like we did in the Designer, except this time we add the code in our Form.
controlName.eventName += new EventHandler(funtionName);    

Here's what mine looks like, event function included.
events-Tut-Form.png


Tip: When adding a new event to a control you will see a lightning bolt next to the event in the auto-complete window.
events-Tut-Auto.png


Thats the basics of creating event handlers in C#. If you want to know how to add an event that isnt covered in the event list? Well that will be covered in Part II.

As always comments, questions, or +rep welcome, and dont forget to rate this thread.
Thanks ~ Committed. :)
  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download