Download presentation
Presentation is loading. Please wait.
Published byTheresa Jennings Modified over 9 years ago
1
Event Handling Tonga Institute of Higher Education
2
Event Driven Programs Event Driven Programs – The program responds to a user event Event - An action or occurrence detected by a program User Clicking a mouse button Pressing a key System Running out of memory An error occurring For example, your program might work like this: Show a form with a welcome message. The form will also contain a textbox, OK button and instructions to enter their name and click the Ok button. Wait for the user to respond When the user performs an action, If the user clicks the X in the top right corner, quit the program If the user typed in their name and pressed the OK button, show a message that you are happy to meet them Gives more freedom to the user. It reacts to the user’s actions Gives user the impression that they are in control Forms are event driven!
3
Current Situation We learned how to display a graphical user interface However, it doesn’t do anything Event – Something that is triggered by the user of a GUI Handler – Code that can detect and respond to events
4
Creating Events VS.NET automatically creates events for you Double clicking on the control will automatically create code to handle the most common event Code in this place will be executed when the button is clicked This place is called a subroutine
5
Demonstration Creating the most common event for a control
6
Creating Uncommon Events - 1 To create an uncommon event 1. View the code 2. Select the control you wish to create an event for 3. Select the event you wish to create
7
Creating Uncommon Events - 2 To create an uncommon event 1. View the code 2. Select the control you wish to create an event for 3. Select the event you wish to create
8
Demonstration Creating an uncommon event for a control
9
Creating Events When you using the techniques previously discussed, VS.NET automatically creates events in your code. If you want, you can create this code yourself Keyword indicates that this method handles an event Name of the Control The Event handled
10
Demonstration Working with Event Code
11
MessageBox An attention getter Displays information to the user Forces user to do something before continuing MessageBox.Show("Hi")
12
Demonstration Displaying a MessageBox when a button is clicked
13
Class Activity 1. Double Click on the button 2. Add the following code to the subroutine: MessageBox.Show("Hello World!") 3. Run the application
14
Opening A Form With Code Sometimes, we need to open a form when an event occurs. (Example: Open a student form when a button is clicked)
15
Demonstration Opening a form with code
16
Hiding a Form vs. Exiting an Application We can use events to hide and show forms frmCustomer.visible = false This is different from exiting an application Application.Exit A program can hide all of it’s forms but still be running. In this case, the program is not usable and is needlessly using the computer’s resources If we are using VS.NET, the program will always stop running when we press the stop button However, when we are using the.exe file, we must use the task manager to shut down programs that are running without any forms showing
17
Demonstration Hiding a Form vs. Exiting an Application
18
Error when Quitting a Program Due to Network Configuration Application.Exit does not work properly when a project is located on a network drive You will get this error It will work if the program is run locally We don’t need to worry about it
19
Demonstration Error Demonstration
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.