1 / 21

MS Visual Basic Applications

MS Visual Basic Applications. Walter Milner. Event-driven programming. Standard approach for GUIs Contrast with old character interfaces – program determines what happens In GUI, the user triggers what application does (mostly) Event examples are key press, mouse move, timer timeouts

mayda
Download Presentation

MS Visual Basic Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MS Visual Basic Applications Walter Milner

  2. Event-driven programming • Standard approach for GUIs • Contrast with old character interfaces – program determines what happens • In GUI, the user triggers what application does (mostly) • Event examples are key press, mouse move, timer timeouts • Correspond to native Windows Messages (next slide) • Event handler = a subroutine which will execute when that event happens

  3. Variables • Dim x as Integer • Dim x,y as Integer NO! • Is case sensitive (kind of) • Variable naming conventions • Microsoft Simonyi Hungarian Reddick • House rules • Assignment statement – x = 4 • Option Explicit YES! • Constants – Private Const MyInt As Integer = 5

  4. Comments, line continuation and hex • Comments start with an apostrophe • 'and run to the end of the line • A very long statement can use a_ to continue onto the next line • Hex constants written like &HFF0012

  5. Data types • Integer Long • Single Double • Currency • Byte unsigned 0 - 255 • String • Boolean • Date • Object • Variant NO!

  6. Data type conversion DIM x as integer x = Cint("10")

  7. Controls Private Sub CommandButton1_Click() Dim x As Integer Dim y As Integer Dim z As Integer x = TextBox1.Value y = TextBox2.Value z = x + y Label1.Caption = z End Sub Copy this Correct it Add other buttons

  8. VB Core II • Conditional statements • Exception handling • Loops • Arrays • Debugging

  9. if statements If c > 5 Then x = 1: y = 3 If c > 5 Then x = 1 y = 3 End If If c > 5 Then x = 1 y = 3 ElseIf c = 4 Then z = 7 Else x = 9 End If If c > 5 Then x = 1 y = 3 Else z = 7 End If

  10. select • Dim Number • Number = 8 ' Initialize variable. • Select Case Number ' Evaluate Number. • Case 1 To 5 ' Number between 1 and 5, inclusive. • x=4 • Case 6, 7, 8 ' Number between 6 and 8. • x=5 • Case 9 To 10 ' Number is 9 or 10. • x=6 • Case Else ' Other values. • x=7 • End Select

  11. Error and exception handling • exception = problem event at run-time • usually related to I/O • eg file not found, server connection lost, invalid user input • not a programming bug • VB calls exception errors • (Unlike Java) VB does not force exception handling – but should do

  12. Error handlers – example - invalid numbers Private Sub Command1_Click() Dim num1 As Integer Dim num2 As Integer Dim result As Integer On Error GoTo myErrorHandler num1 = Textbox1.Text num2 = Textbox2.Text result = num1 + num2 Label1.Caption = result Exit Sub myErrorHandler: If Err.Number = 13 Then MsgBox ("Please enter a valid number") Else MsgBox (Err.Description) End If Resume Next End Sub Exercise Try this out in the calculator program Then deal with divide by zero (11)

  13. For next loops Dim x as Integer, total As Integer total = 0 For x = 1 To 5 total = total + x Next Dim x as Integer, total As Integer total = 0 For x = 1 To 5 Step 2 total = total + x Next

  14. Debugging

  15. Exercises – Use the debugger to watch.. • a for next loop looping from 1 to 5 • outputting the multiples of 3 from 99 to 3 • adding up the odd numbers between 1 and 9

  16. Other loops Dim c As Integer c = 1 Do While c < 5 c = c + 1 Loop Dim c As Integer c = 1 Do Until c >4 c = c + 1 Loop Dim c As Integer c = 1 Do c = c + 1 Loop While c < 5 Dim c As Integer, x as integer c = 1 x=2 Do c = c + 1 Loop Until c>4 And x<>3

  17. Arrays (fixed size) Dim x(100) As Integer Dim i As Integer For i = 0 To 100 x(i) = 99 Next

  18. Exercise – use the debugger – check each stage • Fill an array with 25 random integers in the range 1 to 100 (Rnd() gives a random single between 0 and 1 ) • Find the total • Find the largest • Search the array for a value given in cell(1,1)

  19. Arrays (2 d) Dim x(1 To 3, 1 To 3) As Integer Dim i as integer, j As Integer For i = 1 To 3 For j = 1 To 3 x(i, j) = 99 Next Next

  20. Dynamic arrays Dim x() As Integer ReDim x(5) Dim i, j As Integer For i = 1 To 5 x(i) = 99 Next ReDim Preserve x(10) For i = 6 To 10 x(i) = 100 Next

  21. Exercise • Use the =RAND() function to fill a column of a sheet with 20 random numbers • Then set up a button and write VB code which will – • read those 20 numbers into an array • sort the array into increasing order (bubblesort) • display the numbers in the next column of the spreadsheet

More Related