Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2340: Programming in VB Lab 2 Due 9 pm, Friday, September 14 1.

Similar presentations


Presentation on theme: "CS 2340: Programming in VB Lab 2 Due 9 pm, Friday, September 14 1."— Presentation transcript:

1 CS 2340: Programming in VB Lab 2 Due 9 pm, Friday, September 14 1

2 Button Access Key (Hot Key) Put a “&” in button text The char after “&” is underlined and becomes the Access Key Example Text of btnExit: EXIT (E&XIT) [ALT] + X ==> the same as click btnExit 2

3 Form Properties Text (title) FormBorderStyle WindowsState: Normal, Minimized, Maximized StartPosition ControlBox: Control buttons Minimize, Maximize (restore), Close MinimizeBox, MaximizeBox 3

4 Form Properties Button Click Event Pressing ENTER when a button has the focus Multiple buttons on a form AcceptButton: Pressing ENTER when no button has the focus CancelButton Pressing ESC when no button has the focus Different ways to invoke the button click event 4

5 Invoking Event Procedures Private Sub btnExit_Click(...) Handles btnExit.Click End End Sub ' It will not work if Handles is removed. Private Sub btnExit_Click(...) End End Sub ‘ You could change the Sub’s name Private Sub SubExit(...) Handles btnExit.Click 5

6 Function Format and FormatCurrency txtSum.Text = Format(result, "Currency") txtSum.Text = Format(result, "C") txtSum.Text = Format(result, "General Number") txtSum.Text = Format(result, “n") txtSum.Text = Format(result, "Percent") txtSum.Text = Format(result, "P") txtSum.Text = Format(result, "Standard") txtGrossPay.Text = FormatCurrency(gross) 6

7 Message Box Method Show Overloaded Help Manage Help Settings View Help 7

8 Message Box MessageBoxIcon Asterisk Error Exclamation Hand Information None Question Stop Warning 8

9 String on Multiple Lines ' Special char vbCrLf MessageBox.Show("Invalid Hours!" + vbCrLf + _ "Hours must be non-negative!", _ "Lab 2", MessageBoxButtons.OK, MessageBoxIcon.Error) ' Special integer Keys.LineFeed ' Run time error MessageBox.Show("Invalid Hours!" + Keys.LineFeed + _ "Hours must be non-negative!", _ "Lab 2", MessageBoxButtons.OK, MessageBoxIcon.Error) ' Special integer Keys.LineFeed ' Cast function Chr MessageBox.Show("Invalid Hours!" + Chr(Keys.LineFeed) + _ "Hours must be non-negative!", _ "Lab 2", MessageBoxButtons.OK, MessageBoxIcon.Error) 9

10 GUI Programs Three Steps Input (from controls) Process (no controls) Output (to controls) 10

11 Click Event Procedure for btnCompute ‘ Declare variables Dim rate, hours, grossPay, deduction, netPay As Double Dim input As String ‘ Check ID input = txtID.Text.Trim()... ‘ Check Rate input = txtRate.Text.Trim()... ‘ Check Hours input = txtRate.Text.Trim()... ‘ Process ‘ No more controls! ' Display results using controls 11

12 Lab 2: Checking Rate input = txtRate.Text.Trim() If IsNumeric(input) And _ InStr(input, "e", CompareMethod.Text) = 0 Then rate = Convert.ToDouble(input) If rate <= 0 Then MessageBox.Show("Invalid Rate!" + vbCrLf & _ "Rate must be positive!", "Lab 2", _ MessageBoxButtons.OK, MessageBoxIcon.Warning) txtRate.Focus() Exit Sub End If Else MessageBox.Show("Invalid Rate!" + Chr(Keys.LineFeed) & _ "Rate must be a number!", "Lab 2", _ MessageBoxButtons.OK, MessageBoxIcon.Warning) txtRate.Focus() Exit Sub End If 12

13 Testing GUI Programs COURSE OUTCOMES... Design, develop and test Visual Basic programs. Users can enter whatever they want and in what order they like. We must make sure the program won’t crash and always work correctly! 13

14 Lab 2: Testing ID Empty ID Error message Spaces only Error message String “CS 2340” Good ID String “ CS 2340 ” Good ID 14

15 Positive and Negative Zero is a positive number. TrueFalse Zero is a negative number. TrueFalse Zero is a non-positive number. TrueFalse Zero is a non-negative number. TrueFalse 15

16 Lab 2: Testing Rate (positive) Empty rate Error message Not a number such as “cs2340” or 234cs Error message Scientific notation “2e3” Error message Scientific notation “2E-3” Error message Number 0 Error message Negative number such as -1 Error message Positive number 10.45 Good Rate! 16

17 Lab 2: Testing Hours (non-negative) Similar to testing rate Negative number such as -1 Error message Positive number 10.45 Good Rate! Number 0 Good Hours! Hours can be zero! 17

18 Lab 2: Testing Gross Pay Rate: 10 Hours 20 Gross Pay: 200 Rate: 10 Hours 40 Gross Pay: 400 Rate: 10 Hours 50 Gross Pay: 500? Gross Pay: 550? 18

19 Schedule Lab 2: Due 9pm, Friday, September 14 No Grace Time! Test 1: Friday, September 21 Based on Lab1 and Lab2 19

20 Test 1 Friday, September 21 Lab 206 Create a VB.NET program within 52 minutes 20 points Open book and notes Online help available Your labs available Do it yourself No email No discussion No credit if no executable file or it does not run You can ask me for help, but you may lose 3 points each time you receive help You cannot ask me any questions during the last 15 minutes of the test! -3 for each run time error 20


Download ppt "CS 2340: Programming in VB Lab 2 Due 9 pm, Friday, September 14 1."

Similar presentations


Ads by Google