Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
5.05 Apply Looping Structures
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
IE 212: Computational Methods for Industrial Engineering
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
Chapter 4: The Selection Process in Visual Basic.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Chapter Six: Working With Arrays in Visual Basic.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare.
Chapter 6: The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
6.2 For…Next Loops General Form of a For…Next Loop
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Controlling Program Flow with Looping Structures
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter One: An Introduction to Programming and Visual Basic
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 7 Arrays.
حلقات التكرار.
Chapter 7: Using Functions, Subs, and Modules
Introduction to Computer Programming IT-104
Presentation transcript:

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 1 Chapter 6: Using Arrays

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 2 Using Control Arrays 1. A control array is group of the same type control which is assigned a single name. 2. The individual controls are identified by the control array name and an index value. 3. The Case decision structure can be useful in working with a control array. 4. To create a control array, just enter the same name for more than one control and reply “yes” to the query. 5. The order in which the controls are entered determines the index values for the controls in the control array.

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 3 Using List Arrays 1. Arrays are lists or tables of data which have a single name. Individual array elements are identified by the array name and one or more subscripts or index values. 2. To be used in Visual Basic, an array must be declared; arrays can be fixed size or dynamic size (we use only fixed arrays.) 3. The default lower limit on an array index is zero but this can be changed to one or any other value. 4. It is not possible to exceed the upper limit or go below the lower limit on the array index values.

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 4 Comparing Listboxes and Arrays curPrices(0) curPrices(1) curPrices(3) curPrices(2) curPrices(4) curPrices(6) curPrices(5) curPrices(7) curPrices(8) curPrices(9) Array curPrices

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 5 Entering Array Data 1. Arrays can be input with any of the three types of loops discussed earlier--event driven, for-next, or while/until loops. 2. Event-driven loops and For-Next loops are good for keyboard input; While or Until loops are good for input from a file. 3. In any case, each array element must be input using its subscript value. 4. With an event driven loop, each click of a button increments a counter and inputs the corresponding array element. 5. With a For-Next loop, you must input the number of array elements. The array values must be input with an Inputbox with the For-next counter variable matching the array index. 6. With an Until loop, you can input from a file. You must increment a counter that matches the index for the array element.

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 6 Code to Input from For-Next and Until Loops For-Next Loop Dim intCounter As Integer intNumPrices = CInt(InputBox("How many prices?")) For intCounter = 0 To intNumPrices - 1 curPrices(intCounter ) = CCur(InputBox("Next _ price:")) Next Until Loop Open "a:\prices.txt" For Input As #1 Do Until EOF(1) Input #1, curPrices(intNumPrices) intNumPrices = intNumPrices + 1 Loop

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 Processing Arrays 1. Typical array operations include summing and averaging the array values and finding the largest or smallest value in the array. 2. Working with arrays usually involves a For-Next loop. 3. Summing curPrices array elements involves using a statement of the form: curSum = curSum + curPrices(intCounter) 4. Averaging curPrices array elements involves dividing the curSum by the number of elements. 5. Finding the largest or smallest value involves multiple comparisons of array elements. 6. To find the maximum Price, you must compare each array value to the current highest price; if it is higher, it becomes the highest price. Do this using For-Next loop and If-then If curMax < curPrice(intCounter) then curMax = curPrice(intCounter) End if

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 8 Code to Find MaximumValue in List Private Sub cmdFindMax_Click() Dim intCounter As Integer Dim curLargest As Currency curLargest = curPrices(0) For intCounter = 1 To intNumPrices - 1 If curPrices(intCounter) > curLargest Then curLargest = curPrices(intCounter) End If Next txtMaxPrice.Text = Format(curLargest, _ "currency") End Sub

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 9 Finding Items and Working With Multiple Lists 1. It is possible to work with multiple list arrays by matching the index values for the arrays. 2. Finding a specific array value involves comparing each array element to the desired value. A flag is often used to indicate whether a match was found. 3. The MsgBox can be used as a function by including multiple parameters within parentheses. 4. Parameters can be internal constants. 5. The MsgBox function returns a value which can be checked to determine which button was clicked.

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 Pseudocode to Find Price for Part Identifier Begin procedure to find part identifier Input part identifier Set Flag to false Repeat for each part in parts list If part identifier = identifier on parts list then Flag = True Save index of part identifier on parts list End decision End repeat If Flag = true Use saved index to display part identifier and price Else Display message that part not on parts list End decision End procedure

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 Code to Find Price for a Part Identifier Private Sub cmdFind_Click() Dim intCounter As Integer, intResults As Integer Dim strFindID as String, blnFound As Boolean Dim intPriceIndex as Integer strFindID = InputBox("Input part identifier to find") blnFound = False For intCounter = 0 To intNumPrices – 1 If strFindID = strPartId(intCounter) Then blnFound = True intPriceIndex = intCounter Exit For End If Next If blnFound Then txtFound.Text = strPartId(intPriceIndex ) & " " & _ Format(curPrices(intPriceIndex ), "Currency") Else intResults = MsgBox("Part not found", VbExclamation, _ "Price Search") End If End Sub

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 12 Code to Save Index of Part with Highest Price Private Sub cmdFindMax_Click() Dim intCounter As Integer Dim curLargest As Currency Dim intMaxIndex as Integer curLargest = curPrices(0) For intCounter = 1 To intNumPrices - 1 If curPrices(intCounter) > curLargest Then curLargest = curPrices(intCounter) intMaxIndex = intCounter End If Next txtMaxPrice.Text = strPartId(intMaxIndex) & _ " " & Format(curPrices(intMaxIndex), _ "Currency") End Sub

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 13 Working with Multiple Forms 1. It is possible to have multiple forms in a project. To display a form, use Object.Show To hide the form, use Object.Hide 2. A new form can be added using the Project|New Form menu option or the New Form icon on the toolbar. 3. If a control on one form is referred to on another form, the form name must be included as a part of the control reference.

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 Searching in Strings * Searching for strings using a loop requires that you use UCase() or LCase() to convert all strings to the same case. * To search for a sub-string within a string, you use the function: (Instr(string to search, search string) For example, InStr(“Go Dogs”, “D “) returns 4 because “D” is in the 4 th position. If the character does not exist, InStr() returns a zero. *NOTE: InStr() is case sensitive!!!

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 Code to Search for a Partial Video Name Private Sub cmdSearch_Click() Dim strVideoName As String, intCounter As Integer Dim intNumMatches As Integer strVideoName = txtSearch.Text lstVideos.Clear lstVideos.AddItem "Video Name" For intCounter = 0 To intNumVideos If InStr(UCase(Videos(intCounter)), _ UCase(strVideoName)) > 0 Then intNumMatches = intNumMatches + 1 lstVideos.AddItem strVideos(intCounter) End If Next If intNumMatches = 0 Then MsgBox ("No matching videos found! Try again.") ElseIf intNumMatches <= 5 Then lstVideos.AddItem Str(intNumMatches) + " videos found" Else lstVideos.Clear MsgBox ("Too many matching videos!") End If End Sub

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 16 Code to Display Video Information Private Sub lstVideos_Click() Dim strVideoName As String Dim intCounter As Integer strVideoName = lstVideos.Text lstVideos.Clear For intCounter = 0 To intNumVideos If strVideoName = strVideos(intCounter) Then lstVideos.AddItem strVideoName & " " & _ Format(strVideoPrice(intCounter), _ "currency") & " " & strVideoLoc(intCounter) Exit For End If Next End Sub

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 17 Two-Dimensional Arrays When you are working with a table instead of a list, you have a 2-dimensional array. 2-D arrays use two subscripts with the first subscript referring to the row value and the second subscript referring to the column value. Nested For-next loops are often used to work with 2-D arrays with the inner loop matching the columns and the outer loop matching the rows.

Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 18 Using the Step Commands for Debugging The Step Commands Step into Stepout of Step Over If you select Step Into, you can “step” through the code, line by line and note the result of the code in the various windows--Watch, Locals, or Immediate.