Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,

Similar presentations


Presentation on theme: "More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,"— Presentation transcript:

1 More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,

2 Data Type At the lowest level, all data stored in the computers is written in 1’s and 0’s (binary) How that data gets interpreted, what actions can be performed on it, etc. depends in part on the “data type” associated with it We let the computer know how we mean to interpret a variable by declaring its type, e.g. Dim Count As Integer ‘ (Option Explicit makes VB strongly typed)

3 Some VB types integer : a whole number, that is a number with no fractional part. single: A number with a decimal point, such as 3.14159 (called a “float” in C) double: A number with a decimal point, such as 3.14159265358 (called a “float” in C); is more precise than a single; it also allows for a greater range of numbers

4 More VB Types String: words or text, that is, groups of letters, numbers and symbols Boolean: a variable that can only be true or false (real type as opposed to C’s) Currency: data type used to represent money (dollars and cents); only two decimal places are shown; takes care of unusual rounding associated with money (pp. 26-30 in VB & VBA)

5 The Control type When you drag a control onto a form, you don’t need to declare it Reserving memory locations and so on is done automatically However, if you need to remember a control (e.g. to keep track of what button you pressed), you declare a control

6 Command Type Demo

7 The Control Type (cont.) Dim ControlTrack As Control Private Sub cmdButton1_Click() Set ControlTrack = cmdButton1 End Sub Private Sub cmdButton2_Click() Set ControlTrack = cmdButton2 End Sub Private Sub cmdButton3_Click() Text1.Text = "you last pressed " + ControlTrack.Caption End Sub

8 Binding You will notice a difference between typing “cmdButton1.” and typing “ControlTrack.” In the first case, VB knows that it is a CommandButton control and so provides the properties in a drop-down list (an example of early binding) In the second case, VB does not know the type of control that ControlTrack is until the program runs, hence, it cannot provide the properties (an example of late binding)

9 If-Then Tests a condition and executes statements within the body (block) of the if structure provided the condition was true and skips over those steps otherwise (pp.366-370 VB & VBA)

10 If-Then Example If State=“PA” Then Price=Price*(1.06) End If ‘State ‘Recalculates the price factoring in the PA ‘sales tax if the state is PA ‘Note strings are more easily compared in VB

11 Condition A condition is an expression that evaluates to a Boolean value (true or false) It often involves a comparison A > B (is A greater than B?) A >= B (is A greater than or equal to B?) A < B (is A less than B?) A <= B (is A less than or equal to B?) A=B (is A equal to B?) A <> B (is A not equal to B?) ‘(does not use !)

12 Compound Conditions Boolean operators can be used to combine conditions And: both expressions must evaluate to true for combination to be true Or: if either expression evaluates to true, the combination is true Not: if an expression evaluates to true, Not(expression) evaluates to false and vice versa

13 If-Then-Else The If-Then-Else structure provides two statement blocks, one to execute if the condition is true, the other to execute if the condition is false

14 If-Then-Else Example If State=“PA” Then If City = “Phila” Then Price=Price*(1.07) Else Price=Price*(1.06) End If ‘City End If ‘State ‘This is also an example of nested if’s (an if within an if)

15 ElseIf If Grade > 94 Then Letter_Grade = “A” ElseIf Grade > 86 Then Letter_Grade = “B” ElseIf Grade > 78 Then Letter_Grade = “C” ElseIf Grade > 70 Then Letter_Grade=“D” Else Letter_Grade=“F” EndIf

16 For-Next Loop One of the structures used for repeating steps A condition is tested, specifically whether a counter falls within some limit If the condition is true, the statements in the for- next block are executed, at the “bottom” the counter is incremented, and one returns to the “top” of the loop to test the condition again If the condition is false, the for-next statements are not executed and the next statement executed is the one following Next

17 For-Next Example (pp. 320-322 in VB & VBA) For i=1 To Num_Years Principle=Principle*(1+ Interest/100) Next I ‘Calculates Interest (compounded yearly) on Principle ‘over Num_Years

18 Multiple Forms A project can consist of a few forms Typically one form (the start-up form) appears first Events associated with it bring up the other forms One can have multiple forms showing, or one can hide all but the one the user is interfacing with

19 Message Box A special case of multiple form is a message box, a little window with a warning popping up Message Box function (pp. 429-432 in VB & VBA) Dim r As Integer Private Sub Command1_Click() r = MsgBox("Body Message", vbOKOnly, "Caption Message") End Sub

20 Form Events To control various forms we want to understand their methods and events Initialize: occurs when form is first referenced, occurs once unless form is “terminated” Load: occurs when the specific properties of the form are put into memory, occurs once unless form is “unloaded”

21 More Form Events Resize: occurs when the form is moved or its size is altered There’s a Resize called by load Activate: occurs when a user switches forms The events occur in the order given above Initialize  Load  Resize  Activate

22 Form methods Show: displays (makes visible) a form This will include the loading a form if the form was not loaded previously Hide: makes a form invisible (pp. 37-43 in Visual Basic Controls)

23 Adding another form Go to Project/Add Form Choose whether you want to make a New form or include an already Existing form An existing form is not copied and brought into the project folder, you must choose to do that (right click on the form in the upper right hand side of the IDE and choose Save frmWhatever As …

24 Scope Procedure-level: variable is available only with subroutine or function in which it is declared (local) Module-level: variable is available to all subroutines within a module, e.g. all the subroutines associated with a form Friend: variable is available to other forms within a project, but is not available to anyone outside the project Public: variable is available to other projects

25 Multi-Form Demo

26 Form 1 Code Public FirstName As String Private Sub cmdShowForm2_Click() FirstName = txtForm1.Text frmForm2.Show frmForm1.Hide End Sub

27 Form 2 Code Private Sub cmdReturn_Click() frmForm2.Hide frmForm1.Show End Sub Private Sub Form_Activate() txtForm2.Text = frmForm1.FirstName End Sub

28 ListBox

29 Some ListBox Properties and Methods MultiSelect: allows more than one item to be selected 1-Simple SelCount: gives the number of highlighted items Text: the text of the currently highlighted item AddItem: adds a string to the list (pp. 182-186 in Visual Basic Controls)

30 Some ListBox Code Private Sub Form_Load() lstCourse.AddItem ("csc 362") End Sub Private Sub lstCourse_Click() If lstCourse.SelCount = 1 Then txtCourse1.Text = lstCourse.Text End If If lstCourse.SelCount = 2 Then txtCourse2.Text = lstCourse.Text End If End Sub

31 Radio Buttons

32 OptionButtons A.k.a. Radio Buttons Used if one and only one choice must be made The one and only one logic is built into the radio button controls The default method on an OptionButton is associated with the click event; it tells one that that particular choice was most recently selected Important property: value: Boolean determines if that buttons was the one selected (true) (pp. 292-296 Visual Basic Controls)

33 Frames in action

34 Frame Recall a form provides a surface or container for controls A frame is like a form within a form (a container within a container) It groups controls together, so that can be treated as a separate set Often used if there is more than one set of radio buttons

35 Some Frame Code (pp. 142-143 Visual Basic Controls) Private Sub cmdCommand1_Click() fraFrame1.Enabled = False End Sub ‘Disables everything contained in Frame1


Download ppt "More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,"

Similar presentations


Ads by Google