cs 2340: programming in vb lab 2 due 9 pm, friday, september 14 1

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

Upload: neal-reed

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

CS 2340: Programming in VB

Lab 2

Due 9 pm, Friday, September 14

1

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

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

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

Form Properties

• Text (title) • FormBorderStyle • WindowsState: Normal, Minimized, Maximized • StartPosition • ControlBox: Control buttons

Minimize, Maximize (restore), Close• MinimizeBox, MaximizeBox

3

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

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

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

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

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

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

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

Message Box

Method Show

Overloaded

Help

Manage Help Settings

View Help

7

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

Message Box

MessageBoxIcon

•Asterisk

•Error

•Exclamation

•Hand

•Information

•None

•Question

•Stop

•Warning

8

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

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

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

GUI Programs

Three Steps

Input (from controls)

Process (no controls)

Output (to controls)

10

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

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

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

Lab 2: Checking Rateinput = 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 IfElse MessageBox.Show("Invalid Rate!" + Chr(Keys.LineFeed) & _ "Rate must be a number!", "Lab 2", _ MessageBoxButtons.OK, MessageBoxIcon.Warning) txtRate.Focus() Exit SubEnd If

12

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

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

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

Lab 2: Testing ID

• Empty ID

Error message

• Spaces only

Error message

• String “CS 2340”

Good ID

• String “ CS 2340 ”

Good ID

14

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

Positive and Negative

• Zero is a positive number.

True False

• Zero is a negative number.

True False

• Zero is a non-positive number.

True False

• Zero is a non-negative number.

True False

15

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

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

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

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

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

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

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

Schedule

Lab 2: Due 9pm, Friday, September 14

No Grace Time!

Test 1: Friday, September 21

Based on Lab1 and Lab2

19

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

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