Intro To Programming
Intro To Programming
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.
• Numeric UpDownCounter
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
– Click on BackgroundImageLayout
property and choose Stretch
Properties of Form Object
• Window State
– Normal
– Maximize
– Minimize
• Start Position
– Windows Default
– Center Screen
• 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 (/).
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
PictureBox
(initially empty)
Buttons
Include the following Codes:
Private Sub Button1_Click( )
PictureBox1.Image=Image.FromFile(“x:\icons\StandardRoom.jpeg")
End Sub
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
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.
• 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
• 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.
Note:
VR1 = IT * R1
VR2 = IT * R2
VR3 = IT * R3
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 = 2fL
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
•
The relationship for the total resistance is:
𝑅1 𝑅2 𝑅3
𝑅𝑇 =
𝑅2 𝑅3 + 𝑅1 𝑅3 + 𝑅1 𝑅2
– 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”)
• Syntax:
Format (<variable/expression>, “format”)
Format expressions:
Format
Expression
Abbreviation Result
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”)
• 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 & ")“
• Example:
Dim Val as Single
Val = Math.Pow(2, 8) ‘ returns 256
• 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