Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Nieto

2 2 Font Selection ListBox

3 3

4 4

5 5

6 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 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 8 Choose a Team ListBox Set listbox’s MultiSelect property to 1 in design time

9 9 Choose a Team ListBox

10 10 Choose a Team ListBox

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

12 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 13 Choose a Team ListBox Private Sub Form_Load() lstNames.AddItem ("Lucy") lstNames.AddItem ("Ricky") lstNames.AddItem ("Fred") lstNames.AddItem ("Ethel") End Sub

14 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 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 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 17 Unknown number of grades

18 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 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 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 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 22 Moving Button

23 23 Moving Button

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

25 25 Dynamic Control Array

26 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 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 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 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 30 Unloading

31 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 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 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 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 35 Three Rows 0123 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

36 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 37 Marilyn and Andy

38 38 Marilyn and Andy

39 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 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


Download ppt "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."

Similar presentations


Ads by Google