Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

COLLEGE OF ENGINEERING AND TECHNOLOGY

Computer Fundamentals and Programming 1 (A111L)


Prepared by: Engr. Michael B. Tomas

FORCE CALCULATOR
LECTURE
In this topic, students will create a simple calculator that solve for the Force using Visual Basic.
First is to design the Form. Design the form like the figure below.

Steps
1. Add two (2) Textbox, Four (4) Label and one (1) Button to your form. You can get these objects
from your Toolbox. Simply drag your object to your Form.
2. Change the properties of each Objects including the Form. Below is the list of properties to be
changed.

On Text Property
Name Values
Label1 Mass
Label2 Acceleration
Label3 Force =
Label4 0
Button1 Calculate
Form1 Force Calculator
On Name Property
Name Change Name to
Textbox1 txtMass
Textbox2 txtAcc
Label4 lblForce
Button1 btnCalc
Properties on Form1
Properties Values
ShowIcon False
MaximizedBox False
FormBorderStyles FixedSingle
StartPosition CenterScreen

1|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

3. Double Click the button to go to Code Window then type the Code below.

Public Class Form1

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click


Dim F, m, a As Decimal

m = txtMass.Text
a = txtAcc.Text

F = m * a

lblForce.Text = F

End Sub
End Class
4. All Done, Click the Start button to try your program

2|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

ACTIVITY 1
Create a calculator for Quadratic Equations.

3|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

LOGIN WINDOW
LECTURE
VB Object Properties
1. Open a New Project on Visual Studio
2. Double Click the Form. (Do not create or add any objects)
3. Type the code below
Code
Public Class Form1
Dim txtPass As New TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim lblPass As New Label


Dim btnPass As New Button

Controls.Add(txtPass)
Controls.Add(lblPass)
Controls.Add(btnPass)

txtPass.Location = New Point(150, 30)


txtPass.Font = New Font("Arial", 18, FontStyle.Regular)
txtPass.Size = New Size(200, 40)
txtPass.PasswordChar = "*"

lblPass.Text = "Password"
lblPass.Location = New Point(30, 30)
lblPass.Font = New Font("Arial", 18, FontStyle.Regular)
lblPass.Size = New Size(150, 40)

Me.Text = "Enter Password"


Me.ShowIcon = False
Me.MinimizeBox = False
Me.MaximizeBox = False
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
Me.Size = New Size(410, 200)

btnPass.Text = "Login"
btnPass.Location = New Point(200, 90)
btnPass.Font = New Font("Arial", 18, FontStyle.Regular)
btnPass.Size = New Size(150, 40)

AddHandler btnPass.Click, AddressOf btnPass_Click


End Sub

Private Sub btnPass_Click(sender As Object, e As EventArgs)


If txtpass.text = "456" Then
MsgBox("Welcome", vbInformation, "Mabuhay!")
Else
MsgBox("Wrong password", vbCritical, "Error")
End If
End Sub
End Class
4. Done. Click the start and automatically without dragging object on the form, it creates its own
objects and designs according to the written code.

4|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

ACTIVITY 2
Label sequence using button. The form has two (2) labels and one (1) button. When the button
clicked, the two labels alternately will appear and disappear.

5|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

RESISTANCE CALCULATOR
(Includes Error, Comments, If, and Select Case. Using Combobox)

ACTIVITY 3
Create a Calculator for Total Resistance and Total Current in Series and Parallel

6|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

LECTURE
Design the Form
Design a form like the picture below. The form has Four (4) Textbox, Eight (8) Labels, One (1)
Combobox and One (1) Button.
Rename the following objects.

Name Change Name to


Textbox1 txtR1
Textbox2 txtR2
Textbox2 txtR3
The Label Zero (0) beside Rt lblRt
The Label Zero (0) beside It lblIt
Button1 btnCalc

7|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

Double click the button and enter the code below.

This Code is using IF Syntax

Public Class Form1

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click


Dim R1, R2, R3, Vt, Rt, It As Decimal

R1 = txtR1.Text
R2 = txtR2.Text
R3 = txtR3.Text
Vt = txtVt.Text

If cmb1.Text = "Series" Then


'Formula for Series
Rt = R1 + R2 + R3
It = Vt / Rt
ElseIf cmb1.Text = "Parallel" Then
'Formula for Parallel
Rt = (R1 * R2 * R3) / ((R2 * R3) + (R1 * R3) + (R1 * R2))
It = Vt / Rt
Else
MsgBox("Pili kamuna sa listahan", vbCritical, "Pili Muna")
End If

lblRt.Text = Math.Round(Rt, 2) & " Ohm"


lblIt.Text = Math.Round(It, 2) & " Amp"

End Sub
End Class

8|Page
COLLEGE OF ENGINEERING AND TECHNOLOGY
Computer Fundamentals and Programming 1 (A111L)
Prepared by: Engr. Michael B. Tomas

This Code is using Select Case Syntax.


Notice that the process codes are changed from IF syntax to Select Case Syntax.

Public Class Form1

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click


Dim R1, R2, R3, Vt, Rt, It As Decimal

R1 = txtR1.Text
R2 = txtR2.Text
R3 = txtR3.Text
Vt = txtVt.Text

Select Case cmb1.Text


Case Is = "Series"
'Formula for Series
Rt = R1 + R2 + R3
It = Vt / Rt
Case Is = "Parallel"
'Formula for Parallel
Rt = (R1 * R2 * R3) / ((R2 * R3) + (R1 * R3) + (R1 * R2))
It = Vt / Rt
Case Else
MsgBox("Pili kamuna sa listahan", vbCritical, "Pili Muna")
End Select

lblRt.Text = Math.Round(Rt, 2) & " Ohm"


lblIt.Text = Math.Round(It, 2) & " Amp"

End Sub
End Class

9|Page

You might also like