200 likes | 303 Views
CS 2340: Programming in VB. Lab 2 Due 9 pm, Friday, September 27. Button Access Key (Hot Key). Put a “&” in button text The char after “&” is underlined and becomes the Access Key Example Text of btnExit: E X IT (E&XIT) [ALT] + X ==> the same as click btnExit . Form Properties.
E N D
CS 2340: Programming in VB Lab 2 Due 9 pm, Friday, September 27
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
Form Properties • Text (title) • FormBorderStyle • WindowsState: Normal, Minimized, Maximized • StartPosition • ControlBox: Control buttons • Minimize, Maximize (restore), Close • MinimizeBox, MaximizeBox
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
Invoking Event Procedures Private Sub btnExit_Click(...) HandlesbtnExit.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(...) HandlesbtnExit.Click
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)
Message Box Method Show Overloaded Help Manage Help Settings View Help
Message Box MessageBoxIcon • Asterisk • Error • Exclamation • Hand • Information • None • Question • Stop • Warning
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)
GUI Programs Three Steps Input (from controls) Process (no controls) Output (to controls)
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
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
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!
Lab 2: Testing ID • Empty ID Error message • Spaces only Error message • String “CS 2340” Good ID • String “ CS 2340 ” Good ID
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
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!
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!
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?
Schedule Lab 2: Due 9pm, Friday, September 27 No Grace Time! Test 1: Friday, September 27 Based on Lab1 and Lab2
Test 1 • Friday, September 27 • 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