Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic I Programming

Similar presentations


Presentation on theme: "Visual Basic I Programming"— Presentation transcript:

1 Visual Basic I Programming
CSC 162 Visual Basic I Programming

2 ListBox 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 ListBox’s 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.

3 Procedures Event procedures Visual Basic procedures Sub 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 Function’s name

4 Programmer-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

5 ByVal 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 object’s variable ByRef directly access the data in the calling object’s variable, thus allowing it to be modified

6 Sub 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

7 Sub 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

8 Function 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() 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 Print Area(intL, intW) End Sub

9 Homework Lab Assignment Read Chapter 6.1 – 6.8
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.


Download ppt "Visual Basic I Programming"

Similar presentations


Ads by Google