1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

Working with Intrinsic Controls and ActiveX Controls
5.04 Apply Decision Making Structures
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Program Design and Development
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 5 new The Do…Loop Statement
Visual Basic: An Object Oriented Approach 5: Structured Programming.
Chapter 4 The If…Then Statement
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Visual Basic Games: Prepare for Hangman
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.
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.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
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.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
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 Programming I 56:150 Information System Design.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Pay Example (PFirst98) Please use speaker notes for additional information!
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Controlling Program Flow with Decision Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Introduction to Programming Lecture 2
5.04 Apply Decision Making Structures
A variable is a name for a value stored in memory.
UNIT 5 Lesson 15 Looping.
Chapter 4 The If…Then Statement
Loops BIS1523 – Lecture 10.
Visual Basic 6 (VB6) Data Types, And Operators
3rd prep. – 2nd Term MOE Book Questions.
Iteration, FileSystemObject and Multiple Forms
Department Array in Visual Basic
Chapter (3) - Looping Questions.
Items, Group Boxes, Check Boxes & Radio Buttons
Introduction to Programming
Fundamentals of visual basic
Statement-Level Control Structures
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
If, Subroutines and Functions
Visual Basic Programming
Selection Statements Chapter 3.
Unit 3: Variables in Java
ASP control structure BRANCHING STATEMENTS
Introduction to Programming
Introduction to Computer Programming IT-104
Presentation transcript:

1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons

2 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)

3 BackColor Selection

4

5 Option Explicit Private Sub optBlue_Click() frmSelectColor.BackColor = vbBlue End Sub Private Sub optGreen_Click() frmSelectColor.BackColor = vbGreen End Sub Private Sub optCyan_Click() frmSelectColor.BackColor = vbCyan End Sub

6 BackColor Selection (Extended) optRed’s value property was set to true during development but the backcolor is assigned only on a click event One could have another method (e.g. Form_Load) call optRed_Click whichj will give the same effect as if optRed had been clicked (Note each optionbutton has its own backcolor property, they remain default gray in this example)

7 BackColor Selection (Extended)

8 Multiple choice From a group of OptionButtons, one and only one can have a true value But what if there are a number of categories, and the user must choose one item from each category We have to let the program know which optionbuttons belong to a group This is done with frames

9 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

10 Putting OptionButtons in Frames Make the Frame first, then put the new OptionButton in it; if you move a pre-existing OptionButton into a Frame, it’s not really in the Frame If you use copy and paste to make additional OptionButtons, be sure that the Frame is highlighted before clicking paste (also at this stage we do not want a control array) To move a pre-existing OptionButton into a Frame, cut it and paste it into the Frame

11 Multiple Choice Example

12 Multiple Choice Example

13 Multiple Choice Example

14 Multiple Choice Example Option Explicit Private Sub opt12_Click() txtText.FontSize = 12 End Sub Private Sub opt24_Click() txtText.FontSize = 24 End Sub Private Sub opt8_Click() txtText.FontSize = 8 End Sub

15 Multiple Choice Example Private Sub optArial_Click() txtText.FontName = "Arial" End Sub Private Sub optCourier_Click() txtText.FontName = "Courier" End Sub Private Sub optTimes_Click() txtText.FontName = "Times New Roman" End Sub

16 Select-Case The Select-Case statement is a fancy version of the If-Then structure used when there are several branches of the algorithm that might be taken and which branch is determined by a single expression P. 140 in Deitel, Deitel and Nieto

17 Other uses of Frames Frames are good for grouping controls That might be moved together That might be “disabled” together That might be made “invisible” together Etc.

18 Right Justified Frame

19 Right Justified Frame

20 Right Justified Frame Const MARGIN As Integer = 200 Private Sub Form_Resize() fraFontSize.Left = frmFonts.ScaleWidth - _ fraFontSize.Width - MARGIN End Sub ‘don’t need to move optionbuttons just the ‘frame containing them Using Const instead of Dimmeans the number cannot be changed later on

21 State Tax Example

22 State Tax Example

23 State Tax Example

24 State Tax Example Private Sub cmdCalculate_Click() Select Case UCase(txtState.Text) Case Is = "PA" txtTotal.Text = txtSubTotal.Text * 1.06 Case Is = "CA" txtTotal.Text = txtSubTotal.Text * Case Is = "MA" txtTotal.Text = txtSubTotal.Text * Case Else txtTotal.Text = txtSubTotal.Text End Select End Sub Capitalizes a string

25 Select Case Flexibility In Visual Basic the Select-Case statement allows for cases that are Inequalities (e.g. Case Is < 4) Ranges (e.g. Case 53 To 64) Comma separated values (e.g. Case Is 75, 99, 302) The corresponding statement in other languages is not as flexible

26 Arithmetic Homework Revisited

27 Arithmetic Homework Revisited Option Explicit Dim Number1 As Integer Dim Number2 As Integer ' Private Sub optAdd_Click() cmdSymbol.Caption = "+" txtResult.Text = "" End Sub

28 Arithmetic Homework Revisited Private Sub optMultiplication_Click() cmdSymbol.Caption = "*" txtResult.Text = "" End Sub ' Private Sub optSubtract_Click() cmdSymbol.Caption = "-" txtResult.Text = "" End Sub

29 Arithmetic Homework Revisited Private Sub txtNumber1_Validate(Cancel As Boolean) If IsNumeric(txtNumber1.Text) Then Number1 = CInt(txtNumber1.Text) Else MsgBox ("Please enter a number.") txtNumber1.Text = "" Cancel = True End If End Sub

30 Arithmetic Homework Revisited Private Sub txtNumber2_Validate(Cancel As Boolean) If IsNumeric(txtNumber2.Text) Then Number2 = CInt(txtNumber2.Text) Else MsgBox ("Please enter a number.") txtNumber2.Text = "" Cancel = True End If End Sub

31 Private Sub cmdSymbol_Click() Select Case cmdSymbol.Caption Case "+" txtResult.Text = Number1 + Number2 Case "-" txtResult.Text = Number1 - Number2 Case "*" txtResult.Text = Number1 * Number2 Case Else MsgBox ("Something is wrong.") End Select End Sub

32 Arithmetic Homework Revisited Private Sub cmdClear_Click() txtNumber1.Text = "" txtNumber2.Text = "" txtResult.Text = "" txtNumber1.SetFocus End Sub

33 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 P. 132 in Deitel, Deitel and Nieto

34 Accumulating Interest

35 Accumulating Interest Private Sub cmdCalculate_Click() Dim Year As Integer Dim Balance As Currency Dim Rate As Double Balance = CDbl(txtInitialAmount.Text) Rate = CDbl(txtRate.Text) / 100

36 Accumulating Interest txtBalance.Text = "" For Year = 1 To CInt(txtLife.Text) Balance = Balance * (1 + Rate) txtBalance.Text = txtBalance.Text & Year & _ vbTab & "$" & Balance & vbCrLf Next Year End Sub

37 Step To change the counter by anything other than 1, use the keyword Step to indicate the amount by which the counter should be incremented For example, For I=10 to 0 Step –1 begins a loop that counts backwards

38 Quarterly Interest

39 Quarterly Interest Private Sub cmdCalculate_Click() Dim Year As Single Dim Balance As Currency Dim Rate As Double Balance = CDbl(txtInitialAmount.Text) Rate = CDbl(txtRate.Text) / 100 / 4

40 Quarterly Interest txtBalance.Text = "" For Year = 0.25 To CInt(txtLife.Text) Step 0.25 Balance = Balance * (1 + Rate) txtBalance.Text = txtBalance.Text & Year & _ vbTab & "$" & Balance & vbCrLf Next Year End Sub

41 Style Notes Indent all statements within a method Indent all statements within an If-Then- Else structure Indent all statements within a Select- Case structure Indent all statements within a loop structure

42 Style Notes Put at least a line of space between two methods Put a comment line ‘ above a method Put a comment describing the method above the method Names of Const variables are put in all capital letters