0% found this document useful (0 votes)
57 views113 pages

Intro To Programming

Visual Basic (VB) is a programming language used to create application programs for Windows. VB uses an integrated development environment (IDE) that contains tools like the toolbox, designer, and properties window to help design the user interface and write code. The IDE allows adding controls like buttons, textboxes, and labels to a form. Controls have properties that can be set, like name, size, and color. Code is added using event procedures tied to controls to make the application interactive.

Uploaded by

Loojee Bayles
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
57 views113 pages

Intro To Programming

Visual Basic (VB) is a programming language used to create application programs for Windows. VB uses an integrated development environment (IDE) that contains tools like the toolbox, designer, and properties window to help design the user interface and write code. The IDE allows adding controls like buttons, textboxes, and labels to a form. Controls have properties that can be set, like name, size, and color. Code is added using event procedures tied to controls to make the application interactive.

Uploaded by

Loojee Bayles
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 113

VB 1

Intro to Computer Programming


Programming Languages
• Computer programs that allow users to create
programs using a restricted vocabulary and a
structured syntax that the computer can
understand.
Visual Basic 2015
• Computer language developed to help you
create application programs that work with
the Windows OS.
• Features:
– Event-driven language - does not follow a
predefined sequence of instructions depending
on which event occurs.

– Object-oriented (OO) language -uses software


objects that can respond to events. Objects
combine programming code with data
What do I need to Run VB 2015?
• PC running Windows Vista,
7, 8 or Windows 10

• Microsoft Visual Basic 2015


Visual Basic
Integrated Development Environment
(IDE)
Integrated Development Environment
Integrated Development Environment

New
Project
dialog box
Designing and Coding IDE
Menu bar Shortcut icons Start debugging button

Form
(design) tab

Design
Solution
Surface Explorer

Default form
Toolbox Properties
window

Property
details

Component tray
Most Common Windows
• Toolbox - contains reusable controls and components you
can add to your application.

• Design window - place where you draw your user interface


on your forms, sometimes referred to as the Designer.

• Solution Explorer - contains a hierarchical view of your


solution, contains many projects, whereas a project contains
forms, classes, modules, and components that solve a
particular problem.

• Properties - properties the selected object makes available.


Though you can set these properties in your code, sometimes
it is easier to set them while you are designing your application
Solution Explorer Properties
Controls / Objects
• Refers to visible and non-visible objects such
as buttons, textboxes, and labels that you can
add in the Form.
• Are kept in the Toolbox.
Default Common Controls
• Pointer - Selects another control; moves control
around screen.
• Button - Responds to events, typically a mouse
click
• CheckBox - Responds to being checked
• CheckedListBox - Displays list of text items with
checks beside selected items
• Textbox - Allows user to input data
• ComboBox - Acts a drop-down list box

• Label - Displays text (read only; no input)


Default Common Controls (cont)
• ListBox - Displays list of text items

• PictureBox - Displays an image; responds to


events
• RadioButton - Responds to being on or off

• Numeric UpDownCounter

• MenuStrip - Creates menus in application

• GroupBox - Acts a container for other controls

• Panel - Acts as container for other controls

• WebBrowser – Connects to the internet browser


Other Controls/Objects
Properties
• Properties – refers to distinct set of attributes
of a control. It defines the characteristics of an
object.

Note: Press F4 to display


the Properties window.
Setting the Properties
• Click the object to view
object Properties
window.
• Scroll down to locate
the property that you
want to change.
• Modify the property
and press the Enter
key to accept the
change.
Saving a Project
• On the File menu, select Save All.

• Specify the Name and Location where you want


the project to be saved.

Always save your project in drive Z:\


Click Browse button to locate drive Z:
Designing the User Interface
The FORM Object

Used to build the interface for


your application
Properties of Form Object

Note: Arrange
properties in
ascending order.
Click A-Z icon
Properties of Form Object
• Changing the Name of the Form
– Change Name property to
ViewerForm . Notice that the
Solution Explorer window still
displays Form1.vb

– Right-click Form1.vb in the


Solution Explorer window.

– Choose Rename from the context


menu and change the text from
Form1.vb to ViewerForm.vb .
Properties of Form Object
• Changing the text in the Form’s
Title bar
– By the fault, it displays Form1.

– Change the TEXT property of the


ViewerForm (or Form) to Machine
Problem.
Properties of Form Object
• Changing the BackColor of the
Form
– Click on Backcolor property of the
Form

– Choose the color of the


background to Yellow or Blue.
Properties of Form Object
• Displaying an Image as
Background in the Form
– Click on BackgroundImage
property of the Form.

– Click on the ellipse (3 dots button)


and choose Local Resource->
Import.

– Choose any picture as your


background image.

– Click on BackgroundImageLayout
property and choose Stretch
Properties of Form Object
• Window State
– Normal
– Maximize
– Minimize

• Start Position
– Windows Default
– Center Screen

• Minimize Box / Maximize Box


– True / False
Adding a textbox to the form
• Locate the TextBox tool
• Two (2) ways
– Double click the icon
– Draw the textbox on the form
• A textbox is added to your form
Adding a Label to your Form
• Locate the label control in the toolbox
• Two (2) ways
– Double click the label icon
– Draw the label box on the form
• A new label is added to your form
Design the following User Interface
VB Coding
• Program Code
– refers to set of instructions written by the programmer
that directs the program to carry out the processing
required in the program.

• Event Handler
– handles the event that the user action triggers by
executing the code that performs the required
processing.
Example: for Button object
- Click - MouseDown
- Double Click - Move
Double click the button and add
the ff. codes:
Private Sub Button1_Click()
Dim A, B As Integer
A = TextBox1.Text
B = TextBox2.Text

TextBox3.Text = A + B
End Sub
Revise the User Interface
Include appropriate codes for
subtraction (-), multiplication
(*) and Division (/).

Private Sub Button2_Click()


Dim A, B As Single
A = TextBox1.Text
B = TextBox2.Text

TextBox3.Text = A - B
End Sub
Private Sub Button3_Click() Code Solution
Dim A, B As Single
A = TextBox1.Text
B = TextBox2.Text
TextBox3.Text = A * B
End Sub

Private Sub Button4_Click()


Dim A, B As Single
A = TextBox1.Text
B = TextBox2.Text
TextBox3.Text = A / B
End Sub
Help and Correcting Error
• IntelliSense
– In coding your program, VB provides real-time help so
that you will be less prone to make error.

• Build Errors Message


– VB compiler detects a coding error in the program.
Running/executing your program
• Click Debug->Start from the menu bar. Or
press F5 on your keyboard … or
Exercise #1
• Design the screenshot
as shown.
• Include codes for:
– Standard Room
– Deluxe Room
– Exit Window

PictureBox
(initially empty)

Buttons
Include the following Codes:
Private Sub Button1_Click( )
PictureBox1.Image=Image.FromFile(“x:\icons\StandardRoom.jpeg")
End Sub

Private Sub Button2_Click( )


PictureBox1.Image = Image.FromFile(“x:\icons\DeluxeRoom.jpg")
End Sub

Private Sub Button3_Click( )


Me.Close()
End Sub
Sample User Interface
TextBox Numeric UpDown Counter

Label
PictureBox
ComboBox

DateTime Picker
GroupBox
ListBox
RadioButton

Button
VB Programming
Elements
Variable
• Refers to a memory cell (storage area) in the
computer’s memory that is used to store data.
• Numeric data types
– Data that are classified as numbers
– Available data types: Single, Double, Decimal, Integer,
Long, Date, Boolean, among others

• String data type


– Data that consist of letters and other characters
– Available data types: String, Char
Numeric Data Types
Numeric Number
Data Prefix Range of
Type Bytes
Single Sng 1.4E-45 to 3.4E38 and 4
-3.4E38 to -1.4E-45

Double Dbl -1.79769313486232e308 to 8


+ 1.79769313486232e308

Decimal Dec Have up to 28 16


significant digits
Numeric Data Types
Numeric Number
Data Type Prefix Range of
Bytes

Integer Int -2,147,483,648 to 4


2,147,483,647
Long Lng -9,223,372,036,854,775,808 8
to
9,223,372,036,854,775,807
Short Sht -32,768 to 32,767 2

Date Dtm Between Jan. 1, 0001 and 8


December 31, 9999

Boolean Bln True or False 2


Other data types
• Byte
• Object
• Sbyte
• UInteger
• Ulong
• Ushort
Strings and Characters
• String data type
– holds multiple characters. Default value is Nothing
(a null reference). Note: Null is not the same as the
empty string (value: "").

Example:
Dim st As String
st = “The quick brown fox”
Strings and Characters
• Char data type
– holds a single character. Default value is the
character with a code point of 0. Append the literal
type character to a single-character string literal to
identify it as the Char data type.

Option Strict On
Dim chtr As Char
' The command attempts to convert a string to Char.
'Because Option Strict is On, it generates a compiler error.
chtr = “A"
' This statement succeeds for it specifies a Char literal.
chtr = “A”c
Variable
• How to name a variable?
– Combination of the letters A through Z, the digits 0
through 9, and the underscore(_)
– Space and other special characters are NOT
allowed.
– Always start with a letter.
– Not case-sensitive
Variable
• Should not be restricted keywords in VB.
• Keywords are words that VB uses as a part of
its language.
– Example:
SUB - keywords are displayed in BLUE
and errors in wavy underline.
Private, Public, Class, End, For, Next, If, Else,
Dim, As, To, Is, Then, ElseIf, Do, Loop, While,
Until, Integer, Single, Double, Long, Short,
among others
Declaring a variable:
• Format: DIM <variable name> As <datatype>
• Examples:
1. Declare a variable called MyName as a String
type.

2. Declare IncomeTax variable as Decimal.

3. Declare the two variables in just one variable


declaration.
Declaring a variable (cont)
• Examples:
4. Declare Score1 and Score2 as Short
5. Declare RawScore as Long initialized to
zero.
6. Declare MyBday as a Date type.
Assignment Statement
• Consists of a variable name, followed by the
assignment operator (=), and followed by
some sort of expression.
• Example:
Dim ST As String
Dim X As Integer, S As Single, D As Double
ST = “Apple”
X=5
S = 1.25
D = 3.14159867
Assignment Examples
AmountDue = Price + Tax
TextBox1.text = "Welcome to VB programming"
Confirmed = TRUE
Energy = Mass * LIGHTSPEED ^ 2
NetWorth = Assets – Liabilities
MyDate = #10/8/2008#
Comment statement
• Form of internal documentation that is used to
explain part of a program.
• Begins with the keyword Rem or a single
quote ( ' ).
• Comments are displayed in GREEN on the
screen to distinguish them from executable
codes.
Comment: Example
Rem This is a remark

' This is also a remark

x=2*y ‘Other way to write a remark or comment


VB Built-in Functions
• Today() – returns the system date on your computer.
• Now() –returns the system date and time.
Example: DteCurDeyt = Today()
Deyt = Now()

• CInt() – converts a string to an integer.


Example: X = CInt(Textbox1.text)
Others: CSng(), CBool(), CDec(), CDbl(), CSht()

• CStr() – converts the numeric value into a string.


Example: Textbox1.text = CStr(Sum)
Dot Notation are Used for:
• Control Property
Syntax: <Objectname>.<property>

• Control Method
Syntax: <Objectname>.<method>
Control Property
• Used in an assignment statement
Example:
X = Textbox1.Text
Textbox5.Text = “Juan dela Cruz”
CheckBox1.Checked = True
TextBox1.Enabled = False

VName = Label1.Text
VTick = RadioButton.Checked
Properties of Label Object
Properties of Textbox Object
Properties of Button Object
Properties of Checkbox & RadioButton
Control Method
• Defines the actions that a control can carry
out.
– Examples:
Textbox1.Focus()
‘-> places the cursor position to the TextBox

Textbox1.Clear()
‘-> clears the content of the TextBox.
VB Operators
Visual Basic Operators
• Symbols representing operations that can be
performed on constants and variables.

• Basic operations in VB
– Arithmetic Operation
– Relational Operation
– Logical Operation
Arithmetic operators
• Used to carry out arithmetic calculations.
• Order of precedence:
Operator Operation Example
() Grouping C = (A + B ) / (C + D)

^ Exponentiation C = 10 ^2
- Negation C = -A
* Multiplication C=A*B
/ Division C=A/B
\ Integer Division C = 25 \ 7
Mod Modulus (remainder of division) C = 25 mod 7

+ Addition C=A+B
- Subtraction C=A-B
Examples:
• TakeHome = Salary – Deductions

• Interest = Principal * InterestRate

• Area = 3.14157 * Radius ^2


• RoundUp = (Big \ Tiny + Big) Mod Tiny

• BinVal = Deval mod 2

• Val = Val \ 2
Exercises
Determine the values for the following integer
expressions:
1. 3+4*6 6. 20 – 2 / ( 6 + 3)
2. 3*4/6+6 7. (20 – 2) / 6 + 3
3. 2 * 3 /12 * 8 / 4 8. (20 – 2) \ ( 6 + 3)
4. 10 * (1 + 7 * 3) 9. 50 \ 20
5. 20 – 2 / 6 + 3 10. (10 + 3) Mod 4
Relational operators:
• Operator Comparison
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
Exercises:
• What is the result of the following relational
operations?
1. ( 10 > 5 )
2. ( 123 = 321 )
3. ( 1 + 2 + 3 > = 3 + 2 + 1 )
4. ( 5.0 < = 10.0 / 2.0 )
5. ( 3 + 5 * 10 <> 3 * 5 + 10 )
6. ( 3^2 = 6 )
7. ( ‘y’ = ‘Y’ )
Logical operators
• Operator Operation
Not Logical Not
And Logical And
Or Logical Or
Exercises
• Write the relational expressions for the following
statements:
1. Variable G does not exceed 20
2. Variable Ave is more than 80
3. Variable Speed is from 50 to 75.

4. Variable Temp must be divisible by 3.

5. Variable Weight is no less than 60.5.


Exercises
• Write the relational expressions for the following
statements:
6. Variable Score is outside of 50 to 100.

7. Variable Quiz is at least 80.


8. Variable Num is neither 7 nor 10.

9. Variable X must not be 3, 7 or 15.

10. Variable Pay is below 2500.


Exercise #1:
• The total resistance of a Series Circuit is the
sum of all individual resistance values.
• Write a program that allows the user to input
for 2 resistor values using TextBoxes.
Compute and display the total resistance of a
circuit.
Solution:
Public Class Form1
Private Sub Button1_Click()
Dim R1, R2 As Single
R1 = TextBox1.Text
R2 = TextBox2.Text
TextBox3.Text = R1 + R2
End Sub
End Class
Exercise #2:
• Write a VB program that computes the total
current in a series circuit consisting of three
resistors and a voltage source. The
relationship for the total current is:
IT = VT / ( R1 + R2 + R3 )
where: IT = total current in amps.
VT = voltage source in volts
R1, R2, R3 = value of each resistor

• Allow the user to input for the values of the


three resistors and the voltage source.
Solution
Public Class Form1
Private Sub Button1_Click()
Dim R1, R2, R3, VT As Integer
R1 = TextBox1.Text
R2 = TextBox2.Text
R3 = TextBox3.Text
VT = TextBox4.Text

TextBox5.Text = VT / (R1 + R2 + R3)


End Sub
End Class
Exercise # 3:
• Revise Problem #2 to include computations for
voltage drop of each resistor.

Note:
VR1 = IT * R1
VR2 = IT * R2
VR3 = IT * R3

Note: VT = VR1 + VR2 + VR3


Public Class Form1
Private Sub Button1_Click()
Solution:
Dim R1, R2, R3, VT, IT As Single
R1 = TextBox1.Text
R2 = TextBox2.Text
R3 = TextBox3.Text
VT = TextBox4.Text

IT = VT / (R1 + R2 + R3)

TextBox5.Text = IT
Textbox6.Text = IT * R1
Textbox7.Text = IT * R2
Textbox8.Text = IT * R3
End Sub
End Class
Seatwork #1:
Given the circuit shown below, develop a VB program
that allows the user to input values for resistor (R),
inductor (L), frequency (f) and the voltage source (VS).
Thereafter, it will compute and display the voltage drop
across the resistor (VR) and the inductor (VL).

XL = 2fL
Z = R2 + XL2
VL = VS (XL / Z)
VR = VS (R / Z)
Seatwork #2:
• The area of an ellipse is given by the formula: Area =
ab. The circumference of an ellipse is given by the
formula C =   4 (a+b)2

Write a program to calculate and display the area and


the circumference of an ellipse by allowing the user to
input for the minor (a) and major (b) axes.

Note: Use - Math.PI for the 


- Math.Sqrt() symbol for the square root
- ^ symbol for the exponent
Assignment
• Write a VB program that computes and displays the
total resistance and total current of a parallel circuit
consisting of 3 resistors and voltage source as
shown below:


The relationship for the total resistance is:
𝑅1 𝑅2 𝑅3
𝑅𝑇 =
𝑅2 𝑅3 + 𝑅1 𝑅3 + 𝑅1 𝑅2

The relationship for the total current is:


𝑉𝑇
𝐼𝑇 =
𝑅𝑇
Arithmetic shorthand version
• Addition:
• X +=1 means X = X + 1
• Subtraction
• X -=1 means X = X - 1
• Multiplication
• X *=1 means X = X * 1
• Division
• X /=1 means X = X / 1
Constant Variable
• Variable defined at design time.
• Contains one permanent value throughout the
execution of the program.
– Syntax:
Const <varname> As <vartype> = <const value>

– Examples:
Const PIVal As Double = 3.14157
Const Ryte As Single = 0.07
Const R As Single = 0.07, Yr as Integer = 12
Concatenation Operator
• Combines String variables and constants
• Use the & symbol or the + symbol

• Examples:
Sample.Text = “Hook this” + “to this”
Time.Text = “Current time:" & Format(Now(), “hh:mm”)

Dim First as String = ”Jose”


Dim Last as String = “Rizal”
Msgbox (First & “ ” & Last)
Format() function
• Controls the form of numeric value or date in
the output.

• Syntax:
Format (<variable/expression>, “format”)
Format expressions:
Format
Expression
Abbreviation Result

Currency “c” or Display number with dollar sign, thousand


“Currency” separator, rounds-off to tenths digit
Fixed “f” or “F” Display number with 2 decimal places
Number “n” or “N” Display number with thousand separator
ex: “n2”, “n3”, “n4” and n number of decimal places
Percent “p” or “P” Display number in percentage
Scientific “e” or “E” Use standard notation
Date “MM/dd/yyyy” Display date in “MM/dd/yyyy” format
Time “hh:mm:ss:ms tt” Display expression in “hour:minute:sec:millisec
AM/PM” format
Format expressions:
Format Expression Explanation Result

Rounds to whole number without


Format(781234.57,”0”) separators between = 781234
thousands.
Rounds to 1 decimal place
Format(781234.57,”0.0”) without separators between = 781234.6
thousands.
Rounds to 2 decimal places without
Format(781234.576,”0.00”) separators between thousands. = 781234.58

Rounds to 2 decimal places with


Format(781234.576,”#,##0.00”) separators between = 781,234.58
thousands
Shows dollar sign and rounds to
2 decimal places with
Format(781234.576,”$#,##0.00”) = $781,234.58
separators between
thousands.
Examples
1. Tax = 0.6775
Taxes.Text = Format (Tax, “percent”)

2. Amount = 12345.65
AmtDue.Text = Format (Amount, “c”)

3. Registration.Text = Format(Today(),“MM/dd/yyyy”)
Examples
4. Format(#12/25/2016#, “MM”)

5. Format(#12/25/2016#, “MMM”)

6. Format(#12/25/2016#, “MMMM”)

7. Format(#12/25/2016#, “MMM. d, yyyy”)


Examples
8. Format(#12/25/2016#, “MMMM, yyyy”)

9. Format(#9:37:00 PM#, “hh:mm tt”)

10. Format(#12/25/2016 9:37:00 PM#, “MM/dd/yyyy hh:mm tt”`)


VB Math Functions
• Math.Abs()
– Returns the absolute value of a number

• Example:
X = Math.Abs(-3)
-> returns 3 to X
Result = Math.Abs(3.75)
-> returns 3.75 to Result
TextBox1.Text = Math.Abs(-10.89)
-> displays 10.89 to TextBox1
VB Math Functions
• Math.Sqrt()
– Returns the square root of a positive number.

• Example:
Dim Val, Hypo, A, B As Single
Val = Math.Sqrt(25)
-> returns 5 to Val
Hyp = Math.Sqrt (A^2 + B^2)
Exercise #4:
• Allow the user to input for the base and height
of the right triangle. Find and display the
hypotenuse.
Solution to Exercise #4
Private Sub Button1_Click()
Dim B, H As Single
B = TextBox1.Text
H = TextBox2.Text
TextBox3.Text = Math.Sqrt(B ^ 2 + H ^ 2)
End Sub
VB Math Functions
• Math.Sin( Angle_in_Radian )
– Returns the sine of an angle in radian
– Angle must be converted to Radian

• Example:
Dim Angle as Integer
Dim SVal as Single
Angle =30 ‘ in degrees, so it needs
‘ conversion
SVal = Math.Sin(Angle * math.PI / 180)
‘ returns 0.5
VB Math Functions
• Math.Cos( Angle_in_Radian )
– Returns the cosine of an angle in radian
– Angle must be converted to Radian

• Example:
Dim Angle as Integer
Dim CsVal as Single
Angle =30 ‘ in degrees, so needs
‘ conversion
CsVal = Math.Cos(Angle * math.PI / 180)
‘ returns 0.8660254
VB Math Functions
• Math.Tan( Angle_in_Radian )
– Returns the tangent of the given angle in radian
– Angle must be converted to Radian

• Example:
Dim Angle as Integer
Dim TVal as Single
Angle =45 ‘ in degrees, so needs
‘ conversion
TsVal = Math.Tan(Angle * math.PI / 180)
‘ returns 1
Exercise #5
• Allow the user to input angle in degrees. Write
a VB program that finds all the trigonometric
functions of the given angle.
Solution to Exercise #5
Private Sub Compute_Click()
Dim Angle, Sn, Csn, Tang, Ctang, Scnt, Cscnt As Decimal
Angle = TextBox1.Text
Sn = Math.Sin(Angle * Math.PI / 180)
Csn = Math.Cos(Angle * Math.PI / 180)
Tang = Math.Tan(Angle * Math.PI / 180)
Ctang = 1 / Tang
Scnt = 1 / Csn
Cscnt = 1 / Sn
Label3.Text = "Sine(" & Angle & ")"
Label4.Text = "Cosine(" & Angle & ")"
: : : : :
Label8.Text = "Cosecant(“ & Angle & ")“

TextBox2.Text = Format (Sn, "n4")


TextBox3.Text = Format (Csn, "n4")
: : : : :
TextBox7.Text = Format (Cscnt, "n4")
End Sub
VB Math Functions
• Math.Pow( base, power )
– Returns the result of a base raised to a power.
– Similar to using a caret ( ^ )

• Example:
Dim Val as Single
Val = Math.Pow(2, 8) ‘ returns 256

‘or, can be written as


Val = 2 ^8
Other Math Functions
 Math.Acos()  Math.Cosh()
• Math.Asin()  Math.Sinh()
• Math.Atan()  Math.Tanh()

• Math.Exp()  Math.Log
• Math.Log10  Math.Max()
• Math.Min()  Math.Round()
Exercise #6
• Revise Exercise #4 problem. Find and display
the hypotenuse and the angle of the right
triangle.
Solution to Exercise #6
Private Sub Button1_Click()
Dim b, h, hyp, Angle As Single
b = TextBox1.Text
h = TextBox2.Text
hyp = Math.Sqrt(b ^ 2 + h ^ 2)
TextBox3.Text = hyp
Angle = Math.Asin(h / hyp) * 180 / Math.PI
TextBox4.Text = Angle & "degrees"
End Sub
Seatwork: Oblique triangle
1. Allow the user to input for the two angles A, and B
(all in degrees), and the side a of an oblique
triangle. Thereafter, solve for the triangle using laws
of sine. Draw the user interface.
𝑎 𝑏 𝑐
= =
sin 𝐴 sin 𝐵 𝑆𝑖𝑛 𝐶

2. Allow the user to input for the two sides a and b, and
the angle C (in degrees) of an oblique triangle.
Thereafter, solve for the triangle using laws of
cosine. Draw the user interface.
a2= b2 + c2 – 2bc cos A
b2= a2 + c2 – 2ac cos B
c2= a2 + b2 – 2ab cos C
VB String Function
• Len()
– Returns the number of characters in a string
Examples:
X = Len (“VB is fun”)
Y = Math.Sqrt ( Len(“Nine”) )
Z = Len(“101011011”)

• UCase / LCase()
– Returns the upper/lower case letters
Example:
txtAnswer.Text = UCase (“VB is fun”)
VB String Function
• Microsoft.VisualBasic.Left(“string”, n )
– Retrieves text from the left side of a string

• Example:
MsgBox(Microsoft.VisualBasic.Left(“This is a Test.”, 4)
‘=> prints “This”

X = Microsoft.VisualBasic.Left(“101011011”, 6)
VB String Function
• Microsoft.VisualBasic.Right(“string”, n)
– Retrieves text from the right side of a string.

• Example:
MsgBox(Microsoft.VisualBasic.Right(“This is a Test.”, 4)
‘=> prints “est.”

X = Microsoft.VisualBasic.Right(“101011011”, 3)
VB String Function
• Mid (“string”, starting n, [n characters])
– Retrieves text within a string

Example:
Dim Str As String = "Mid Function Demo“
X = Mid(Str, 1, 3) -> returns “Mid”
Y = Mid(Str, 14, 4) -> returns “Demo”.
Z = Mid(Str, 5) -> returns "Function Demo”
M = Mid (“101011011”, 4, 5)
-> returns ???
VB String Function
• Chr()
– Returns the character associated with the specified
character code.

Example:
Dim chtr As Char
chtr = Chr(65) ‘ returns "A” to chtr
chtr = Chr(97) ‘ returns “a”
chtr = Chr(62) ‘ returns “>”
chtr = Chr(37) ‘ returns “%”
VB String Function
• Asc()
– Returns an integer value that represents the
character code corresponding to a character

Example:
Dim cod As Integer
cod = Asc("A") ‘Returns 65 to cod
cod = Asc(“a") ‘Returns 97
cod = Asc("Apple") ‘Returns 65
Understanding the colors in Coding
• Anything in GREEN is a comment and will not be
executed when the program runs.
• Anything in BLUE is a VB keyword.
• Anything in RED is a VB string constant.
• Anything in BLACK is an executable statement.
Executable statements frequently contain keywords
so a mixture of BLUE and BLACK is very common.
• Anything in “squiggly” BLUE line is a syntax error.
Assignment
1. A wire is stretched from the top of a vertical
pole standing on level ground. The wire
reaches to a point on the ground with X feet
from the foot of the pole, and makes an angle
of Y with the horizontal. Draw a screen design
that allows user to input for the X and Y.
Thereafter, write a code to compute for the
height of the pole and the length of the wire.
Assignment
2. From the top cliff X feet high the angle of
depression of a boat is Y degrees. Draw the
screen design allowing the user to input for the
values for X and Y. Also, write a code to
compute the distance between the boat and
the foot of the cliff.
Assignment
3. Algebraic solution of two equations system.
Allow the user to input for the numerical
coefficients of x and y and the constant values
for the two equations. Determine and display the
values for x and y.
Format: ax + by = e
cx + dy = f

Use the formula:


x = (de – bf) / (ad – bc)
y = (af – ce) / (ad – bc)
End of Module 1

You might also like