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.
Now let's take a look at how those events would be handled in Visual Basic .NET. Now you'll notice a couple of differences here, and I'll switch back and forth and I'll show you the main differences. First of all we can use classes inside other code structures, inside our modules and so forth. And so this public class actually could have been inside here; but we won't get into that right now. But notice if you go back to VB6, we had a class module and you had put your code in there and save that module, and compile it. And in VB .NET you just simply declare it as a class and everything in between there becomes a member of that class. So in our class definition, one of the first things we do here is say - Public Event Test - and notice I don't have all of the code in here. This is just to help you understand the skeletal aspects of events. I declare the event by saying public event test. And this is the name of our event: test. We will be passing this event ByVal s As string; then in our class, this is just the first thing we have to do for an event is to first of all declare it inside our class. And then we have to raise it from inside our class. And so here is a raise event test; now it is inside a sub and every time this sub is called on the object that is created off of this class, this event will fire. OK, and this event will raise this event test, and kick it up here and event test will begin to broadcast out that it needs to be handled, it needs to be listened to, and so what's going to happen then, is because of the handles keyword right here. And notice - handles X.test - that when this event goes, it will go automatically go down here and be handled right here. So let's look at our code in our form one VB. Notice, we create a new instance of our class the event right here. We create this new instance as X, and so X now exists as an instantiated object of that class, and so, what we are going to do is, when anybody clicks on the button OK on our form, the click event for the button OK will fire or will call the fire event right here, which will in turn raise the event test. And when that happens the test event will fire, it will be handled right here and whatever code we put here will take place. Now a couple of things I want you to notice, in VB6 all of our events were handled based on the object name, and underline and then the event. If you'll notice right here, this was tied to handling test in VB6 by the virtue of the fact that it used this name right here. And so, if you ever changed this name, it created a problem for our code. Well in .NET the magic now is this handles keyword, because now this really don't matter. I could change this name all I want to, but the fact of the matter is that the public event test is going to be handled right here every time because it says handles X.test. The handles keyword is what ties this particular sub to this particular event; this is what makes it the handler of that event. Now let's go look at this in code, and I want to show you exactly of what's happening here. So, let me start off down here at the bottom, this is a windows application, let me show you the form. There is just a simple button in the middle of the form and when we click it that click event is going to kick all this off. So the first thing I did here was created a class called the event class and I declared my event - Public Event SendMessage - and it takes an argument of a string. And then second thing I did was, have to raise that event, had to do something to cause this event to be fired off. And so, I executed - RaiseEvent SendMessage - and passed it some text, whenever somebody calls the trigger event method, or the trigger event method that is going to be in this object that comes from this class. And so at this point we are basically through with the class. We would probably have other functionality in our class, but for now we are just going to have this event in here. Now in my code, in my client side code if you want to call it that, first thing I want to do is instantiate my object, and I am going to instantiate it with - Private WithEvents - and we are going to call this object ec, and it's a new instantiation of the event class. Then when somebody clicks on button one on our form, notice this particular Sub - Handles Button1.Click. Ok, now it's just kind of interesting and I'll show in the next video, if there was a button two, I could put, you know - Handles Button2.Click - but I don't have one out there. I'll show you later. But it is real easy to have events handled in .NET as compared to six. So when they click the button, we are going to call our ec trigger event which is going to raise that test; then notice what is going to happen. We are then going to tell it that when the event send message happens, that this particular sub right here is going to handle it. And so message sent (MsgSent) is going to handle ec SendMessage. So whenever the SendMessage event gets fired off of that object, then there you go. Its going to happen right here. And you'll see a message box; so let's run this and take a look at it and see what happens. Now the form will pop up here, and when I click on it, all it's going to do is call that method off of that object that raises the event, then it comes out and gets handler and simply says the button has been clicked. And if you'll remember, back over here that's exactly what we passed- "the button has been clicked". So this is what we passed out when we set this event out, it says hey somebody handle this thing, and right here handles ec SendMessage says I'll handle anything that comes from ec's SendMessage event. And sure enough it dropped our text, the button has been clicked into the message box. And it put "message sent from the event class" as the title of that message box.
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |