Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 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 – Sliders – Arrays of user defined types v Controls - Frames, Radio Buttons, Check Boxes v Controls - List, combo, picture, image, timer, shape v Data Validation

2 Lec4 P 2 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Looping Constructs v Looping constructs in Visual Basic: v For...Next Loop – used to perform a statement (or set of statements) a fixed number of times – you should know how many times you want to perform the statements inside the loop before you reach the loop v Do...Loop – used to perform a statement (or set of statements) one or more times, depending on whether the condition at the top or bottom of the loop is true or false

3 Lec4 P 3 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton For...Next Loop Construct v In C : for (count=startvalue; condition; increment) { statements; }  In VB :: Students to add syntax Count can be any of these types: u Integer or Long, Single or Double, Currency

4 Lec4 P 4 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton For...Next Loop Step  We can also specify the step size and count backwards: Sub Command1_Click () Dim iCount As Integer For iCount = 100 To 0 Step -10 Print "iCount = "; iCount Next iCount End Sub v Unless a Step is specified the For loop will increment by 1 each time v You should never change the value of the control variable inside the For loop

5 Lec4 P 5 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Do...Loop Forms v There are a number of forms of the Do...Loop: General form : Do [{While / Until}] condition statements exitdo ‘can be used to terminate the loop Loop same as break in C Do statements exitdo Loop [{While/Until}] condition

6 Lec4 P 6 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Do While condition….Loop 1.Repeat while loop Do While condition statements Loop v Tests condition first v Do While executes whilst the condition is True, terminating when it becomes False v Statements within looping structure are only executed if the condition is true

7 Lec4 P 7 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Do...Loop Until condition 2.Repeat Until Loop Do statements Loop Until condition v Loop will always execute at least once. v Loop executes Until the condition is True,

8 Lec4 P 8 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Examples (1) Students to add example

9 Lec4 P 9 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question v Write the equivalent C structures for : (a) the repeat while loop (b) the repeat until loop

10 Lec4 P 10 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Answers Students to add answer

11 Lec4 P 11 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Refresh/DoEvents v While in a loop VB directs all the computers processing to calculations in the loop - can cause problems. v Refresh forces the object to be refreshed eg. Lable1.Caption.Refresh v DoEvents forces checks on input from the user. eg. DoEvents v Consider For inum=1 to 1000 label1.Caption=Str$(inum) Next

12 Lec4 P 12 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Arrays - Defining an Array v Arrays can be delared for any type using the standard declarations : Dim - at module, form or procedure level Global - at module level Static - at procedure level v Delaring the size of the array Students to add examples An array variable can be declared in a code module, a form, (or a procedure see later notes)

13 Lec4 P 13 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Accessing an Array v Array name and the element number are used  Dim asName(5) As String asName(2) = “Pete” asName(3) = “Lucy” Text1.Text = asName(4) (1) “Jane” (3) “Lucy” (2) “Pete” (4) “Dave” (5) “Ian” asName

14 Lec4 P 14 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Array Bounds & Option Base v Arrays have a lower address or lower bound, and an upper address or upper bound v We can alter the default lower bound by using Option Base in the general declarations section of a form or module v Option Base can be set to either 0 or 1 –Option Base 0 ‘sets the lower bound to 0 –Option Base 1 ‘sets the lower bound to 1 v Dim asName(5) As String Option Base 1 Option Base 0asName01234 51234

15 Lec4 P 15 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Explicitly Stating Array Bounds v We can just declare how many elements we want in our array and Visual Basic will set them up using the Option Base setting: Dim asName(5) As String  We can however state explicitly the lower and upper bounds that we want for the array Dim asName(5 To 9) As String Option Base 1asName51234 95678

16 Lec4 P 16 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Using Arrays in Applications v Let’s consider a simple application which stores up to 5 names: Sub Command2_Click () Dim iPos As Integer iPos = Val(Text1.Text) If (iPos>=1) And (iPos<=5) Then Text2.Text = asNames(iPos) Else Text2.Text = "Error" End If Text1.SetFocus End Sub Sub Command1_Click () Dim iPos As Integer iPos = Val(Text1.Text) If (iPos>=1) And (iPos<=5) Then asNames(iPos) = Text2.Text Else Text2.Text = "Error" End If Text1.SetFocus End Sub Option Base 1 Dim asNames(5) As String In the form’s general declarations:

17 Lec4 P 17 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Multi-Dimensional Arrays v Visual basic will allow us up to 60 dimensions!  What about a three dimensional chess board? Dim asBoard(1 To 8, 1 To 8, 1 To 8) As String asBoard(1,4,3) = “White Knight” v We would advise you not to use too many dimensions, otherwise you will become confused v Best idea is only to use multi-dimensional arrays where they map clearly to the real-world

18 Lec4 P 18 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Clearing Arrays  To clear a complete array you can use the Erase command: Students to add statement v This resets all fields to their ‘null’ values

19 Lec4 P 19 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Dynamic Arrays v Array size is allocated on demand  In the declaration Section: Dim asNames() As String  Within a procedure: ReDim asNames(10) or ReDim Preserve asNames(15)

20 Lec4 P 20 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Finding Array Boundaries  To find the bounds of a single dimension array we can use: iLowerBound = LBound( asNames ) iUpperBound = UBound( asNames )  To find multi-dimensional bounds: Students to add statement

21 Lec4 P 21 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question - Good Practice v Why is the following consider good practice? Const NOOFSTUDS = 30 Dim Ages(NOOFSTUDS) As Integer For icount=1 to NOOFSTUDS iTotAges = iTotAges + Ages(icount) Next icount

22 Lec4 P 22 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Data Validation Controls - Scroll Bars v There are two types of Scroll Bars Vertical & Horizontal v Main properties to set are: Max Min SmallChange LargeChange v Property usually retrieved is Value v The events usually processed are Change or Scroll – Change: when the position has changed message is received – Scroll: when the thumbbar moves the message is received

23 Lec4 P 23 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question - arrays of records + Scroll bars v Telephone Data entered into the text boxes is to be written into a UDT array at an index given by the value of a scroll bar. v A scroll bar change event displays the data stored in the UDT array at the index given by the scroll bar value. v Show code

24 Lec4 P 24 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Controls: Option (Radio) Button v A Radio Button is one of the controls that can be used on a group frame v The property that we are usually interested in is Value Option1.Value = FALSE Option1.Value = TRUE v Grouped Radio Buttons are mutually exclusive v Can process on an Option Click event or wait for a command event v A Click event is automatically generated if you assign a value to the Value property of an option from within a program

25 Lec4 P 25 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Controls: Radio Button Example Usage  Checking which option is selected: Sub Command1_Click () If Option1.Value Then Label1.Caption = "Option1 Selected" ElseIf Option2.Value Then Label1.Caption = "Option2 Selected" ElseIf Option3.Value Then Label1.Caption = "Option3 Selected" Else Label1.Caption = "No Options Selected" End If End Sub

26 Lec4 P 26 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Controls: Check Box 1 v A Check Box is another control that can be used on a group frame: v Check Boxes can be control arrays Check1.Value = 0 ’unchecked Check1.Value = 1 ’checked

27 Lec4 P 27 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Controls: Group Frame v Frames can be used to group sets of controls, both visually and programmatically: v The Click event can be used v When you place certain new controls on top of the frame they are automatically grouped together v If controls are not on a frame they are grouped on a form v Radio Buttons can be control arrays Group Frame Control

28 Lec4 P 28 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Controls: Check Box 2  The code behind the Show Checked command button would be: Sub Command1_Click () ‘clear the label caption Label1.Caption = "" ‘add checked options to the label If (Check1.Value) Then Label1.Caption=Label1.Caption+"Check1 Checked"+Chr(13) End If If (Check2.Value) Then Label1.Caption=Label1.Caption+"Check2 Checked"+Chr(13) End If If (Check3.Value) Then Label1.Caption=Label1.Caption+"Check3 Checked"+Chr(13) End If End Sub

29 Lec4 P 29 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Controls : List and Combo Boxes v Used to select item(s) to be processes. v Combo boxes open up when selected v Combo boxes have an entry/edit area

30 Lec4 P 30 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton List Box v A list box can be used to display & select items, it can be sorted Items are stored in an array starting at index 0 v Useful Methods: Students to add methods

31 Lec4 P 31 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton List Box Useful Methods 1  AddItem adds the item specified to the List Box List1.AddItem “Item Text”  RemoveItem removes the item specified from the List Box List1.RemoveItem 1  Selected indicates if the specified item is currently selected List1.Selected(1)  ListIndex indicates the first selected item in the List Box iSelItem = List1.ListIndex

32 Lec4 P 32 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton List Box Useful Methods 2  Clear clears all entries from the List Box List1.Clear  ListCount indicates how many items are currently in the List Box List1.ListCount  List is the array containing each of the items that are currently in the List Box Label1.Caption = List1.List(iCount) v Click is the event that is normally processed by the programmer

33 Lec4 P 33 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Combo Box v A combo box is like a list box that only displays the currently selected item, it drops down to allow selection, it can be sorted Items are stored in an array starting at index 0 v Useful Methods: Students to add methods

34 Lec4 P 34 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Comparing List box & Combo box v Occupies only one line on form when not in use. Drop-down list appears when in use. v Can select from entries shown, or type in another value v Can select only one entry v Can display only one column v Occupies several lines on form at all times v Can select only from entries shown v Can select several items v Can display list in multiple columns List box Combo box

35 Lec4 P 35 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Question v Write a section of code to add items to a list box under program execution - take the items from a text box. v Provide a count of the number of items in the list box v When an item is selected from the list box copy the item to a label box.

36 Lec4 P 36 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Timer

37 Lec4 P 37 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton Picture, Image, Shape v Students to write notes on the above heading.


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

Similar presentations


Ads by Google