Dynamic Arrays List Boxes Dynamic Arrays Dynamic Control
Dynamic Arrays List. Boxes, Dynamic Arrays, Dynamic Control Arrays List. Boxes are on pp. 423 -427 and dynamic arrays are in Chapter 7 of Deitel, Deitel and Nieto 1
Font Selection List. Box 2
Font Selection List. Box 3
Font Selection List. Box 4
Font Selection List. Box 5
Font Selection List. Box Option Explicit Has a click event Text of the currently highlighted item Private Sub lst. Font_Click() txt. Message. Font. Name = lst. Font. Text End Sub Method to add an Private Sub cmd. Add. Font_Click() item to the list lst. Font. Add. Item (txt. New. Font. Text) txt. New. Font. Text = "" 6 End Sub
Font Selection List. Box Private Sub Form_Load() Dim i As Integer Const PHRASE As String = "All work and no play “ & _ “makes Jack a dull boy. " & vb. Cr. Lf For i = 1 To 20 txt. Message. Text = txt. Message. Text + PHRASE Next i End Sub 7
Choose a Team List. Box Set listbox’s Multi. Select property to 1 in design time 8
Choose a Team List. Box 9
Choose a Team List. Box 10
Choose a Team List. Box Option Explicit Private Sub cmd. Add. Name_Click() lst. Names. Add. Item (txt. Name. Text) txt. Name. Text = "" End Sub 11
Choose a Team List. Box Private Sub cmd. Choose. Team_Click() The number of items Dim i As Integer currently in a listbox An- array For i = 0 To lst. Names. List. Count 1 of booleans, true if the item is selected, If lst. Names. Selected(i) Thenfalse otherwise lbl. Team. Caption = lbl. Team. Caption & lst. Names. List(i) & " " An array containing the text for each item End If Next i 12 cmd. Choose. Team. Enabled = False
Choose a Team List. Box Private Sub Form_Load() lst. Names. Add. Item ("Lucy") lst. Names. Add. Item ("Ricky") lst. Names. Add. Item ("Fred") lst. Names. Add. Item ("Ethel") End Sub 13
Properties of a List. Box The List() property of the List. Box 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 Multi. Select property is 1 or 2) If Multi. Select is set to 0 (None) it is easier to use the Text (for the currently selected text) and List. Index(for the currently selected index)14 properties
Properties of a List. Box The Item. Data() 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 Add. Item method allows one to add items to the list; this means that the associated arrays List(), Selected() and Item. Data() are variable in size — they 15
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 Re. Dim to establish the size of the array 16
Unknown number of grades 17
Unknown Number of Grades (Code) Option Explicit Dim Grades() As Integer Dim Number. Grades As Integer Don’t know size when declaring Private Sub cmd. Enter_Click() Number. Grades = txt. Grade. Number. Text - 1 Re. Dim Grades(Number. Grades) Now we know Dim Ave As Double Dim i As Integer For i = LBound(Grades) To UBound(Grades) Grades(i) = Input. Box("Enter Grade " & i, "Grades") Next i Ave = Average(Grades) ‘Average given last week txt. Average. Text = Ave End Sub the size 18
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 19
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 Picture. Box 20
Coordinate System Top Left 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 21
Moving Button 22
Moving Button 23
Moving Button (Code) Option Explicit Private Sub cmd. Move_Click() cmd. Move. Top = cmd. Move. Top + 1000 * (1 - 2 * Rnd()) cmd. Move. Left = cmd. Move. Left + 1000 * (1 - 2 * Rnd()) If cmd. Move. Left < 0 Then cmd. Move. Left = 0 End If End Sub 24
Dynamic Control Array 25
Dynamic Control Array Code Part 1 Option Explicit Don’t know how many grades Dim Grades() As Integer Dim Number. Grades As Integer Const SPACING As Integer = 100 Private Sub cmd. OK_Click() Number. Grades = txt. Grade. Number. Text Re. Dim Grades(Number. Grades 1) Now we-know Dim Height As Integer Dim i As Integer 26
Dynamic Control Array Code Part 2 Height = lbl. Student(0). Height + SPACING For i = 1 To Number. Grades - 1 Introduces a new lable and Load lbl. Student(i) textbox onto the form Load txt. Grade(i) lbl. Student(i). Top = lbl. Student(i - 1). Top + Height txt. Grade(i). Top = txt. Grade(i - 1). Top + Height lbl. Student(i). Visible = True Assigns Top of new txt. Grade(i). Visible = True control so that it is lbl. Student(i). Caption = "Student" & i + 1 under previous control Next i txt. Grade(0). Set. Focus End Sub New controls are made invisible by default, you must make them visible if you want to see them 27
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 28 superimposed
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 29
Unloading 30
Unloading (Code) Private Sub cmd. Clear_Click() Dim i As Integer Backwards! For i = Number. Grades - 1 To 1 Step -1 Unload lbl. Student(i) Getting rid of a control Unload txt. Grade(i) txt. Grade. Number. Text = "" txt. Grade. Number. Set. Focus txt. Grade(0). Text = "" Next i End Sub 31
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 32
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 Number. Of. Players – If (Number mod 2 = 0) then “even” – Else “odd” 33
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 34
Three Rows 0 1 2 3 0 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 35
Controls in Rows and Columns A control’s Left property is determined by what column it is in, which is related to – Index Row. Number or – Index Mod Column. Number A control’s Top property is determined by what row it is in, which is related to – Index Mod Row. Number – Index Column. Number or 36
Marilyn and Andy 37
Marilyn and Andy 38
Marilyn and Andy Private Sub cmd. Make. Panel_Click() Dim Row As Integer, Col As Integer Dim i As Integer Dim Height As Integer, Width As Integer Row = txt. Row. Text Col = txt. Col. Text Height = pic. Marilyn(0). Height Width = pic. Marilyn(0). Width 39
Marilyn and Andy For i = 1 To Row * Col - 1 Load pic. Marilyn(i). Top = pic. Marilyn(0). Top + (i Mod Row) * Height pic. Marilyn(i). Left = pic. Marilyn(0). Left + (i Row) * Width pic. Marilyn(i). Visible = True Next i End Sub 40
- Slides: 40