Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.

Similar presentations


Presentation on theme: "1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier."— Presentation transcript:

1 1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier

2 2 This Week – Managing Lists Controls for displaying or selecting from a list –List Box –Combo Box Arrays – Used for storing numerous related values –What an Array is –How to declare with different scopes (Global, Module, Static) –Accessing Elements –Using LBound and UBound –Resizing Arrays and maintaining values while doing so

3 3 The ListBox Control The ListBox control allows the user to view a list of items The ListBox control also allows the user to select one or more items from a list – if the programmer (you) allows multiple selections

4 4 ListBox Summary The ListBox supports: –Properties Columns, List, ListCount, ListIndex, MultiSelect, SelCount, Sorted, Text, and numerous other standard ones like Visible, Enabled, TabStop, etc. –Methods AddItem, Clear, RemoveItem –Event Procedures Click, DblClick, GotFocus, LostFocus

5 5 Managing the ListBox List You can initialize elements in the Design Mode using the List property To add elements to the ListBox: lstItems.AddItem item[, index] lstItems = name of the control item = string expression to add to the control index = position of the item in the list (starts at 0) To remove a single item from the ListBox (Example 14.2) lstItems.RemoveItem index index = position of the item to be removed (0-based) To remove all items from the ListBox: lstItems.Clear To sort the items in the ListBox lstItems.Sorted = True

6 6 Selecting Items in the List To programmatically “select” a specific item in the ListBox (only at runtime) lstItems.Selected(index) = True Determining what item a user has selected –ListIndex – the index of the selected item in a list -1 = no item is currently selected N = Nth item selected – varies from 0 to ListCount – 1 –Or loop through items looking for one with Selected = True ListCount - the number of items in a list Example 14.1

7 7 Selecting Multiple Items from a List Box You may enable a ListBox to allow a user to select more than one item Syntax –listbox.Multiselect = setting Where setting is: 0 = Multiple selection not allowed (default) 1 = Simple multiple selection (click or spacebar) 2 = Extended multiple selection (Shift+click, Shift+arrow key) –This property can also be set in Design Mode Use a ListCount to loop through the entire list and determine which individual items have Selected(index) = True Example 14.4

8 8 Do It Together! Write a program that allows the user to enter String s in a TextBox. Each String input is added to a ListBox. As each String is added to the ListBox, ensure that the String s are in sorted order. You should also add a CommandButton, that when clicked adds the String in the TextBox to the ListBox and clears the TextBox.

9 9 Sample Interface

10 10 Sample Solution Option Explicit Private Sub cmdAdd_Click() ' lstBox's Sorted property has been set to True lstBox.AddItem(txtInput.Text) End Sub

11 11 The ComboBox Control The ComboBox control combines TextBox features with a short drop-down list. The ComboBox enables the user to select a single item from a list The user’s selection appears in the TextBox

12 12 Like a ListBox The ComboBox supports most of the same Properties, Methods, and Event Procedures that ListBox does. –Properties Enabled, List, ListCount, ListIndex, Sorted, Visible –Methods AddItem, Clear, RemoveItem –Event Procedures GotFocus, LostFocus Adding elements to the list is the same as a ListBox –Programmatically The AddItem Method –Design Mode The List Property

13 13 Not Like a ListBox Other than the obvious visual differences, the primary difference between a ComboBox and a ListBox is how you determine what the user’s selection is – the user’s selection is contained in the Text Property Syntax cboCities.Text –Example 14.3 A ComboBox also supports three different visual appearances via the Style Property –vbComboDropDown: allows the user to type data directly into the text area or select items from the list –vbComboSimple: has the visual appearance of a TextBox – a drop- down list never appears –vbComboDropDownList: only allows selections from the list, no typing allowed

14 14 Do It Together! Create a GUI with a ComboBox and a ListBox. Write a program that displays the names of 15 states (or whatever) in a ComboBox. When the user selects an item from the ComboBox, remove the item from the ComboBox and add it to the ListBox. Your program should check to ensure that the ComboBox contains at least one item. If it doesn’t, print a message using the MsgBox function and terminate program execution.

15 15 Interface Example

16 16 Sample Solution Option Explicit Private Sub Form_Load() 'Or simply load in the List at Design-Time cboStates.AddItem("Iowa") cboStates.AddItem("New Jersey") cboStates.AddItem("Nebraska") cboStates.AddItem("Delaware") cboStates.AddItem("Colorado") cboStates.AddItem("Illinois") cboStates.AddItem("Wyoming") cboStates.AddItem("Michigan") cboStates.AddItem("Montana") cboStates.AddItem("Kentucky") End Sub Private Sub cboStates_Click() lstBox.AddItem(cboStates.List(cboStates.ListIndex)) cboStates.RemoveItem(cboStates.ListIndex) If cboStates.ListCount = 0 Then MsgBox("ComboBox is empty. Terminating.") End End If End Sub

17 17 Arrays An array is a sequence of multiple locations in memory where values can be stored –Individual memory locations are accessed with an index Arrays provide a way to group related data items Arrays allow you to avoid developing unique variable names for similar items Each element in an array has the same data type – simple (Integer, Boolean, etc.) or complex (UDTs or Classes) Use a single array element in the same places you’d use normal variables – assign values to them, retrieve from them, use them in expressions, etc. Syntax for declaring an array: Dim friends(5) As String Dim moreFriends(1 To 5) As String

18 18 Accessing Array Elements Declare Dim Days(3) As String Accessing lblDayOfWeek.Caption = Days(3) Setting Days(4) = “Wednesday” Displaying Array Contents For OfMine = 0 To 4 Print Spc(25); Friend(OfMine) Next OfMine Example 14.5

19 19 Array Scope Like all variables, array variables have scope –Global Array: Place a Public statement in the Declarations section of a standard code module –Module-level Array: Place a Dim statement in the General Declarations section of a form module or standard module –Array with Local Scope: Declare the array within a procedure: Static scope for the array Static scope for the entire procedure and Dim for the array You can change the size of local array variables - Redim

20 20 Array Resizing Arrays can be dynamically resized at the procedure level ReDim arrayVar(x To y) As dataType Dynamic allocation (resizing) is important: –Array size can change as items are added/deleted –The size of the list may not be known in advance –Allows an array to be declared at the procedure level –Makes it possible to release all memory allocated for an array ReDim friends(0) By default, the values in an array are lost when you resize –To retain current values, use the Preserve keyword ReDim Preserve friends(1 To 4) –Example 14.9

21 21 Using LBound and UBound Functions Arrays in Visual Basic are zero-based, by default You can change to a different lower bound three ways: –Option Base 1 lower bound of 1 for arrays Placed in the Module’s General Declarations section –Dim days(1 To 7) As String –ReDim days(1 To 7) As String Visual Basic provides two functions you can use to determine the lower and upper bounds of an array - LBound and UBound Syntax: LBound(arrayname) UBound(arrayname) –arrayname = Name of the array variable Example 14.6

22 22 Using Control Arrays and Variable Arrays Together As we’ve seen before, Control Arrays are a group of controls that have the same name and are accessed via an index Info to be displayed in the Control Array could be stored in an Array Example txtText(element).Text = arrayVariable(element) Examples from the Textbook –14.7: The days of the week displayed in a Label Control Array –14.8: Creating an application with Variable Arrays

23 23 Just Do It! Write a program that simulates the rolling of two dice. The program should use the Rnd to roll the first dice, and should use Rnd again to roll the second dice. Each time you roll the pair of dice you should add the two values together. Your program should roll the dice 36,000 times. You should use an Array to tally how many times each possible sum appeared. Since each dice can have a value between 1 and 6, the possible sums of the dice are between 2 and 12. Once your program has rolled the dice 36,000 times then you should print a table telling how many times each sum was rolled.

24 24 Sample Solution Option Explicit Private Sub Form_Load() ' Array expected contains counts for the expected ' number of times each sum occurs in 36 rolls of the dice Dim i As Long Dim x As Integer, y As Integer, totals(2 To 12) As Long Randomize For i = 1 To 36000 x = 1 + Int(Rnd() * 6) y = 1 + Int(Rnd() * 6) totals(x + y) = totals(x + y) + 1 Next i For i = 2 To 12 Print i & " " & totals(i) & " " & (totals(i) / 36000) Next i End Sub

25 25 Next Class… Next Class –Read Chapter 15 – Multidimensional Arrays –HW #3 will be due at the beginning of class –Complete Mini-Project #3 Following Week –Research Presentations


Download ppt "1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier."

Similar presentations


Ads by Google