CO1403 Visual Basic Programming - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CO1403 Visual Basic Programming

Description:

It is fair to say that we have not even started looking at how we write Visual Basic code ... You are not expected to be able to reel these off from memory ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 26
Provided by: ucla9
Category:

less

Transcript and Presenter's Notes

Title: CO1403 Visual Basic Programming


1
CO1403 Visual Basic Programming
  • Week 4

2
Introduction
  • This week we will look at,
  • Programming models
  • Sequential programming
  • Procedural programming
  • Event-driven programming
  • Events
  • Event procedures
  • Look at some common events
  • Pages 32-35

3
Programming Models
  • It is fair to say that we have not even started
    looking at how we write Visual Basic code
  • Before you can confidently write your own code,
    you should be able to predict how that code will
    execute
  • One of the important things about predicting how
    your code will run is to understand in what
    sequence your lines of code will execute

4
Programming Models
  • Our pseudo-code examples have all been of a
    similar ilk
  • The algorithm starts at the first line of
    pseudo-code, and ends at the last line
  • There is no particular sequence of events as
    specified within the pseudo-code
  • However, when writing a Windows application, a
    user can interact with an application in many
    different ways, and are not always prescribed to
    take a particular series of steps

5
Programming Models
  • Different programming models have matured over
    the years
  • We are about to look at three different models

6
Sequential Programming
  • Sequential programming is when one line of code
    is executed after the previous line of code
  • Sequential programs have only ONE starting point
  • Sequential programs have only ONE ending point

7
Sequential Programming
  • To give examples of sequential programs will be
    quite difficult
  • People have now gotten used to Windows
    applications, and few people will have used a DOS
    application
  • A DOS application ran slightly differently from a
    Windows application

8
Sequential Programming
  • A DOS application would operate in the following
    manner,
  • You would start the application
  • You would follow a pre-determined series of steps
  • You would finish the program

9
Sequential Programming
  • An example of sequential programming,
  • intResult 10 3
  • intResult intResult 7
  • print (intResult)
  • Program starts at the first line of code, and
    finishes after executing the last line of code
  • The sequence of execution is set as you are
    writing the code

10
Procedural Programming
  • Procedural programming is an extension of
    sequential programming
  • Works by grouping together a series of lines of
    code in to a procedure
  • Once lines are grouped together, we can give the
    group an identifier
  • We can then use this identifier in the rest of
    our program in order to execute the grouped
    together lines of code

11
Procedural Programming
  • Procedural programming is the same as sequential
    programming in that
  • we have a start and a stop
  • and that the lines of code are executed in a
    predetermined order
  • Grouping together lines of code gives us several
    advantages
  • It reuses code that we have typed, rather than
    having to type it all out again
  • It prevents mistakes from creeping in to our
    programs

12
Procedural Programming
  • A 10
  • B 20
  • C A B
  • EdtResult.Text cstr(C)
  • A 20
  • B 30
  • C A B
  • EdtResult.Text cstr(C)
  • A 30
  • B 40
  • C A B

13
Procedural Programming
  • Can be rewritten as,
  • Sub DoAdd(A, B)
  • C A B
  • EdtResult.Text cstr(C)
  • End sub
  • DoAdd(10, 20)
  • DoAdd(20, 30)
  • DoAdd(30, 40)

14
Event-Driven
  • Event-driven programming is used in most (if not
    all) Windows programming languages
  • It is the model followed in Visual Basic
  • It is slightly different than the previous
    examples
  • Users interact with Windows applications in a
    non-predetermined sequence
  • Our programs REACT to user interaction (rather
    than predetermine the order)

15
Event-Driven (Drawn on Board)
  • An event-driven program does the following,
  • When a program starts it goes in to an infinite
    loop
  • The program sits and does nothing except waits
    for a user to interact with the application in
    some way
  • When the program detects the user has interacted,
    it exits the infinite loop and executes some
    lines of code
  • Once these lines of code are executed (in their
    predetermined order), the program returns to the
    infinite loop
  • The infinite loop is only ended when the user
    closes the program

16
Event-Driven
  • It is important to understand how the
    event-driven model works
  • Fortunately, you dont have to understand how to
    write the infinite loop, or how to break out of
    the infinite loop
  • Microsoft have written this for you already
  • Whenever you create a VB application, this
    event-driven programming model is built in to
    your application

17
Event-Driven
  • Looking at an example that should be familiar to
    you all,
  • Applying Microsoft Word to this model
  • Double click Word icon and program starts
  • Once application is loaded in to memory, it just
    sits there and does nothing (Word now in its
    infinite loop waiting for some kind of
    interaction)
  • If you type a key, Word recognises this and draws
    the letter on the screen. In the background, a
    keypress was detected by the infinite loop, and
    control was passed to some code that drew the
    letter on the screen

18
Event-Driven
  • After the letter is drawn on the screen, control
    is passed back to the infinite loop until some
    other interaction is detected
  • No matter what kind of interaction occurs, Word
    merely sits there and waits for you to do
    something
  • Once you have finished editing your document, you
    save your work (another interaction) and exit the
    application
  • This model can be applied to most (if not all)
    Windows application

19
Events
  • Events are important in event-driven programming
  • An event is essentially the thing that causes the
    program to leave the infinite loop
  • An event is when something happens
  • An event is normally caused by some kind of user
    interaction (although this is NOT always the case)

20
Events
  • For example,
  • When we press a key, a keyboard event is
    triggered, and our program drew the key on the
    screen
  • Likewise, every time we move our mouse, a mouse
    event is triggered, and the mouse cursor is
    redrawn at a different position on the screen

21
Event-Procedures
  • When an event is triggered, our infinite loop
    checks to see if it can execute some code in
    response to that event
  • As a developer, we can write an event-procedure
  • We can associate an event-procedure with a
    particular event, and the code that we write
    within that event-procedure is automatically
    executed

22
Event-Procedures
  • Fortunately, Visual Basic makes all this process
    very easy
  • VB automatically adds the infinite loop to our
    programs (run a VB application to prove it)
  • VB automatically associates an event with an
    event procedure
  • All we have to do as developers is write the
    event-procedure

23
Common Events
  • There are events in Windows that are common
    across many controls
  • Some of those are listed here
  • Click event happens when you click the mouse
    button on a control
  • GotFocus event happens when the control
    receives focus
  • KeyDown event happens when you press a key

24
Common Events
  • Paint event happens when the screen is
    repainted
  • Load event happens when a form is loaded for
    the first time
  • UnLoad event happens when a form is unloaded
    from memory

25
Common Events
  • DO NOT WORRY if you cant remember what all the
    events are called
  • You are not expected to be able to reel these off
    from memory
  • However, you WILL, over time, get to know what
    all these events are used for, and you will quite
    easily refer to the different events
Write a Comment
User Comments (0)
About PowerShow.com