control structures part 1 if then control. index of projects in this ppt absolute value simple...
Post on 20-Dec-2015
214 views
TRANSCRIPT
![Page 1: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/1.jpg)
Control structures
Part 1 if then control
![Page 2: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/2.jpg)
Index of projects in this ppt
• Absolute value• Simple payroll• Letter grade assignment• Dental billing• GPA calculator• Boolean operations –nearly completed• Security panel• GPA calculator revisited for + and - grades• Quadratic equation –two versions
![Page 3: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/3.jpg)
If statement
• An If statement is used to make a (run time) decision on how processing should occur.
• Code is provided for each case.• Examples:
– Should employee get overtime pay?– Is a numeric value greater than zero?– What letter grade should be assigned for a
student?
![Page 4: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/4.jpg)
The if stmt is the simplest control structure
Simple if syntax:
If condition thenStmts go hereEnd if
![Page 5: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/5.jpg)
condition
• The condition is a boolean expression. • It could be a boolean variable or an
expression involving booleans.• It could be a comparison between
comparable variables and values• Typically, we use operators =, >, <,
<>,<=,>= to compare numerical expressions for equality, greater than, less than, not equal, etc.
![Page 6: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/6.jpg)
Example 1
• If you have declared
Dim val as Integer
• Then the following are legal boolean expressions (but not necessarily legal VB statements)Val=0 ‘ is val zero?
Val<0 ‘is val less than zero?
Val<>0 ‘is val different from zero
![Page 7: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/7.jpg)
Example 2
• If you have declared
Dim hours as Single ‘ or Double
• Then the following are legal boolean expressions (but not necessarily legal VB statements)hours<40 ‘work less than 40 hours?
Hours>40 ‘work overtime?
Hours<>0 ‘work any hours this week?
![Page 8: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/8.jpg)
Example 2A
• Assuming variables for othours, hoursworked, basepay and pay of type double (or single) you could use an if to calculate overtime pay:
If hoursworked>40 then
Pay= othours*1.5*rate+basepay
endif
![Page 9: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/9.jpg)
Example 3
• If you have declared
Dim ok as Boolean
• Then the following are legal boolean expressions (and legal VB statements )Ok=false ‘is it false? Set it false.
Ok=a>b ‘set ok to the result of another compare operation
![Page 10: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/10.jpg)
Putting things together
• An application that displays the absolute value of a (real) number
![Page 11: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/11.jpg)
How does it work?
• I defined txtInput_Click event.
• When the input box is clicked, I convert the input string into a decimal value.
• I check (use an if!) to see if it is less than zero (and multiply it by -1 if it is).
• Otherwise the value is displayed unchanged.
![Page 12: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/12.jpg)
The event handling subroutine Private Sub txtInput_click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles txtInput.Click
Dim input As String Dim value As Doubletry ‘check to make sure input is numeric value = Double.Parse(txtInput.Text) If value < 0 Then value = -value ‘flip to positive if it was neg End If lblAnswer.Text = "absolute value is " &
value.ToString()Catch…End try End Sub
![Page 13: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/13.jpg)
What I didn’t do
• I didn’t bother checking for legal input.
• I’ll leave as an exercise to add a try… catch…
![Page 14: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/14.jpg)
Simple payroll
![Page 15: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/15.jpg)
Simple payroll
![Page 16: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/16.jpg)
In button click event handler
Dim rateString, HoursString As String Dim rate, hours, pay As Double rateString = txtRate.Text HoursString = txtHours.Text‘should put try catch hererate = Decimal.Parse(rateString) hours = Decimal.Parse(HoursString) pay = rate * hours ‘basic pay If hours > 40 Then ‘need to pay some overtime pay = pay + (hours - 40) * rate * 0.5 End If txtPay.Text = pay.ToString("C")End Sub
![Page 17: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/17.jpg)
What about empty fields?
![Page 18: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/18.jpg)
Lots more could be done
• Use try catch to find number format errors
• Display better error messages
![Page 19: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/19.jpg)
To handle “either or” case
If condition then
True clause stmts
Else
False clause stmts
End if
![Page 20: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/20.jpg)
To handle multiple cases
If boolean-expression then
Stmts
Elseif boolean-expression then
Stmts
Elseif….
Else: …’default… not handled above
End if
![Page 21: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/21.jpg)
Reserved symbols
• If, else, then and end if are reserved symbols
• That means, for example, you can’t use them for var names.
![Page 22: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/22.jpg)
Example 3: A letter grader
• User enters a student’s average (should be a real between 0 and 100)
• Form displays the letter grade to assign this student.
![Page 23: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/23.jpg)
The letter grade example
![Page 24: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/24.jpg)
The letter grade example
![Page 25: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/25.jpg)
Input text changed code
Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged
lblResult.Text = "hit calculate button"
End Sub
![Page 26: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/26.jpg)
Buttonclick code Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCalc.Click Dim input, answer As String Dim ave As Double input = txtInput.Text answer = "Grade is " Try ‘try…catch needed for number format exception ave = Decimal.Parse(input) If ave < 50 Then answer &= "E“ ‘must be E ElseIf ave < 60 Then answer &= "D“ ‘E already assigned, so it must be D ElseIf ave < 75 Then answer &= "C“ ‘etc ElseIf ave < 85 Then answer &= "B" Else : answer &= "A“ ‘note colon for last case End If Catch ex As FormatException answer = "input error" End Try lblResult.Text = answer
End Sub
![Page 27: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/27.jpg)
What could be added?
• Check to make sure the input is between 0 and 100.
![Page 28: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/28.jpg)
Boolean operators
• And (andAlso), or (orElse), not, xor are operators for boolean values
• Not is a unary operator, the others are binary.
![Page 29: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/29.jpg)
Boolean operators: And (andAlso)
• Generally, truth tables are used to display how each works.
A B A and B
False False False
False True False
True False False
True True TRUE
![Page 30: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/30.jpg)
Or (OrElse)
A B A or B
False False False
False True True
True False True
True True TRUE
![Page 31: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/31.jpg)
Xor
A B A xor B
False False False
False True true
True False true
True True false
![Page 32: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/32.jpg)
A boolean operator form
![Page 33: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/33.jpg)
A boolean operator form
• Uses a combobox for the user to select from. A combobox holds a collection, in this case strings, which the programmer enters into the properties for the control.
![Page 34: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/34.jpg)
Items property is a collection: enter choices
![Page 35: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/35.jpg)
A project to display the boolean operations
![Page 36: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/36.jpg)
The boolean operations project• We’ll use a combobox, which gives the user a selection
of strings to choose from… we define the strings in an edit window
![Page 37: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/37.jpg)
Names
• I named my labels lbl00, lbl01, lbl02, lbl10, lbl11, lbl12, etc.
• I named my combobox operators. Recommended naming of comboboxes starts with “cbo…”
![Page 38: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/38.jpg)
The running form
![Page 39: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/39.jpg)
xor
![Page 40: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/40.jpg)
Combobox codePrivate Sub
operator_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles operator.SelectedIndexChanged
Dim choice As String
choice = operator.SelectedItem
lbl02.Text = False ‘ this one is always false
‘use if ..else stmt hereto check which cb ‘choice and assign labels values
End Sub
![Page 41: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/41.jpg)
I do show all code for this example
• This is one of your choices for this week’s project but I did not provide any code for it here.
![Page 42: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/42.jpg)
If examples: A Dental Billing Form
![Page 43: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/43.jpg)
Our control logic
• On button click:– Clear previous total
• If no name entered or no boxes checked display message
– Else• Set billtotal to zero
• if cleaning is checked add cleaning cost to total
• if filling is checked add filling cost to total
• if x-ray is checked add xray cost to total
• Display total (as currency)
![Page 44: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/44.jpg)
The working Dental Billing form
![Page 45: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/45.jpg)
Failed to enter name
![Page 46: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/46.jpg)
Failed to select service
![Page 47: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/47.jpg)
Button click code Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim total As single Dim clean, fill, xray As Boolean Dim name As String total = 0 name = txtName.Text clean = cbClean.Checked fill = cbfill.checked xray = cbXRay.Checked If name = "" Then 'no name given txtTotal.Text = "You must enter name" Else ‘make sure a service is selected If Not (clean Or fill Or xray) Then 'error no service txtTotal.Text = "You must select services" Else 'seems to be a name and services If clean Then total += Decimal.Parse(lblClean.Text.Substring(1)) ‘total =total +35.50 End If If xray Then total += Decimal.Parse(lblXRay.Text.Substring(1)) End If If fill Then total += Decimal.Parse(lblFill.Text.Substring(1)) End If txtTotal.Text = total.ToString("C")
End If
End If End Sub
![Page 48: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/48.jpg)
Other ways to do it
• You could open a messagebox to display errors• When a checkbox click state is changed you
could display a message for the user to recalculate the bill total
• I got the numeric value of services out of the labels using substring. This could be done differently… for example, define numeric fields in the class for service costs and use these numbers both for label display and to add up the total.
![Page 49: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/49.jpg)
A GPA calculator
• Allow grades to be entered.
• Compute GPA for current list of grades
![Page 50: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/50.jpg)
The GPA calculator: checks data
![Page 51: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/51.jpg)
GPA calculator keeps a list
![Page 52: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/52.jpg)
GPA calculator uses listbox
• You’ll need to look at next slideshow or in your book to see how to add and configure and use a listbox
![Page 53: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/53.jpg)
GPA calculator computes current GPA
![Page 54: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/54.jpg)
How does it work?
• You need to keep track of both current count of grades entered and current tally of grade values
• Those will need to be field values and you need to initialize them before processing begins.
![Page 55: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/55.jpg)
How does it work?
• In add grade button, check the grade for legal value, and then add it to the list if it’s ok
• In compute gpa button, check to make sure gradecount>0. If it is go ahead and display the gpa (the quotient of sum of grades divided by count of grades).
• I used a function to return grade’s point value
![Page 56: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/56.jpg)
Add button event sub• Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnAdd.Click• Dim grade As String• grade = txtLetter.Text• If grade <> "A" And grade <> "B" And grade <> "C" And grade <> "D" And grade <> "E"
Then• MessageBox.Show("you must enter A,B,C,D,or E", "input error", MessageBoxButtons.OK
_• , MessageBoxIcon.Error)• Else• lstGrades.Items.Add(grade)• gradecount += 1• gradesum += getValue(grade)• ' txtGPA.Text = gradecount.ToString() & " " & gradesum.ToString()• End If• txtLetter.Clear()
• End Sub•
![Page 57: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/57.jpg)
A function to return grade value from a String
Private Function getValue(ByVal grade As String) As Double If grade = "A" Then getValue = 4.0 ElseIf grade = "B" Then getValue = 3.0 ElseIf grade = "C" Then getValue = 2.0 ElseIf grade = "D" Then getValue = 1.0 Else :
End IfEnd Function
![Page 58: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/58.jpg)
getGPA button eventPrivate Sub btnGPA_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGPA.Click Dim gpa As Double Dim answer As String answer = "error" If gradecount <> 0 Then gpa = gradesum / gradecount answer = gpa.ToString("N") End If txtGPA.Text = answer
End Sub
![Page 59: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/59.jpg)
Disabling/enabling a button
• The field enabled on a button can be set to true or false in properties or in your code to turn on or turn off a button.
• We could change the GPA calculator so until grades have been entered the getGPA button is not enabled.
• We could also disable the getGPA button after GPA is calculated, until a new grade is entered.
![Page 60: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/60.jpg)
GPA button.enabled set to false
![Page 61: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/61.jpg)
After a legal grade is entered…
![Page 62: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/62.jpg)
The code to do it
• In formload or in class initializations add the code:
btnGPA.Enabled =false
• In the if statement in buttonAddGrade event handler, when a legal grade is accepted, put the code:
btnGPA.Enabled =true
![Page 63: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/63.jpg)
Focus
• You can change focus by having the control who should get focus next execute a
Control.focus()• In the GPA project, when the form is
loaded, and after any button is pressed, we could send the focus back to the letter grade textbox with the code
• txtLetter.focus()
![Page 64: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/64.jpg)
After getGPA button is pressed
![Page 65: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/65.jpg)
Security panel project
![Page 66: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/66.jpg)
features
• Uses select case…control structure
• Textbox readonly set to true and password character set to ‘*’ in properties.
![Page 67: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/67.jpg)
Select case expression
• Select case is equivalent to a multipart if else structure• It has the form
Select case expCase val1StmtsCase val2Stmts…Case else ‘optionalEnd select
• Here val1, val2 etc., are possible values of the expression exp, and the else case (optional) will handle all other cases.
![Page 68: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/68.jpg)
functionality
• Each digit button just adds a digit (String, as in “1”, or “2”, etc) to a string holding the current code.
• The clear button sets the code to “” and the # key checks the valeu of the code.
• The string code is cleared after checking, and when the form is loaded as well as in the clear button event.
• The pound button displays the access provided to the given password
![Page 69: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/69.jpg)
Btnpound event-handlerPrivate Sub btnpound_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnpound.Click Dim intcode As Integer Dim message As String intcode = Integer.Parse(code) code = "" txtcode.Text = code Select Case intcode Case 1234 message = "OK" Case 3456 message = "OK" Case 5555 To 6666 message = "limited" Case Else message = "restricted" End Select lststatus.Items.Add(message)
End Sub
![Page 70: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/70.jpg)
Substring function
• The substring function of strings takes one or two integer arguments.
• If you give one integer argument, it returns the substring of the original from that char position, to the end.
• If you give two arguments, it returns the substring that starts at the first position and is the length of the second.
• String subscripts start at 0, not 1.
![Page 71: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/71.jpg)
Example VB
![Page 72: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/72.jpg)
Substring button code
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim text As String Text = txtinput.Text lbl1.Text = "substring(0):" & text.Substring(0) lbl2.Text = "substring(2,4):" &
text.Substring(2, 4) lbl3.Text = "substring(5):" & text.Substring(5) End Sub
![Page 73: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/73.jpg)
Revisitng GPA calculator
![Page 74: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/74.jpg)
For GPA calculator
• You may also want to convert strings (that is, “ “ entities) into chars
• Two ways to do it:
• Convert.tochar(string)
• String.chars(position)
![Page 75: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/75.jpg)
An input tester for GPA calculator
![Page 76: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/76.jpg)
Illegal input
![Page 77: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/77.jpg)
Code in button Dim text As String Dim first, second As Char Text = txtinput.Text lbl1.Text = "substring(0,1):" & text.Substring(0, 1) lbl2.Text = "substring(1,1):" & text.Substring(1, 1) first = text.Chars(0) second = Convert.ToChar(text.Substring(1, 1)) If text.Length > 2 Then lbllength.Text = "too long" Else lbllength.Text = "length ok" End If If first = "A" Or first = "B" Or first = "C" Or first = "D" Or first = "E" Then lblfirst.Text = first & " is legal" Else lblfirst.Text = first & " not legal" End If If second = "+" Or second = "-" Then lblsecond.Text = second & "is ok" Else lblsecond.Text = second & "not legal" End If
![Page 78: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/78.jpg)
Quadratic equation form
![Page 79: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/79.jpg)
Numeric operations exercises
• declare x1, x2, a, b, and c to be type double
• get values for a, b, and c from (hypothetical) textboxes txta, txtb, and txtc.
• Compute x1 and x2, the roots of the quadratic equation: ax2+bx+c
• Recall: root= (-b+/-sqrt(b2-4ac))/2a• Complete a VB form to do this as an
exercise.
![Page 80: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/80.jpg)
Quadratic equation 1
![Page 81: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/81.jpg)
Quadratic equation 2
![Page 82: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/82.jpg)
An Aside on Text alignment for textboxes and labels
![Page 83: Control structures Part 1 if then control. Index of projects in this ppt Absolute value Simple payroll Letter grade assignment Dental billing GPA calculator](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d405503460f94a1af99/html5/thumbnails/83.jpg)
Select one to align text R/center/L and up/center/down