Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
Lecture Roger Sutton CO331 Visual Programming 12: One-dimensional Arrays 1.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
An array of controls Not particularly convenient in VB Examples: array of pictureboxes Array of textboxes Notes on Concentration game (a possible final.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
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.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
 Pearson Education, Inc. All rights reserved Arrays.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
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.
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Chapter Six: Working With Arrays in Visual Basic.
The PictureBox Control Prefix Prefix – pic Image Property PictureBox Image Property – Changes the image or file that appears inside of the PictureBox SizeMode.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
Tutorial 6 The Repetition Structure
30/10/ Iteration Loops Do While (condition is true) … Loop.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Visual Basic Programming
Applications Development
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Two Forms Please use speaker notes for additional information!
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Slide 1 VB Graphics Controls & Timer Control. Slide 2 Default Controls.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
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.
Controlling Program Flow with Decision Structures.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
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,
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Chapter 7 Arrays.
Chapter 6 Sub Procedures
Lbound and Ubound Functions
ARRAYS Variable Arrays, Control Arrays, Dynamic Variable Arrays, Dynamic Control arrays.
Introduction to Computer Programming IT-104
Presentation transcript:

Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer

Seeding Random Number Generators Because the numbers are given by an algorithm, the same numbers come up each time the algorithm runs A “seed” is a parameter in the random number generator; changing the seed, changes algorithm results Randomize seeds the generator off the clock so the program is different at different times Randomize should only be done once, perhaps in a form_initialize method

Variable Arrays If a group of variables are of the same type (e.g. all int’s) (Note: same type does not mean same value) and play a similar role (i.e. the code for them is nearly identical), it is convenient to make them an array For example, grade1, grade2, … becomes grade(0), grade(1), …. Chapter 7 in Deitel, Deitel and Nieto

Arrays in Memory Recall variables correspond to memory locations The elements of an array are stored in consecutive memory locations When you refer to the entire array, you “point” to the first memory location

Before Arrays Dim Grade1 As Integer Dim Grade2 As Integer Dim Grade3 As Integer Dim Grade4 As Integer Dim Grade5 As Integer Dim Grade6 As Integer BECOMES Dim Grade(5) As Integer

After Arrays Average = (Grade1 + Grade2 + Grade3 + _ Grade4 + Grade5 + Grade6) / 6 BECOMES Average = 0 For i = 0 To 5 Average = Average + Grade(i) Next i Average = Average / 6

Array Vocabulary Array: refers to the entire set of related variables Element: refers to one member of the array Index: (a.k.a. subscript) is an integer (0,1,2,…) that is associated with each element Grade is the array, Grade(3) is an element in that array, that element has an index 3 (note: it is the fourth item)

More Array Vocabulary Option Base: If you declare Grade(5), VB makes an array with six elements Grade(0) through Grade(5), that is, it starts indexing at 0 If you want to start indexing at 1, you can type Option Base 1 (just below Option Explicit) Note: Control Arrays are always zero based (regardless of Option Base)

Average, High and Low

Option Explicit Const SCORENUMBER As Integer = 5 Private Sub cmdEnter_Click() Dim Score(SCORENUMBER - 1) As Integer Dim i As Integer Dim Sum As Integer Dim Min As Integer Dim Max As Integer Dim Average As Double

Average, High and Low Score(0) = InputBox("Enter Score 1: ") Sum = Score(0) Min = Score(0) Max = Score(0) ‘Enter first score outside of loop and make it the ‘min and the max

Average, High and Low For i = 1 To SCORENUMBER - 1 Score(i) = InputBox("Enter Score " & i + 1 & ": ") Sum = Sum + Score(i) If Score(i) < Min Then Min = Score(i) End If If Score(i) > Max Then Max = Score(i) End If Next i

Average, High and Low Average = Sum / SCORENUMBER txtAverage.Text = Average txtHigh.Text = Max txtLow.Text = Min End Sub Private Sub Form_Load() lblInstruct.Caption = "Click the button below to begin " & _ "entering the " & SCORENUMBER & " scores and “ & _ “calculate " & “their average, high and low." End Sub

Scaling To scale something is to change its size (e.g. the number of elements in an array) If a program is written in a way which facilitates various size changes, it is said to be “scalable” The use of the constant SCORENUMBER in the previous code made it scalable, since the code would only one change if the size of the score array was varied

Array as argument of function 1 Const NUMBEROFGRADES As Integer = 5 Private Sub cmdEnter_Click() Dim Grades(NUMBEROFGRADES-1) As Integer Dim Ave As Double Dim i As Integer For i = 0 To NUMBEROFGRADES-1 Grades(i) = InputBox("Enter Grade " & i, "Grades") Next i Ave = Average(Grades) txtAverage.Text = Ave End Sub

Array as argument of function 2 Private Function Average(mArray() As Integer) As Double Dim i As Integer Average = 0.0 For i = LBound(mArray) To UBound(mArray) Average = Average + mArray(i) Next i Average = Average / (UBound(mArray) - LBound(mArray) + 1) End Function

Bounds The Lower Bound is the lowest index in an array The Upper Bound is the highest index in an array Lbound(ArrayName) finds the lower bound Ubound(ArrayName) finds the upper bound of an array

Shuffle 'randomly shuffles an array of strings Private Sub Shuffle(mArray() As String) ‘Strings only Dim i As Integer Dim Swap As String Dim j As Integer dim lb as Integer lb = LBound(mArray) dim ub as Integer ub = UBound(mArray) For i = lb To ub j = lb + Int((ub - lb + 1) * Rnd()) Swap = mArray(i) mArray(i) = mArray(j) mArray(j) = Swap Next i End Sub

By Reference In VB the default situation is that variables are passed to a function “by reference” (whether or not they are arrays) That is, if the variable is changed in the function, it is also changed in the caller More about this in a future lecture

Control array If a group of controls are of the same type (e.g. all CommandButtons) and play a similar role (i.e. the code for them is nearly identical), it is convenient to make them an array (p. 274 in Deitel, Deitel and Nieto)

Array of TextBoxes

Code for Array of Textboxes Option Explicit Private Sub cmdSumHours_Click() Dim i As Integer 'Counter Dim Total As Integer Total = 0 For i = 0 To txtHours.Ubound ‘ Ubound is a property NOT function Total = Total + txtHours(i).Text Next i txtSum.Text = Total End Sub

Which button was pressed?

Which button revisited (code) Option Explicit Dim StoreButtonIndex As Integer Private Sub cmdButton_Click(Index As Integer) StoreButtonIndex = Index End Sub Private Sub cmdWhich_Click() txtWhich.Text = cmdButton(StoreButtonIndex).Caption & _ " was last pressed." End Sub Comes automatically and tells us which button was pressed

Collection of Forms The index property comes into play when a control is an element in an array Notice that forms do not have an index property However there is a way to apply these scaling ideas to forms Forms have an order, the order in which they are loaded The third form loaded can be referred to as Forms(2) (zero-based counting) Doesn’t matter what it was named

Toward Scaling with Forms

Option Explicit Dim FormIndex As Integer Private Sub optForm_Click(Index As Integer) FormIndex = Index + 1 End Sub Private Sub cmdGoToForm_Click() Me.Hide Forms(FormIndex).Show End Sub Private Sub Form_Load() 'the order in which forms are loaded 'is their order in the collection Call Load(frmNumber1) Call Load(frmNumber2) Load frmNumber3 ‘note alternate syntax for calling subs End Sub

PictureBox The pictureBox control (not Image control) is found near the top of the control in the VB IDE The properties we will use Picture: refers to the picture to be shown We’ll load the pictures during runtime instead of during development Index: since we will make an array of PictureBoxes Tag: an internal labeling, we can give matching PictureBoxes the same Tag and use that property to test for a match

PictureBox (Cont.) We will load the picture using the function LoadPicture(filename) which loads an image file include “the complete path” e.g. LoadPicture(“a:\dice2.gif”) If the image files are in the project folder, one can use App.Path e.g. LoadPicture(App.Path & “\dice2.gif”) LoadPicture() empties the PictureBox Note: no argument in LoadPicture()

Craps Example

Craps code Option Explicit Private Sub cmdRoll_Click() Dim i As Integer Dim Die_Value As Integer For i = 0 To picDie.UBound Die_Value = Int(Rnd() * 6 + 1) picDie(i).Picture = LoadPicture(App.Path & "\dice" & Die_Value & ".gif") Next i End Sub Private Sub cmdClear_Click() Dim i As Integer For i = 0 To picDie.UBound picDie(i).Picture = LoadPicture() Next i End Sub

Timer Important Properties Interval: the amount of time in milliseconds between Timer events Enabled: When True, Timer events occur, when False, no Timer events occur Events/Methods Timer: when the timer is enabled a Timer event occurs roughly every N milliseconds, where N is the Interval property

Stop Watch

Stop Watch Code Option Explicit Private Sub cmdStart_Click() tmrStopWatch.Enabled = Not tmrStopWatch.Enabled If tmrStopWatch.Enabled Then cmdStart.Caption = "Stop" Else cmdStart.Caption = "Start" End If End Sub Private Sub tmrStopWatch_Timer() txtTimer.Text = txtTimer.Text ‘interval = 100 End Sub