Methods & Events / Handling Multiple Events
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
Now in this video that I've entitled Handling Multiple Events, we're going to delve a little bit deeper into Events and I kind of want to show you something a little bit under the hood here and I'll give all the appropriate disclaimers when we get there, but I want you to understand what's happening with events here because at some point you're going to want to delve a lot deeper into events and delegates and so forth. We're going to talk about delegates and I'm going to give you some, some just very basics on delegates in a different video, but there's a lot going on here. If you remember from the video entitled Event Handlers, if you've seen that video, I covered some of the basic event handling aspects of Visual Studio 2008 and C# 2008 and I want to kind of continue that here. Right now we have a single button on our form and if we double-click that button it takes us to the Event Handler for the default event for that particular object which is the Click Event. Now if I want to drop another button on that form I just go into the Toolbox, double-click on Button and there's my second button and notice the neat way that Visual Studio 2008 puts the blue lines and snaps it and helps me line it up. Now, if I double-click on button2 it will bring up another event handler for button2 and let's say that I want both of these to show that we clicked, well is there any point in writing two event handlers with the exact same code in two places? No. And this will give you an idea of some of the flexibility you have with events in Visual Studio 2008 and Visual C#. All I have to do is click on my second button, come over here to Properties and click on the little Events Icon and notice the Click Event for button2 is looking at the button2 handler in my code, right? All I have to do is click the drop-down and choose button1 Click!, okay? Now, even though I'm clicking button2, it's going to point to the button1 section of code to run. Now, if I go look at my code by clicking on Form.cs you'll notice I only have this one event handler here and when I run my application now if I click on button1 I get Clicked! and if I click on button2 I get Clicked! Because what has happened here is we have mapped it right here so that button2, the click event is handled at button1 Click! method, or routine, or function, inside my code and if I look at button1 it's button1 Click! and if you notice right here this is changing when I'm clicking on the two controls, but this is not. Now I want to show you something that is happening in the background. When I went in and clicked on button1 and said, you know, this is what I want to have handle that, if you go up here into the Solution Explorer and click this little expansion Icon next to Form1.cs and then double-click Form1.Designer it takes you into this Form1.Designer.cs page and it shows you some of the code that Microsoft's generating for you. Now the two buttons that we dropped on, notice, button1, which is an object that's instantiated from the System.Windows.Forms.Button Class and button2, but if I expand that generated code you're going to notice what has happened. First of all, on button1 they generated some code for us that says this.button1.Click, I mean that particular form and this plus equal kind of ties it to it, new system event handler this.button1 Click, OK, so this is what actually ties this button1 Click! to that object and notice on button2 we said this.button2 Click! notice new system handler, but notice the one it's pointing to it's also pointing to button1 Click!, OK, so I just wanted you to see what's going on in the background and you can actually do some of this stuff yourself, but we're not going to get into it in this course, where this is an Intro course and I won't go too deep but I did want to give you a clue about what's going on. Go out and look at some documentation and you can see this stuff happening. Now, here's the disclaimers. Don't ever edit this manually. If you want to add an event go back and do it through the Properties Window. So, I just wanted you to see that that's out there, but by using this Properties Tool on this Event Button here we can make all kinds of cool things happen with our programs and we can control what particular event is fired when a certain object is clicked, okay. I hope that helps and I hope that makes sense, but there are going to be a lot of times, for example, in an interface where you may have a number of text boxes, notice I'm on a click, I'm going to go right back out there and click again, then I'm going to go out there and click one more time, OK and so we have these text boxes in an App and any time certain things happen to these text boxes we want to know what's going on, so text is changed when an enter, when a form becomes the active, when the control becomes the active control, the form, meaning they basically jumped into it, right; I can build events for these and point all three to the same one. So this can really help you save a lot of code and help you really simplify the construction and the maintenance of your application by pointing a number of objects to the same event handler. So, a lot of stuff there and it can get pretty deep on you and kind of confusing, so go through this and play with it and make sure you understand what can happen with these events working inside Visual Studio 2008 along with Visual C# 2008.
Tutorial Information
| Course: | Introduction to Microsoft C# 2008 |
| Author: | Mark Long |
| SKU: | 34046 |
| ISBN: | 1-935320-78-5 |
| Release Date: | 2009-10-09 |
| Duration: | 7 hrs / 76 lessons |
| Work Files: |
Yes |
| 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 