Top Banner
CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3
28

CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Jan 02, 2016

Download

Documents

Noreen Page
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

CSE2207/CSE3007Rapid Applications

Programming with Windows

Week 3

Page 2: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

CSE2207/CSE3007 Week 3

Prototyping, step by stepDesigning an Application

the GUI and W95 conventions TOE charts

Interface Navigation, Form HandlingWriting Code: the Code EditorProgramming Fundamentals

Page 3: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Prototyping, Step by Step

Start by laying out the formsSome basic guidelines:

Humans don’t think in numbers (let the computer create primary keys)

Use lists for user choices (no validation!)support the typing of the first few letters

Never force the user to use or enter codesa displayed code is a defeat!

Page 4: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Prototyping, Step by Step...

Establish Standards Placement of “Back”, “Exit”, “Cancel”

buttons? Fonts, colors, use of “Help”, function keys Size, placement of forms? Content of menus?

All forms must display exactly the same menus always, with the inappropriate options disabled

• mnuFileSave.Enabled = False

Names of objects

Page 5: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Prototyping, Step by Step...

Break up the Job into Tasks The user’s mental conceptualisation occurs here Sit with the user and sketch out forms, form

linkages Get the user to talk about standard tasks and

procedures. Exceptions should come later. Sub-tasking is necessary where tasks take >1

screen Use Frames, Tabbed Dialog control to isolate

different tasks on the same form Support closure: user should never have to

remember information across screens: so repeat information across forms if necessary

Page 6: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Prototyping, Step by Step...Pass Screen Ownership to the User

The user can enter data in any sequence, make mistakes etc

Consider/discuss when, how editing/validation should take place:only when the user passes control to the

program by clicking the “Done” or “Save” buttons?

As soon as the entry field loses focus?Ok to use masked edit controls?

Remember. you are not the boss (BUT, it is YOUR RESPONSIBILITY to ensure that

the data entered by the user is valid before you use it or write it to the database)

Page 7: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Designing an Application

Identify the tasks for the whole application each form/screen

Decide forms relationships, interactionFor each form

specify tasks, objects(controls) involved in those tasks, events for those objects (TOE chart)

start with a paper sketch, and involve the user

Page 8: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Designing an Application...TOE chart can be later used in form-level

testingCategories of Form Tasks:

Input from user/database/files Validation, calculation Output to screen/files/printer/database Start/End processing Repeated tasks Interaction with user, other forms

Printing Documentation, forms (Design Time) File|Print then Form Image, Code,

or Form as Text (see Example 4) (Run Time) frmInput.PrintForm

Page 9: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

The Task

The company 'Skate-Away Sales'sells skateboards by phone. Theskateboards cost $100 each andcome in 2 colors: blue and yellow.

Currently a team of 20 sales peoplewrite the customer and order detailson a form. They then calculate theorder total and price, including a 5%sales tax.

This system is time-consuming andprone to errors. Also they want tosend confirmation notices to thecustomers, and currently the hand-written forms are not suitable forthis.

They would like a computerisedsystem to do all this.

Page 10: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

The GUI and W95

Basic Windows ‘skill-set’ clicking, selecting, dragging, menu selection

User controls program flow complete visual representation of salient

program features and choicescontinual display only for options in continual useplace options in menu bar to avoid clutter

provide feedbackhourglass cursor, gauge, status bar, dialog box

Page 11: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Naming Objects VB will automatically name each object you create e.g.

Project1, Form1, List1 etc ALWAYS change the default name as soon as you create

the object Naming conventions:

the first 3 characters indicate the type of object remaining characters represent its purpose:frmSplash, txtName, cmdQuit, fraNewDetails

Naming Rules: must begin with a letter consists only of letters, numbers, underscore 40 characters or less

Page 12: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Object Naming Conventions

http://msdn.microsoft.com/library/

devprods/vs6/vbasic/vbcon98/vbconobjectnamingconventions.ht

m

Page 13: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Form Handling... When a VB app starts, the startup form is

displayed, then the following events occur

Initialize: occurs ONCE when an instance of a form is created; use to initialize data e.g. read a file into an array the window, controls do not yet exist.

Load: occurs ONCE, when form is loaded into memory use to initialize controls, data, but cannot set focus

to a control (not visible yet) General/Declarations section is automatically

executed

Page 14: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Form HandlingActivate: occurs whenever the form has focus occurs before a related GotFocus() event triggered by user click, a form’s Show, SetFocus methods; Use e.g. to write

code to highlight the text in a particular text box. Change form’s BorderStyle, StartUpPosition, MaxButton, MinButton, Visible

properties Use a Splash Screen to ‘mask’ setup tasks (Example 2) A VB application can begin with a startup form or startup procedure called Sub

Main() (See Examples 1,2)

A VB Application can begin with a startup form or startup procedure called Sub Main() (See Examples 1, 2) Project|Project Properties|General Tab|Startup Object

Page 15: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Form Handling...

An event-driven application ends when: all forms are closed no code is executing (any hidden forms?) (See

Ex3) End ends the app. immediately: no further code is

processed, no further events occur When a form is unloaded, 3 events occur:

QueryUnload: occurs in all forms before any are unloaded; cancel unload via the Cancel parameter

Unload: Occurs as each form is unloaded Also has a Cancel parameter.

Terminate: indicates that VB has removed the form and its module from memory.

Page 16: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Interface Styles (Introduction)Multiple Document Interface

An interface which allows the user to view and work with multiple windows (forms) which are all displayed within a single container (parent) form e.g. MS Word, Excel

Max of 2 active windows (1 parent, 1 child)Single Document Interface

An interface which allows the user to view and work with multiple windows (forms) which are independent of each other, and can occupy the whole screen if required. (e.g. WordPad)

Page 17: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Interface Styles (Introduction)...

The Explorer-style Interface A single window containing two panes or

regions, usually consisting of a tree or hierarchical view on the left and a display area on the right, as in the Microsoft Windows Explorer.

This type of interface lends itself to navigating or browsing large numbers of documents, pictures, or files.

Experiment with styles in the App. Wizard

Page 18: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Programming Fundamentals

Variables, Data Types, Constants, ScopeModules, Sub Procedures, Function

Procedures

Page 19: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Variables

Variables can be used for storing data/information which is not stored in a control on the user interface.

Variables hold values that can be changed when the program runs.

A variable has a name (the word used to refer to the variable in your code) and a data type.

Convention: use first 3 characters of name to indicate data type (some developers also indicate scope e.g. Dim pintTotal as Integer)

Page 20: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Data Types The Variant data type handles all types of fundamental data and

converts between them automatically. Integer% (int) 2 bytes –32,768 to 32,767 Long & (lng) 4 bytes –2,147,483,648 to 2,147,483,647 Single! (sng) 4 bytes (Floating point) Double# (dbl) 8 bytes (Floating point) Currency@ (cur) 8 bytes 15.4 String$ (str) 1 byte per character 0 to approximately 65,500(F)

+ 10 for Var Lgth 0 to 2 billion (V) Byte (byt) 1 byte 0 to 255 Boolean (bln) 2 bytes True or False Date (dat) 8 bytes January 1, 100 to December

31, 9999 Object (obj) 4 bytes Any Object reference Variant (vnt) 16-22 bytes + 1 byte for each character

Page 21: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Data Types...

ExamplesintEmpAge% = 24 ‘Implicit variable declarationvntEmpAge = 24 ‘VB default - VariantDim intEmpAge As Integer intEmpAge

= 24Dim datBirthday As Date datBirthday = #April 2, 1974#Dim curCost as Currency curCost = 2999.95Dim strName, strAddress as String

strAddress = txtStreet.Text & “Street”Dim strID As String * 7 ‘ A fixed-length string with

room for exactly 7 charactersStatic lngTotalStudents as Long

Page 22: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Converting an Expression

Function/Ret Type Function/Ret Type

CBool Boolean Cbyte ByteCCur Currency CDate DateCDbl Double CInt IntegerCLng Long CSng SingleCStr String CVErr ErrorCVar Variant(See Example 5)

Page 23: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Constants

Constants are used to hold values that will not change for the duration of the program

Literal constant e.g. 5 Symbolic constant

a memory location whose CONTENTS do not change for the duration of the program

code is more readable, and easier to modify Values assigned may NOT contain variables,

functionsPublic Const conSngPi as Single = 3.141593Const conHeading = “Lucky Lou’s Betting Agency” ORConst HEADING = “Lucky Lou’s Betting Agency”

Page 24: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Variables and Constants

Must begin with a letter, and contain only letters, numbers and the _ character.

Must be 40 or fewer characters in length.Cannot be a reserved wordForce VB to check for undeclared

variables: Manually enter ‘Option Explicit’ in the

General Declarations section of EVERY form and code module OR (recommended!!)

Tools|Options|EditorTab|Require Variables Declaration

Page 25: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Scope of Variables and Constants

A local (or procedure level) variable is declared with Dim in a procedure, is visible only from its own procedure, and exists in memory only when the procedure is running. EXCEPTION: Static variables will retain their value

A module-level variable is declared in the module’s General (object) Declarations (Proc) Section, using the Private keyword. Can be used by every procedure in the module.

A global variable is declared with the Public keyword in the General Declarations Section of a Module. It is available to every line of code in your application.

Page 26: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Scope of Variables and Constants

All variables declared in the general section of a Standard (Code) Module: are public throughout the whole application,

unless the Private keyword is used are available for the life of the application

All variables declared in the general section of a Form Module: are public throughout the whole application,

unless the Private keyword is used public form variables are treated as properties

of the form and continue to exist even after the form is unloaded (Example 6)

Page 27: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Modules, Procedures and Functions VB modules: Form, Standard (or Code), Class

Project|Add|Module All contain code that can be shared at various levels

of scope in the application. (Example 6) Code is written as an Event, General procedure

Event procedure is triggered by the user, system General procedure must be called by your

program Function Procedures and Sub procedures are types of

General procedures named sequence of statements executed as a

single unit Function returns a value, Sub(procedure) does not

Page 28: CSE2207/CSE3007 Rapid Applications Programming with Windows Week 3.

Using Procedures, Functions

(In a Code Module or in the General Declarations Section of a Form, if application has only 1 form)

Sub Multiply(intFirst As Integer, intSecond As Integer)lngGlobalVar = inFirst * inSecond

End Sub Then call it: Call Multiply(3, 5)Function Multiply(ByVal intFirst As Integer, ByVal

intSecond as Integer) As LongMultiply = CLng(intFirst * intSecond)

End Function Then call it: lngProduct = Multiply(236, 9999) See Example 7