Title: CSC 162
1CSC 162
- Visual Basic I Programming
2ListBox Object
- Unlike a TextBox, a ListBox can hold multiple
items. - Each item gets its own line.
- Once the ListBox is full, it will automatically
provide scroll bars to view all the contents. - A ListBox cannot be edited directly by the user.
- A ListBoxs naming convention prefix is lst.
- To add an item to a ListBox, use the .AddItem
method - lstNames.AddItem(strLastName ", "
strFirstName) - Use the ListBox for outputting the Pythagorean
Triples, since using Print runs the output off
the form.
3Procedures
- Event procedures
- respond to an event (mouse click, button press,
etc.) - Visual Basic procedures
- built-in procedures that perform common tasks
(MsgBox, InputBox, etc.) - Sub procedures
- programmer-defined procedures that perform tasks
- Function procedures
- programmer-defined procedures that perform
calculations and return values through the
Functions name
4Programmer-defined Procedures
- May have a private or public scope.
- Scope refers to the region in which the
identifier can be referenced - An identifier with private scope may only be
referenced from within the current module - An identifier with public scope may be referenced
from anywhere within the current project
5ByVal or ByRef
- Procedures can be passed values (called
parameters) from the calling object. - The parameter can either be passed by value
(ByVal) or by reference (ByRef). - ByVal makes a copy of the value being passed,
thus preserving the original value in the calling
objects variable - ByRef directly access the data in the calling
objects variable, thus allowing it to be modified
6Sub Procedure Example (ByVal)
- Private Sub PrintSquare(ByVal intNum As Integer)
- intNum intNum 2
- Print intNum
- End Sub
- Private Sub cmdPrint_Click()
- ' Local variables
- Dim intValue As Integer
- ' Get User Data
- intValue Val(txtNumber.Text)
- ' Print Initial Value
- Print intValue
- ' Call to Sub Procedure
- Call PrintSquare(intValue)
- ' Print Final Value
Output (Given 5 in txtNumber) 5 25 5
7Sub Procedure Example (ByRef)
- Private Sub PrintSquare(ByRef intNum As Integer)
- intNum intNum 2
- Print intNum
- End Sub
- Private Sub cmdPrint_Click()
- ' Local variables
- Dim intValue As Integer
- ' Get User Data
- intValue Val(txtNumber.Text)
- ' Print Initial Value
- Print intValue
- ' Call to Sub Procedure
- Call PrintSquare(intValue)
- ' Print Final Value
Output (Given 5 in txtNumber) 5 25 25
8Function Procedure Example
- Private Function Area(ByVal intLength As Integer,
ByVal intWidth As Integer) As Integer - ' Local variables
- Dim intA As Integer ' Local variable for the
Area - intA intLength intWidth
- Area intA ' Return final value
- End Function
- Private Sub cmdFindArea_Click()
- ' Local variables
- Dim intL As Integer ' User's entered length
- Dim intW As Integer ' User's entered width
- ' Get User Data
- intL Val(txtLength.Text)
- intW Val(txtWidthr.Text)
- ' Print Area
9Homework
Lab Assignment
Read in two legs of a right triangle from the
user in TextBoxes. Compute the hypotenuse using
the Pythagorean Theorem. Return the value in a
Label. To compute the hypotenuse, create a
function called Pythag that takes two Singles as
input and returns its answer as a Single.