1 / 24

Visual Basic Programming II Lecture 4

Visual Basic Programming II Lecture 4. MIS233 Instructor – Larry Langellier. This Week – Collections of Information. Multi-Dimensional Arrays ListBoxes with multiple columns The MSFlexGrid Control Arrays of User-Defined Types. Two-Dimensional Arrays.

giza
Download Presentation

Visual Basic Programming II Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic Programming IILecture 4 MIS233 Instructor – Larry Langellier

  2. This Week – Collections of Information • Multi-Dimensional Arrays • ListBoxes with multiple columns • The MSFlexGrid Control • Arrays of User-Defined Types

  3. Two-Dimensional Arrays • The arrays we learned last week are one-dimensional • This means a single index is employed to retrieve or store information • Visual Basic also supports multi-dimensional arrays • Declaring two-dimensional arrays DimarrayName( L1ToU1, L2 To U2) Astype DimarrayName( B1, B2) Astype DimarrayName( B1, L2 To U2) Astype • Each element in the array is identified by a pair of indices • Accessing an element in a two-dimensional array arrayName(x, y) = aValue where x and y are integers specifying an array location

  4. Two-Dimensional Examples • Look-Up Tables • Used to store and retrieve related information • Examples • 15.1: Computing taxes • 15.2: Using two different look-up tables to compute taxes • We’ll discuss multi-dimensional arrays in a moment, you could have more than two related values • Monthly Sales by Salesperson • Two dimensions: which salesperson and which month • Example • MonthlySales • Checkerboard • Checker pieces or Chess pieces could be denoted in an 8x8 array

  5. Multi-Dimensional Arrays • You aren’t limited to just one or two dimensions (indices) in Visual Basic – you actually have access to up to 60! • To declare more than two dimensions – simply continue to add more comma-separated boundary definers • Procedure Level Example Static monthlySales () As Double ReDim monthlySales (1 To NumOfSP, 12, 1990 To 2010) As Double • Examples • Monthly Sales for several years • List of names, addresses and phone numbers • Building, Floor, Office, Cabinet, File Drawer, Folder, Paper • 3-Dimensional games • Multi-dimensions are rare in business applications, but can be found frequently in mathematics, science and games. • Each new dimension/index adds one more loop nesting level

  6. Do It Together! Let’s extend and modify the Monthly Sales example. Add two command buttons One button, that when clicked, will ask the user to enter a salesperson’s number (1-5), a month (1-12), and an amount for how much was sold. This should be accomplished with three separate InputBoxes. The sales should then be stored in the Array. Another button should first ask the user what quarter they’d like to see a summary for (via an InputBox), and then display that quarter’s summary to the Form.

  7. Interface Example

  8. Sample Solution Dim MonthlySales(1 To 5, 1 To 12) As Double Private Sub cmdAdd_Click() Dim salesPerson As Integer Dim month As Integer Dim salesAmount As Double salesPerson = InputBox("Enter a saleperson's number (1-5):") If salesPerson = 0 Then Exit Sub End If month = InputBox("Enter the month (1-12):", "Monthly Sales") If month = 0 Then Exit Sub End If salesAmount = InputBox("Enter the sales amount:", "Monthly Sales", 0#) If salesAmount = 0 Then Exit Sub End If MonthlySales(salesPerson, month) = salesAmount End Sub

  9. Sample Solution (cont.) Private Sub cmdDisplay_Click() Dim quarter As Integer, startMonth As Integer, i As Integer, j As Integer quarter = InputBox("What quarter would you like a summary of?“) If quarter = 0 Then Exit Sub End If Cls Select Case quarter Case 1: Print "First Quarter Sales" Case 2: Print "Second Quarter Sales" Case 3: Print "Third Quarter Sales" Case 4: Print "Fourth Quarter Sales" End Select startMonth = (quarter - 1) * 3 + 1 For i = 1 To 5 Print "Salesperson " & i; For j = startMonth To startMonth + 2 Print " " & Format(MonthlySales(i, j), "Currency"); Next j Print Next i End Sub

  10. More on Arrays… • Parallel Arrays • Arrays are limited to have elements that are all of the same data type • One way around this limitation is to use a different array for each different data type you need and store related values at the same index value • Sorting an Array’s contents • There are numerous strategies for sorting the contents of an array • Bubblesort – Multiple passes through an array, each pass moves another value into it’s proper position • Not very efficient, but easy to understand • Example – Ex7.7 • Searching an Array • Look through an array to find a sought-after element • Sorted versus Unsorted

  11. Let’s Display Information in Rows and Columns • A ListBox can display it’s values in more than one column • It looks like a Table, even though it functions like a simple list • To do this - change 2 properties aListbox.Columns = N aListbox.Multiselect = True • Example • 15.3: Creating a Multi-Column list for making multiple selections

  12. The MSFlexGrid Control • The MSFlexGrid control is specifically designed to allow the display of two-dimensional tables of info • The MSFlexGrid is not typically available on your toolbox • Adding Toolbox Controls Project Menu -> Components Item -> Controls Tab • Select “Microsoft FlexGrid Control 6.0”

  13. Using the MSFlexGrid Control • Sizing the MSFlexGrid Control • Rows – total number of rows in grid • Cols – total number of grid columns • FixedRows – non-scrolling rows • FixedCols – non-scrolling columns • Cells are located at row/column intersections • You access an individual cell by setting both the Row and Col properties (not Rows and Cols) • Grid1.Row = 1 • Grid1.Col = 1 • Row and Column numbering is 0 (zero) based

  14. MSFlexGrid Summary • Properties • Text – returns or sets the text contents of a cell • CellPicture – returns or sets an image (icon) • CellFont – a group of properties setting the font characteristics of the text in the cell • ColSel, RowSel – returns or sets the start or end for a range of cells • ColWidth, RowHeight – returns or sets width of column (or height of row) in twips • Methods • AddItem – Add text one row at a time, separate values with a Tab • You can also add information one cell at a time using Row, Col, Text • Event Procedures • Click, DblClick, EnterCell, LeaveCell, All Key and Mouse Events, etc. • Examples • 15.4: Entering numbers into the MSFlexGrid • 15.5: Enter items from Text Boxes into an MSFlexGrid

  15. Do It Together! Extend the MonthlySales example from earlier in this lecture so quarterly sales are displayed in an MSFlexGrid. Also, enhance the solution so a user can edit the values in the MSFlexGrid cells. It’s never a good idea to treat an interface control as the storage place for edited information, so make sure all changes in the MSFlexGrid are also stored back to the array.

  16. Interface Example

  17. Sample Solution Dim MonthlySales(1 To 5, 1 To 12) As Double Private Sub flxSales_KeyPress(KeyAscii As Integer) If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Then flxSales.Text = flxSales.Text & Chr(KeyAscii) MonthlySales(flxSales.Row, flxSales.Col) = Val(flxSales.Text) End If End Sub Private Sub Form_Load() Dim i As Integer With flxSales .Row = 0 .Col = 1 .Text = "Jan" .Col = 2 .Text = "Feb" .Col = 3 .Text = "Mar" .Col = 0 For i = 1 To 5 .Row = i .Text = "Person " & i Next i End With End Sub

  18. Declaring Your Own Data Types • User-defined types combine variables of several (potentially) different types to define a new type • Syntax: [Private | Public] Typemyusertype elementname [(subscripts)] AsTypeName [elementname [(subscripts)] AsTypeName] End Type • Example Private Type Person Name As String Age As Integer End Type

  19. User-Defined Types (cont.) • Declaring a Variable of the New Type • DimmyVarAsmyusertype • Example PublicTypeAsset NameAs String * 20 LocationAs Integer ValueAs Currency End Type DimInventoryAs Asset • Array of User-Defined Variables DimInventory(1 To 100) As Asset • Example • 15.6: Extending options using an Array of User-Defined Type

  20. Do It Together! We can’t leave the Monthly Sales example now! Add a UDT that represents a salesperson. This UDT should provide an attribute for the person’s name and another attribute which is a two-dimensional array of monthly sales figures for 10 years. Your user interface should have 2 primary controls: a ListBox for selecting a salesperson (by Name) an MSFlexGrid for displaying the ten years of sales data Don’t bother providing a data entry mechanism, simply populate the name and randomly generate 10 years worth of sales figures for each salesperson in the Form_Load event procedure

  21. Interface Example

  22. Sample Solution In a Code Module Public Type SalesPerson Name As String Sales(1995 To 2005, 1 To 12) As Double End Type In a Form Dim MonthlySales(1 To 5) As SalesPerson Private Sub cboSalesPerson_Click() Dim i As Integer, j As Integer For i = 1 To 12 For j = 1995 To 2005 flxSales.Row = j - 1994 flxSales.Col = i flxSales.Text = _ Format(MonthlySales(cboSalesPerson.ListIndex).Sales(j, i), "Currency") Next j Next i End Sub

  23. Sample Solution (cont.) In a Form Private Sub Form_Load() Dim i As Integer, j As Integer, k As Integer With cboSalesPerson .AddItem "Jane" .AddItem "John" .AddItem "Mary" .AddItem "Nancy" .AddItem "Tom" End With MonthlySales(1).Name = "Jane" MonthlySales(2).Name = "John" MonthlySales(3).Name = "Mary" MonthlySales(4).Name = "Nancy" MonthlySales(5).Name = "Tom" Randomize For i = 1 To 5 For j = 1 To 12 For k = 1995 To 2005 MonthlySales(i).Sales(k, j) = 500 + (Rnd() * 5000) Next k Next j Next i End Sub

  24. What’s Next? • Next Class • Research Presentations • HW #4 will be due at the beginning of class • Following Week • Read Chapter 18 – Sequential Files • HW #5 will be due at the beginning of class • Midterm Project Handout

More Related