ms visual basic applications
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/1.jpg)
MS Visual Basic Applications
Walter Milner
![Page 2: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/2.jpg)
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
![Page 3: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/3.jpg)
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
![Page 4: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/4.jpg)
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
![Page 5: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/5.jpg)
Data types
Integer Long Single Double Currency Byte unsigned 0 - 255 String Boolean Date Object Variant NO!
![Page 6: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/6.jpg)
Data type conversion
Conversionfunction
Converts an expression to
Cbool Boolean
Cbyte Byte
Ccur Currency
Cdate Date
CDbl Double
Cint Integer
CLng Long
CSng Single
CStr String
Cvar Variant
CVErr Error
DIM x as integer
x = Cint("10")
![Page 7: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/7.jpg)
ControlsPrivate Sub CommandButton1_Click()Dim x As IntegerDim y As IntegerDim z As Integerx = TextBox1.Valuey = TextBox2.Valuez = x + yLabel1.Caption = zEnd Sub
Copy this
Correct it
Add other buttons
![Page 8: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/8.jpg)
VB Core II
Conditional statements Exception handling Loops Arrays Debugging
![Page 9: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/9.jpg)
if statements
If c > 5 Then x = 1 y = 3End If
If c > 5 Then x = 1: y = 3
If c > 5 Then x = 1 y = 3Else z = 7End If
If c > 5 Then x = 1 y = 3ElseIf c = 4 Then z = 7Else x = 9End If
![Page 10: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/10.jpg)
select
Dim Number Number = 8 ' Initialize variable. Select Case Number ' Evaluate Number.
Case 1 To 5 ' Number between 1 and 5, inclusive. x=4Case 6, 7, 8 ' Number between 6 and 8. x=5Case 9 To 10 ' Number is 9 or 10. x=6Case Else ' Other values. x=7
End Select
![Page 11: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/11.jpg)
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
![Page 12: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/12.jpg)
Error handlers – example - invalid numbersPrivate Sub Command1_Click()Dim num1 As IntegerDim num2 As IntegerDim result As Integer
On Error GoTo myErrorHandlernum1 = Textbox1.Textnum2 = Textbox2.Textresult = num1 + num2Label1.Caption = resultExit Sub
myErrorHandler:If Err.Number = 13 Then MsgBox ("Please enter a valid number")Else MsgBox (Err.Description)End IfResume Next
End Sub
Exercise
Try this out in the calculator program
Then deal with divide by zero (11)
![Page 13: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/13.jpg)
For next loops
Dim x as Integer, total As Integertotal = 0For x = 1 To 5 total = total + xNext
Dim x as Integer, total As Integertotal = 0For x = 1 To 5 Step 2 total = total + xNext
![Page 14: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/14.jpg)
Debugging
![Page 15: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/15.jpg)
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
![Page 16: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/16.jpg)
Other loopsDim c As Integerc = 1Do While c < 5 c = c + 1Loop
Dim c As Integerc = 1Do Until c >4 c = c + 1Loop
Dim c As Integerc = 1Do c = c + 1Loop While c < 5
Dim c As Integer, x as integerc = 1x=2Do c = c + 1Loop Until c>4 And x<>3
![Page 17: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/17.jpg)
Arrays (fixed size)
Dim x(100) As IntegerDim i As IntegerFor i = 0 To 100 x(i) = 99Next
![Page 18: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/18.jpg)
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)
![Page 19: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/19.jpg)
Arrays (2 d)
Dim x(1 To 3, 1 To 3) As IntegerDim i as integer, j As IntegerFor i = 1 To 3 For j = 1 To 3 x(i, j) = 99 NextNext
![Page 20: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/20.jpg)
Dynamic arrays
Dim x() As IntegerReDim x(5)
Dim i, j As IntegerFor i = 1 To 5 x(i) = 99Next
ReDim Preserve x(10)For i = 6 To 10 x(i) = 100Next
![Page 21: MS Visual Basic Applications](https://reader033.vdocuments.site/reader033/viewer/2022061617/56815da8550346895dcbd539/html5/thumbnails/21.jpg)
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