Title: Distance Learning Center
1Distance Learning Center
- Lecture 9
- Introduction to Visual Basic
- Programming
- Melissa Lin, IT Consultant
- HenEm, Inc. Parkville, Missouri
- linm_at_ipfw.edu
- http//www.etcs.ipfw.edu/linm
2Lecture 9
- Input Validation
- Calling procedures
- Hands-On Example
- Debugging
3Input Validation
- The validation is to verify that appropriate
values have been entered before beginning
calculations, including checking for numeric
number, specific values, a range of values
(reasonableness), or for a required field (not
blank) - Performing Multiple Validations
- Using Try/Catch blocks
- Use nested If statement to validate multiple
values on a form (Examine example on Page 156) - Using Nested IF statement for performing multiple
validation - Using Case structure to validate multiple values
4Input Validation
- Example
- If Integer.Parse(HoursTextBox.Text) gt 10 - to
check a range of the input - If nameTextBox.Text ltgt Then to check empty
field - If IsNumberic (expression) to check if the data
is numeric - If IsNumeric(inputTextBox.Text) True Then
- MessageBox.Show("Good Job of Entering
Numerical Input") - Else
- MessageBox.Show ("Whoops, try again")
- End If
5Sharing Event
- Add events to the Handles clause at the top of an
event procedure - Allows the procedure to respond to events of
other controls. - Key to using a shared event procedure is the
sender argumentthat is passed to the CheckChanged
event procedure, such as radio button. - Cast (convert) sender to a specific object type
using the CType function.
6Calling Event
- You can call another subroutine from within a
subroutine - When you call an event procedure, the entire
procedure is executed. - General Form
- Call ProcedureName ( )
- Keyword Call is optional and rarely used
- Example
- Private Sub calculateButton_Click()
- If sevenInteger ltgt 7 Then
- Call exitButton_Click() call the exit event
- EndIf
- Continue with calculations
7Calling Event (continued)
- Call newOrderButton_Click(sender, e)
- OR
- newOrderButton_Click(sender, e)
- Example
- Private Sub summaryButton_Click(ByVal
- sender As System.Object, ByVal e As
System.EventArgs) Handles summaryButton.Click - newOrderButton_Click(sender, e)
- Continue with calculations
8Hands-On Programming
- Coffee Sales project - to accumulate individual
items for one customer. Clearing the entire order
for the next customer when the customer s order
is completed, and having a summary - Adding a Takeout Check Box
- Adding Coffee Selection Radio Buttons
- Five buttons Calculate, Clear, New Order,
Summary, and Exit - The steps to write the project
- Define the GUI
- Set the properties of each object as you have
planned - Write the code working from the pseudocode, and
each event procedure - When completed, to ensure the tab order is set
correctly - Save the form
- Save the project
- Run the program
9Planning GUI Design
Coffee Selection
GroupBox1
Quantity
Cappuccino
Takeout
Espresso
latteRadioButton
Calculate
Calculate Selection
Clear for Next Item
Latte
Iced Latte
Iced Cappuccino
GroupBox2
Item Amount
subTotalLabel
Sub Total
Tax (If Takeout)
Total Due
newOrderButton
exitButton
New Order
Exit
Summary
summaryButton
10Place Control on the Form
- Select True at
- Checked for
- Cappuccino
11Plan the Event Procedures
- Procedure Actions
- calculateButton_Click Validate for blank or
nonnumeric amount - Find price of drink selection
- Multiply price by quantity
- Add amount to subtotal
- Calculate tax if needed
- Calculate total subtotal tax
- Formal and display the values
- Disable the Takeout check box
- Enable the Clear button
- clearButton_Click Clear the coffee selection
- Clear the quantity and the item price
- Set the focus to the quantity
- Disable the Clear button
12Plan the Event Procedures (continue)
Procedure Actions summaryButton_Click If current
order not added to totals call
newOrderButton_Click Calculate the
average Display the summary totals in a message
box newOrderButton_Click Confirm clearing current
order Clear the current order Accumulate
total sales and count Set subtotal and total due
to 0 Enable Takeout check box Disable the
Clear button exitButton_Click Exit/Terminate the
project
13Write Code Module Level Declaration
Module Level
14Write Code Local Level Declaration
Local Level
NestIf
15Write Code Try/Catch
Try / Catch
Enable
16Write Code MessageBox
messageString "Number of Orders _
customerCountInteger.ToString() _
ControlChars.NewLine ControlChars.NewLine _
"Total Sales "
grandTotalDecimal.ToString("C") _
ControlChars.NewLine ControlChars.NewLine _
"Average Sale " averageDecimal.ToString("C) M
essageBox.Show(messageString, "Coffee Sales
Summary",_ MessageBoxButtons.OK,
MessageBoxIcon.Information)
17Output
Click Calculate
18Output (continued)
After click NewOrder
Click on Clear for Next Item
Click Summary
19Output (continued)
After click Yes
20Debugging
Step Into
Step Out
Step Over
Break All
Continue
21Debugging (continue)
22Debugging - Example
Tools -gt Options -gt debugging -gt General -gt In
break
23Debugging Example (continue)
24Debugging (continue)
- To start debugging / execution
- From the Debug menu, choose Start, Start without
debugging, Step Into, or Step Over. -or- - If you choose Start, your application starts up
and runs until it reaches a breakpoint or an
exception occurs. - If you choose to break execution to examine
values, modify variables, or examine statements
at your program. - Place Debug.WriteLine method in your code to
display the information in the output window.
25Debugging (continue)
- Select Help menu, then select Index
Ex. Type Step Into at Look For Click on Step
Into Command
26Stepping through Code
- The best way to debug a project is to use debug
stepping tools to trace program execution line by
line and see the progression of the program as it
executes through the code. - Step Into (F11) when the line of code is
executed each time, the program pauses in break
time. when the line of code calls to other
procedures, the first line of code of the other
procedure displays. - Step Over (F10) when the line of code calls to
other procedures, Step Over displays only the
lines of code in the current procedure being
analyzed. - Note the stepping function only works when
VB.NET is in the Break mode.
27Stepping through Code (continued)
- Step Out when you step through a called
procedure, the Step Out command continues rapidly
execution until the called procedure completes,
and returns to break mode at the statement
following the call. - Continuing Program Execution (F5) to continue
rapid execution and resume the form. Or choose
the Restart command to restart execution. - Stopping Execution by selecting Stop Debugging
to correct the errors and run again
28Hands-On Debugging
Open the Ch04Debug Folder Run it
Enter Quantity 100 with Blue, then click on
Calculate, The Total Blue is 100
Enter Quantity 50 with Blue, then click on
Calculate The Total Blue is 150
29Hands-On Debugging (continue)
Enter Quantity 10 with Red, t hen click on
Calculate The Total Red is 10
Enter Quantity 30 with Red, then click on
Calculate
30Debugging - Set up Break Point Writing to the
Output Window
View the values of properties, variables,
mathematical expressions, and conditions
Click in the gray margin to set a break point at
quantityDecimal
31Debugging (continue)
quantityTextBox 30D
32Writing to the Output Window
Syntax Debug.WriteLine(TextString) Debug.WriteLi
ne(Object) Example If redRadioButton.Checked
Then Debug.WriteLine("Perform
redTotalDecimal") Output Perform redTotalDecimal
33Locals Auto Windows
- Locals Window
- Appears while program is running
- Tab at bottom of VB.NET window
- Autos Window
- Appears while program is running
- Tab at bottom of VB.NET window
- displays variables used in the current statement
and the previous statement
Autos Window
34Debugging Step by Step
redTotalDecimal 30D quantityDecimal 30D
35An Step by Step Example
36An Step by Step Example
37Correction
redTotalDecimal quantityDecimal
38Re-Run the Program
- Set Option Explicit On and Option Strict On
- Using step into to find errors
- Correct it and Clear all Breakpoints before
re-running the project
39Output
Before click on Calculate button
After click on Calculate button
40Summary
- Input Validation
- Calling procedures
- Hands-On Example
- Debugging
- Next chapter 5
41Question?