1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp. 423-427 and dynamic arrays are in Chapter 7 of Deitel, Deitel and.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
VBA Modules, Functions, Variables, and Constants
Arrays in Visual Basic Week 9 CM What is an array ? An array is a data structure that enables us to store a list of values that can be thought.
VB Default Controls List Box, Combo Box
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
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.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
VB .NET Programming Fundamentals
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.
Controls General Discussion. VB Controls Visual Basic Controls A control is the generic name for any object placed on a form Controls may be images,
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
IE 212: Computational Methods for Industrial Engineering
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic Games: Prepare for Hangman
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
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.
ListBox, ComboBox, Menu Chapter 5.4, ComboBox Control: Properties & Methods u Combines TextBox features with a short drop- down list  cboOne.AddItem(string)
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
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.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
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.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Visual Basic Strings: Left$, Mid, Replace Files: Reading and Writing.
© 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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Two Forms Please use speaker notes for additional information!
© 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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
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 Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Visual Basic Variables
Visual Basic 6 (VB6) Data Types, And Operators
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
Visual Basic..
المحاضرة الأولى Lab(1) أ.ساره الأحمدي برمجة حاسب 2.
Visual Basic: Week 5 Review User defined functions
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
To understand what arrays are and how to use them
ARRAYS Variable Arrays, Control Arrays, Dynamic Variable Arrays, Dynamic Control arrays.
Introduction to Computer Programming IT-104
Presentation transcript:

1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and Nieto

2 Font Selection ListBox

3

4

5

6 Option Explicit Private Sub lstFont_Click() txtMessage.FontName = lstFont.Text End Sub Private Sub cmdAddFont_Click() lstFont.AddItem (txtNewFont.Text) txtNewFont.Text = "" End Sub Has a click event Text of the currently highlighted item Method to add an item to the list

7 Font Selection ListBox Private Sub Form_Load() Dim i As Integer Const PHRASE As String = "All work and no play “ & _ “makes Jack a dull boy." & vbCrLf For i = 1 To 20 txtMessage.Text = txtMessage.Text + PHRASE Next i End Sub

8 Choose a Team ListBox Set listbox’s MultiSelect property to 1 in design time

9 Choose a Team ListBox

10 Choose a Team ListBox

11 Choose a Team ListBox Option Explicit Private Sub cmdAddName_Click() lstNames.AddItem (txtName.Text) txtName.Text = "" End Sub

12 Choose a Team ListBox Private Sub cmdChooseTeam_Click() Dim i As Integer For i = 0 To lstNames.ListCount - 1 If lstNames.Selected(i) Then lblTeam.Caption = lblTeam.Caption & lstNames.List(i) & " " End If Next i cmdChooseTeam.Enabled = False End Sub The number of items currently in a listbox An array of booleans, true if the item is selected, false otherwise An array containing the text for each item

13 Choose a Team ListBox Private Sub Form_Load() lstNames.AddItem ("Lucy") lstNames.AddItem ("Ricky") lstNames.AddItem ("Fred") lstNames.AddItem ("Ethel") End Sub

14 Properties of a ListBox The List() property of the ListBox is an array of strings corresponding to what is written for each item in the list The Selected() property is an array of booleans to indicate which items are selected (useful when the MultiSelect property is 1 or 2) If MultiSelect is set to 0 (None) it is easier to use the Text (for the currently selected text) and ListIndex(for the currently selected index) properties

15 Properties of a ListBox The ItemData() property is an array of Longs (integers) which is not displayed, it allows the programmer to associate an additional piece of data to each item in the list The AddItem method allows one to add items to the list; this means that the associated arrays List(), Selected() and ItemData() are variable in size — they are dynamic arrays

16 Dynamic Array When the size (the number of elements) of an array is not known until runtime, a dynamic array is required When declaring the array (Dim), it has empty parentheses When the array size is known, use the keyword ReDim to establish the size of the array

17 Unknown number of grades

18 Option Explicit Dim Grades() As Integer Dim NumberGrades As Integer Private Sub cmdEnter_Click() NumberGrades = txtGradeNumber.Text - 1 ReDim Grades(NumberGrades) Dim Ave As Double Dim i As Integer For i = LBound(Grades) To UBound(Grades) Grades(i) = InputBox("Enter Grade " & i, "Grades") Next i Ave = Average(Grades) ‘Average given last week txtAverage.Text = Ave End Sub Unknown Number of Grades (Code) Don’t know size when declaring Now we know the size

19 Dynamic Control Arrays If there are unknown number of controls at design time, one needs a dynamic control array One drags and drops the first one onto the form, gives it an index of 0, and then adds others when the desired number of controls is known The problem with the dynamic control arrays is that they must be placed on the form, but where?

20 Top and Left The Top and Left properties determine the position of a control within its container, which may be a Form, a Frame or a PictureBox

21 Coordinate System Top is typically measured from the top of the container down to top of the control Left is typically measured from the left of the container over to the left of the control Top Left

22 Moving Button

23 Moving Button

24 Moving Button (Code) Option Explicit Private Sub cmdMove_Click() cmdMove.Top = cmdMove.Top * (1 - 2 * Rnd()) cmdMove.Left = cmdMove.Left * (1 - 2 * Rnd()) If cmdMove.Left < 0 Then cmdMove.Left = 0 End If End Sub

25 Dynamic Control Array

26 Dynamic Control Array Code Part 1 Option Explicit Dim Grades() As Integer Dim NumberGrades As Integer Const SPACING As Integer = 100 Private Sub cmdOK_Click() NumberGrades = txtGradeNumber.Text ReDim Grades(NumberGrades - 1) Dim Height As Integer Dim i As Integer Don’t know how many grades Now we know

27 Dynamic Control Array Code Part 2 Height = lblStudent(0).Height + SPACING For i = 1 To NumberGrades - 1 Load lblStudent(i) Load txtGrade(i) lblStudent(i).Top = lblStudent(i - 1).Top + Height txtGrade(i).Top = txtGrade(i - 1).Top + Height lblStudent(i).Visible = True txtGrade(i).Visible = True lblStudent(i).Caption = "Student" & i + 1 Next i txtGrade(0).SetFocus End Sub Introduces a new lable and textbox onto the form Assigns Top of new control so that it is under previous control New controls are made invisible by default, you must make them visible if you want to see them

28 Dynamic Control Arrays The first control in the array is placed on the form and given an index of 0 When an element of a control array is loaded, it is given the Top and Left properties of the original (same Height and Width too) A more reasonable Top and Left must be provided, else the controls are superimposed

29 Dynamic Control Arrays The newly loaded control has its Visible property set to False, so they must be made visible If the program allows the size of the control array to be altered, then one might have to unload some of the controls –Unload them in reverse order

30 Unloading

31 Unloading (Code) Private Sub cmdClear_Click() Dim i As Integer For i = NumberGrades - 1 To 1 Step -1 Unload lblStudent(i) Unload txtGrade(i) txtGradeNumber.Text = "" txtGradeNumber.SetFocus txtGrade(0).Text = "" Next i End Sub Backwards! Getting rid of a control

32 Toward Rows and Columns: Integer Division There are two ways to divide integers in VB –The first way results in a float (real number) 15 / 4  3.75 –The second way results in an integer (the whole number part of the above result) 15 \ 4  3 Not VB uses a backward slash to indicate integer division

33 Toward Rows and Columns: Mod Another outcome of integer division is the modulus (or remainder) –15 Mod 4  3 –Useful if you have players taking turns –Player = (Player + 1) Mod NumberOfPlayers –If (Number mod 2 = 0) then “even” –Else “odd”

34 Rows and Columns Control arrays are one dimensional arrays, i.e. there is only one index Integer division and modulus can help one make a one-dimensional array look like a two- dimensional array –Internally multi-dimensional arrays are really single dim

35 Three Rows Index: 0 R: 0 Mod 3 C: 0 \ 3 Index: 3 R: 3 Mod 3 C: 3 \ 3 Index: 6 R: 6 Mod 3 C: 6 \ 3 Index: 9 R: 9 Mod 3 C: 9 \ 3 1 Index: 1 R: 1 Mod 3 C: 1 \ 3 Index: 4 R: 4 Mod 3 C: 4 \ 3 Index: 7 R: 7 Mod 3 C: 7 \ 3 Index: 10 R: 10 Mod 3 C: 10 \ 3 2 Index: 2 R: 2 Mod 3 C: 2 \ 3 Index: 5 R: 5 Mod 3 C: 5 \ 3 Index: 8 R: 8 Mod 3 C: 8 \ 3 Index: 11 R: 11 Mod 3 C: 11 \ 3

36 Controls in Rows and Columns A control’s Left property is determined by what column it is in, which is related to –Index \ RowNumber or – Index Mod ColumnNumber A control’s Top property is determined by what row it is in, which is related to –Index Mod RowNumber or –Index \ ColumnNumber

37 Marilyn and Andy

38 Marilyn and Andy

39 Marilyn and Andy Private Sub cmdMakePanel_Click() Dim Row As Integer, Col As Integer Dim i As Integer Dim Height As Integer, Width As Integer Row = txtRow.Text Col = txtCol.Text Height = picMarilyn(0).Height Width = picMarilyn(0).Width

40 Marilyn and Andy For i = 1 To Row * Col - 1 Load picMarilyn(i) picMarilyn(i).Top = picMarilyn(0).Top + (i Mod Row) * Height picMarilyn(i).Left = picMarilyn(0).Left + (i \ Row) * Width picMarilyn(i).Visible = True Next i End Sub