Professional Documents
Culture Documents
NET
Events
Objectives
this lecture will cover:
event-driven programming and the event loop different types of events accessing the Code Window and using its features implementing simple event handlers correcting simple syntax errors the program development cycle pseudo-code solutions for simple problems
Visual Basic
Slide 2 of 42
Event-driven programming
program waits until an event occurs User controls the program An event can occur in 3 ways:
The user doing something (e.g. Click a button) Something happening within the computer (e.g. System shutdown) Something happening within the program (e.g. Program crash)
Determine which "things" respond to which events Write code to be executed when event occurs to perform an action e.g. Calculate the average of a series of numbers
Visual Basic Slide 3 of 42
Events in VB
Events occur to objects on a form or the form
Activate events: user selects item on screen Changed events: control property modified Focus events: control gets or loses focus Key events: user interacts with keyboard Mouse events: user interacts using mouse
Visual Basic
Slide 4 of 42
Event loop
continuously monitors events
Event handlers
Event handlers have:
header identifies event handler body contains statements to be executed
Private Sub btnPress_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnPress.Click 'statements that form the body of event handler 'statement End Sub
header
body
Visual Basic
Slide 6 of 42
Method header
Name of the event handler
defaults to control name, underscore and event can be modified but normally left unchanged
Visual Basic
Slide 7 of 42
Event Type
Visual Basic Slide 8 of 42
Editor features
useful features:
standard clipboard operations automatic indentation of code spacing of statements capitalisation of keywords ensuring consistent capitalisation of names auto code completion collapsible methods colour coding
Visual Basic
Slide 9 of 42
Running Applications
Program is run by either:
Clicking the Start Debugging icon Pressing F5 or
IDE:
Compiles or builds program
Traps any syntax errors Displays message box if errors occur
Visual Basic
Visual Basic
Slide 11 of 42
Visual Basic
Slide 12 of 42
Private Sub btnPress_Click(...) Handles ... 'comment 'statements to be executed End Sub
Visual Basic
Slide 13 of 42
Types of properties
Appearance Behaviour Size & position
Visual Basic
Slide 15 of 42
required colour
Private Sub btnPink_Click(...) Handles ... 'change background colour lblPink.BackColor = Color.HotPink End Sub
Visual Basic
Slide 16 of 42
Visual Basic
Slide 17 of 42
Private Sub btnFormColour_ Click (...) Handles ... 'change form background Me.BackColor = Color.BlanchedAlmond End Sub
Visual Basic
Slide 18 of 42
Visual Basic
Slide 19 of 42
Determining object
Object that triggered event is held in sender This can determine which object was used
Private Sub btnEither_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnOne.Click, btnTwo.Click 'report button pressed lblPressed.Text = sender.Name & " pressed" End Sub
Visual Basic
Slide 20 of 42
Errors in code
Some errors are recognized by the IDE i.e. Syntax error Some may not become apparent until the program is executed i.e. Logic error and Compiler error Correcting errors can be difficult:
explanation of error may be difficult to understand due to the technical terminologies used Location of error may not be exactly as indicated by the compiler
Visual Basic Slide 21 of 42
Syntax errors
Misspelled words and missing punctuation Normally trapped by IDE as they are typed
explanation shown when cursor held over error should be corrected before running program
Visual Basic
Slide 22 of 42
Run-time errors
Program can not run as required
invalid calculation attempt to open a non-existent file
Visual Basic
Slide 23 of 42
Only discovered during program execution when input data is entered Testing can show that code has errors Debugging is finding and correcting errors
Visual Basic Slide 24 of 42
Visual Basic
Slide 25 of 42
Program development
Stage 1: Design screens
ensure screens are logical and easy to follow
Visual Basic
Slide 26 of 42
Visual Basic
Slide 27 of 42
Design screens
Single screen with 3 buttons and 1 label
Hello
Exit
btnDisplay
btnClear
btnExit
Visual Basic
Slide 28 of 42
Design pseudo-code
When btnDisplay button is clicked:
Set Text of lblGreetings to "Hello" Enable btnClear button Disable btnDisplay button
Visual Basic
Slide 29 of 42
Visual Basic
Slide 30 of 42
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click display greeting lblGreetings.Text = "Hello" btnClear.Enabled = True btnDisplay.Enabled = False End Sub Private Sub btnClear_Click() Handles btnClear.Click clear greeting lblGreetings.Clear() btnDisplay.Enabled = True btnClear.Enabled = False End Sub Private Sub btnExit_Click()Handles btnExit.Click end program End End Sub
Visual Basic Slide 31 of 42
Test program
Run program and ensure:
Clear button initially disabled Greetings label initially blank
Visual Basic
Slide 32 of 42
Summary
This lecture has covered:
the principles of event-driven programming writing code to handle events that occur within a program finding and correcting syntax errors the stages involved in developing a program
Visual Basic
Slide 33 of 42