Events / Using a Class
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Ok now, I'm going to show you a different way to handle events, and we won't even use the with events keyword. We actually are going to use a class to handle all of our events for us. I'm going to create a new class from the other location, and I'm just going to copy it. Actually, I'm going to cut it. I'm going to paste it into my class down here. Now I'm going to have to make it public so that I can see it. Notice I'm going to take the handles keyword off and so there is no more handles, I'm not going to change anything in the class; we still are declaring our event and then we raise our event. The only difference is that we've moved our handler out to a separate class called Mark. And so now what we will do, is up here we'll take the with events off, and we would just create ec as a new event class. And then we are also going to create x as a new Mark. So we can get to our new class down there that has our handler in it. And what I'll do here is on the form load I'll simply add - AddHandler x.AddHandler, ec.SendMessage. So we are going to handle ec.SendMessage. And the address of where we'll handle that is - x.MsgSent. So notice what we told this thing, we added a handler we said that ec.SendMessage is going to be handled by x.MsgSent, and we instantiated Mark as x and event class as ec. And so now when we run this, if I didn't fat finger anything we should get it. And notice our event's working. Now, let me go back and recap this for you to make sure that you understand what I did. Ah all I did was simply move my code for event handlers out into the separate class and then I just Dimmed my objects as normal, but this time I put on the load of the form, I could put it here on the button click, add the handler for ec.SendMessage as the address of x.MsgSent. This is very, well actually this is delegate, we are doing some delegates in the background here. And if you remember how we did delegates with address of, ah this the delegate pointer to that particular location. So this is something else that you can do here and also later on in my code, I may want to actually remove handler. I can do a remove handler and take that back off of there. That's a little bit more dynamic, there is a lot more you can do with these. We're constrained by time here, get out into some of the documentation and start to play with events. There is an awful lot you can do with those things in VB .NET.
Tutorial Information
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 