470 likes | 717 Views
CSCI110 Event-Driven Programming with Visual Basic. Fundamentals 1. Problem Solving and Computer Programming. Algorithms Program Design Software Lifecycle. Algorithms. An algorithm is a sequence of precise instructions (written in English) that leads to the solution to a problem.
E N D
Problem Solving and Computer Programming Algorithms Program Design Software Lifecycle
Algorithms An algorithm is a sequence of precise instructions (written in English) that leads to the solution to a problem. An algorithm can be compared to a recipe to bake a cake, a set of directions to enroll for a class or a set of instructions to install an operating system...
Start Flowchart of a program to calculate the average of 3 numbers input 3 numbers calculate total divide total by 3 output result End
input the numbers calculate the total calculate the average output the result Pseudocode of a program to calculate the average of 3 numbers
Hierarchy chart of a program to calculate the average of 3 numbers Calculate average of 3 numbers print out the result divide the total by 3 input the numbers calculate the total
Software Lifecycle 1. Problem definition 2. Algorithm design 3. Coding 4. Testing 5. Maintenance
Visual Basic Objects Visual Basic Events Numbers Strings Input and Output Built-In Functions
Visual Basic Objects Visual Basic Events Numbers Strings Input and Output Built-In Functions
Visual Basic Objects Visual Basic programs use a Windows style screen called a form. On the form are placed text boxes for the user to type in text, and command buttons for the user to press.
The most commonly used VB objects (aka controls): Text Box Command Button Label Picture Box
The Text Box A Text Box Walkthrough Page 43 of the textbook
The Command Button A Command Button Walkthrough Page 49 of the textbook
The Label A Label Walkthrough Page 50 of the textbook
The Picture Box A Picture Box Walkthrough Page 51 of the textbook
Visual Basic Objects Visual Basic Events Numbers Strings Input and Output Built-In Functions
Visual Basic Events When a VB program is running, a form and its controls appear on the screen. Nothing happens until the user interacts with the form or its controls in some way, such as clicking on a button. Such an interaction is known as an event.
What is an Event? Examples: • Click a button • Double click a button • Changing text in a text box anything that happens to an object (aka control) is an event
The 3 steps to creating a VB program: 1. Create the user interface (form, buttons). 2. Set the properties of the objects. 3. Write the code that executes when the events occur.
The 3 steps to creating a VB program: 1. Create the user interface (form, buttons). 2. Set the properties of the objects. 3. Write the code that executes when the events occur.
When you are in the program design mode of VB, and you perform some action on an object such as clicking on a command button on your form, a code window opens up with some code already supplied for you . . .
. . . the content of the supplied code will depend on which object you clicked on . . .
The statements to be executed when an event occurs are written in a block of code known as an event procedure. The structure of an event procedure is: Private Sub objectName_event() statements End Sub
The word Sub in the first line signals the beginning of the event procedure, and identifies the object and the event occurring to that object… Private Sub objectName_event()
The last line (End Sub) signals the end of the event procedure. The statements to be executed appear between these two lines...
Private Sub objectName_event() statements End Sub
The word Private means that this event procedure cannot be invoked (made to execute) by an event from another form. This will not concern us until much later in the course when we use multiple forms.
An example event procedure Private Sub cmdButton_Click() txtBox.Text = " " End Sub this event procedure clears the contents of the text box when the button is clicked . . .
Homework • Read pages 41 - 70 of the textbook