More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

Microsoft® Small Basic
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
VBA Modules, Functions, Variables, and Constants
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Multiple Forms and Standard Modules
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
CHAPTER THREE Representing Data: Constants and Variables.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Chapter 12: How Long Can This Go On?
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
CPS120: Introduction to Computer Science
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
Chapter 6: The Repetition Structure
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 5: More on the Selection Structure
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
I Power Higher Computing Software Development High Level Language Constructs.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Problem Solving and Control Statements.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Computer Science Up Down Controls, Decisions and Random Numbers.
Visual Basic Fundamental Concepts
Introduction to Programming Lecture 2
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
JavaScript: Control Statements.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
VISUAL BASIC.
Visual Basic..
Chapter 8 JavaScript: Control Statements, Part 2
T. Jumana Abu Shmais – AOU - Riyadh
Items, Group Boxes, Check Boxes & Radio Buttons
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
If, Subroutines and Functions
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,

Data Type At the lowest level, all data stored in the computers is written in 1’s and 0’s (binary) How that data gets interpreted, what actions can be performed on it, etc. depends in part on the “data type” associated with it We let the computer know how we mean to interpret a variable by declaring its type, e.g. Dim Count As Integer ‘ (Option Explicit makes VB strongly typed)

Some VB types integer : a whole number, that is a number with no fractional part. single: A number with a decimal point, such as (called a “float” in C) double: A number with a decimal point, such as (called a “float” in C); is more precise than a single; it also allows for a greater range of numbers

More VB Types String: words or text, that is, groups of letters, numbers and symbols Boolean: a variable that can only be true or false (real type as opposed to C’s) Currency: data type used to represent money (dollars and cents); only two decimal places are shown; takes care of unusual rounding associated with money (pp in VB & VBA)

The Control type When you drag a control onto a form, you don’t need to declare it Reserving memory locations and so on is done automatically However, if you need to remember a control (e.g. to keep track of what button you pressed), you declare a control

Command Type Demo

The Control Type (cont.) Dim ControlTrack As Control Private Sub cmdButton1_Click() Set ControlTrack = cmdButton1 End Sub Private Sub cmdButton2_Click() Set ControlTrack = cmdButton2 End Sub Private Sub cmdButton3_Click() Text1.Text = "you last pressed " + ControlTrack.Caption End Sub

Binding You will notice a difference between typing “cmdButton1.” and typing “ControlTrack.” In the first case, VB knows that it is a CommandButton control and so provides the properties in a drop-down list (an example of early binding) In the second case, VB does not know the type of control that ControlTrack is until the program runs, hence, it cannot provide the properties (an example of late binding)

If-Then Tests a condition and executes statements within the body (block) of the if structure provided the condition was true and skips over those steps otherwise (pp VB & VBA)

If-Then Example If State=“PA” Then Price=Price*(1.06) End If ‘State ‘Recalculates the price factoring in the PA ‘sales tax if the state is PA ‘Note strings are more easily compared in VB

Condition A condition is an expression that evaluates to a Boolean value (true or false) It often involves a comparison A > B (is A greater than B?) A >= B (is A greater than or equal to B?) A < B (is A less than B?) A <= B (is A less than or equal to B?) A=B (is A equal to B?) A <> B (is A not equal to B?) ‘(does not use !)

Compound Conditions Boolean operators can be used to combine conditions And: both expressions must evaluate to true for combination to be true Or: if either expression evaluates to true, the combination is true Not: if an expression evaluates to true, Not(expression) evaluates to false and vice versa

If-Then-Else The If-Then-Else structure provides two statement blocks, one to execute if the condition is true, the other to execute if the condition is false

If-Then-Else Example If State=“PA” Then If City = “Phila” Then Price=Price*(1.07) Else Price=Price*(1.06) End If ‘City End If ‘State ‘This is also an example of nested if’s (an if within an if)

ElseIf If Grade > 94 Then Letter_Grade = “A” ElseIf Grade > 86 Then Letter_Grade = “B” ElseIf Grade > 78 Then Letter_Grade = “C” ElseIf Grade > 70 Then Letter_Grade=“D” Else Letter_Grade=“F” EndIf

For-Next Loop One of the structures used for repeating steps A condition is tested, specifically whether a counter falls within some limit If the condition is true, the statements in the for- next block are executed, at the “bottom” the counter is incremented, and one returns to the “top” of the loop to test the condition again If the condition is false, the for-next statements are not executed and the next statement executed is the one following Next

For-Next Example (pp in VB & VBA) For i=1 To Num_Years Principle=Principle*(1+ Interest/100) Next I ‘Calculates Interest (compounded yearly) on Principle ‘over Num_Years

Multiple Forms A project can consist of a few forms Typically one form (the start-up form) appears first Events associated with it bring up the other forms One can have multiple forms showing, or one can hide all but the one the user is interfacing with

Message Box A special case of multiple form is a message box, a little window with a warning popping up Message Box function (pp in VB & VBA) Dim r As Integer Private Sub Command1_Click() r = MsgBox("Body Message", vbOKOnly, "Caption Message") End Sub

Form Events To control various forms we want to understand their methods and events Initialize: occurs when form is first referenced, occurs once unless form is “terminated” Load: occurs when the specific properties of the form are put into memory, occurs once unless form is “unloaded”

More Form Events Resize: occurs when the form is moved or its size is altered There’s a Resize called by load Activate: occurs when a user switches forms The events occur in the order given above Initialize  Load  Resize  Activate

Form methods Show: displays (makes visible) a form This will include the loading a form if the form was not loaded previously Hide: makes a form invisible (pp in Visual Basic Controls)

Adding another form Go to Project/Add Form Choose whether you want to make a New form or include an already Existing form An existing form is not copied and brought into the project folder, you must choose to do that (right click on the form in the upper right hand side of the IDE and choose Save frmWhatever As …

Scope Procedure-level: variable is available only with subroutine or function in which it is declared (local) Module-level: variable is available to all subroutines within a module, e.g. all the subroutines associated with a form Friend: variable is available to other forms within a project, but is not available to anyone outside the project Public: variable is available to other projects

Multi-Form Demo

Form 1 Code Public FirstName As String Private Sub cmdShowForm2_Click() FirstName = txtForm1.Text frmForm2.Show frmForm1.Hide End Sub

Form 2 Code Private Sub cmdReturn_Click() frmForm2.Hide frmForm1.Show End Sub Private Sub Form_Activate() txtForm2.Text = frmForm1.FirstName End Sub

ListBox

Some ListBox Properties and Methods MultiSelect: allows more than one item to be selected 1-Simple SelCount: gives the number of highlighted items Text: the text of the currently highlighted item AddItem: adds a string to the list (pp in Visual Basic Controls)

Some ListBox Code Private Sub Form_Load() lstCourse.AddItem ("csc 362") End Sub Private Sub lstCourse_Click() If lstCourse.SelCount = 1 Then txtCourse1.Text = lstCourse.Text End If If lstCourse.SelCount = 2 Then txtCourse2.Text = lstCourse.Text End If End Sub

Radio Buttons

OptionButtons A.k.a. Radio Buttons Used if one and only one choice must be made The one and only one logic is built into the radio button controls The default method on an OptionButton is associated with the click event; it tells one that that particular choice was most recently selected Important property: value: Boolean determines if that buttons was the one selected (true) (pp Visual Basic Controls)

Frames in action

Frame Recall a form provides a surface or container for controls A frame is like a form within a form (a container within a container) It groups controls together, so that can be treated as a separate set Often used if there is more than one set of radio buttons

Some Frame Code (pp Visual Basic Controls) Private Sub cmdCommand1_Click() fraFrame1.Enabled = False End Sub ‘Disables everything contained in Frame1