Membuat Calculator dengan menggunakan Visual Studio 2008
- Design form seperti dibawah ini
Gambar
1.1
- Lalu ketikan perintah seperti dibawah ini sesuai pada bagian sub nya
Public Class Form1
Dim a As Double = 0
Dim b As Double = 0
Dim c As Double = 0
Dim bil As String = "0"
Dim
operatornya As String
= ""
Function
hasil(ByVal xoperatornya As String) As Double
Select Case xoperatornya
Case
"kali" : c = a * b
Case
"bagi" : c = a / b
Case "tambah" : c = a + b
Case
"kurang" : c = a - b
End Select
hasil = c
End Function
Private Sub btn1_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn1.Click
If bil
<> "0" Then
bil = bil + "1"
Else
bil = "1"
End If
layar.Text = bil
End Sub
Private Sub btn2_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn2.Click
If bil
<> "0" Then
bil = bil + "2"
Else
bil = "2"
End If
layar.Text = bil
End Sub
Private Sub btn3_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn3.Click
If bil
<> "0" Then
bil = bil + "3"
Else
bil = "3"
End If
layar.Text = bil
End Sub
Private Sub btn4_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn4.Click
If bil
<> "0" Then
bil = bil + "4"
Else
bil = "4"
End If
layar.Text = bil
End Sub
Private Sub btn5_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn5.Click
If bil
<> "0" Then
bil = bil + "5"
Else
bil = "5"
End If
layar.Text = bil
End Sub
Private Sub btn6_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn6.Click
If bil
<> "0" Then
bil = bil + "6"
Else
bil = "6"
End If
layar.Text = bil
End Sub
Private Sub btn7_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn7.Click
If bil
<> "0" Then
bil = bil + "7"
Else
bil = "7"
End If
layar.Text = bil
End Sub
Private Sub btn8_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn8.Click
If bil
<> "0" Then
bil = bil + "8"
layar.Text = bil
Else :
bil = "8"
layar.Text = bil
End If
End Sub
Private Sub btn9_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn9.Click
If bil
<> "0" Then
bil = bil + "9"
Else
bil = "9"
End If
layar.Text = bil
End Sub
Private Sub btn0_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btn0.Click
If bil
<> "0" Then
bil = bil + "0"
Else
bil = "0"
End If
layar.Text = bil
End Sub
Private Sub btnhasil_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnhasil.Click
b = Val(bil)
layar.Text = hasil(operatornya)
bil = "0"
a = 0
b = 0
End Sub
Private Sub btnbagi_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnbagi.Click
If a =
0 Then
a = Val(bil)
operatornya = "bagi"
bil = "0"
Else :
b = Val(bil)
operatornya = "bagi"
bil = ""
End If
layar.Text = "0"
End Sub
Private Sub btnkali_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnkali.Click
If a =
0 Then
a = Val(bil)
operatornya = "kali"
bil = "0"
layar.Text = "0"
Else
b = Val(bil)
operatornya = "kali"
bil = ""
layar.Text = "0"
End If
End Sub
Private Sub btnkurang_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnkurang.Click
If a =
0 Then
a = Val(bil)
operatornya = "kurang"
bil = ""
Else
b = Val(bil)
operatornya = "kurang"
bil = ""
End If
layar.Text = "0"
End Sub
Private Sub btntambah_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btntambah.Click
If a =
0 Then
a = Val(bil)
operatornya = "tambah"
bil = ""
Else
b = Val(bil)
operatornya = "tambah"
bil = ""
End If
layar.Text = "0"
End Sub
Private Sub btnc_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnc.Click
a = 0
b = 0
c = 0
bil = "0"
layar.Text = "0"
End Sub
Private Sub btnback_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnback.Click
Dim jml
As Integer =
bil.Length
bil = bil.Substring(0, jml - 1)
layar.Text = bil
End Sub
Private Sub btnminus_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles btnminus.Click
Dim
minus As Integer
= Val(bil) * -1
bil = Val(bil) * -1
layar.Text = bil
End Sub
Private Sub btntitik_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btntitik.Click
If bil
= "0" Then
bil = bil + "."
Else
bil = "0"
End If
layar.Text = bil
End Sub
Private Sub btn1_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn1.GotFocus
btn1.BackColor = Color.AliceBlue
End Sub
Private Sub btn1_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn1.LostFocus
btn1.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn2_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn2.GotFocus
btn2.BackColor = Color.AliceBlue
End Sub
Private Sub btn2_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn2.LostFocus
btn2.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn3_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn3.GotFocus
btn3.BackColor = Color.AliceBlue
End Sub
Private Sub btn3_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn3.LostFocus
btn3.BackColor = Color.AntiqueWhite
End Sub
Private Sub btntambah_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btntambah.GotFocus
btntambah.BackColor = Color.AliceBlue
End Sub
Private Sub btntambah_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btntambah.LostFocus
btntambah.BackColor = Color.Gold
End Sub
Private Sub btnkurang_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnkurang.GotFocus
btnkurang.BackColor =
Color.AntiqueWhite
End Sub
Private Sub btnkurang_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnkurang.LostFocus
btnkurang.BackColor = Color.Gold
End Sub
Private Sub btnkali_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnkali.GotFocus
btnkali.BackColor = Color.Gold
End Sub
Private Sub btnkali_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnkali.LostFocus
btnkali.BackColor = Color.AntiqueWhite
End Sub
Private Sub btnbagi_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnbagi.GotFocus
btnbagi.BackColor = Color.Gold
End Sub
Private Sub btnbagi_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnbagi.LostFocus
btnbagi.BackColor = Color.AliceBlue
End Sub
Private Sub btnminus_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnminus.GotFocus
btnminus.BackColor = Color.Gold
End Sub
Private Sub btnminus_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnminus.LostFocus
btnminus.BackColor = Color.AntiqueWhite
End Sub
Private Sub btntitik_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btntitik.GotFocus
btntitik.BackColor = Color.Gold
End Sub
Private Sub btntitik_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btntitik.LostFocus
btntitik.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn0_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn0.GotFocus
btn0.BackColor = Color.AliceBlue
End Sub
Private Sub btn0_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn0.LostFocus
btn0.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn7_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn7.GotFocus
btn7.BackColor = Color.AliceBlue
End Sub
Private Sub btn7_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn7.LostFocus
btn7.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn8_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn8.GotFocus
btn8.BackColor = Color.AliceBlue
End Sub
Private Sub btn8_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn8.LostFocus
btn8.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn9_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn9.GotFocus
btn9.BackColor = Color.AliceBlue
End Sub
Private Sub btn9_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn9.LostFocus
btn9.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn4_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn4.GotFocus
btn4.BackColor = Color.AliceBlue
End Sub
Private Sub btn4_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn4.LostFocus
btn4.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn5_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn5.GotFocus
btn5.BackColor = Color.AliceBlue
End Sub
Private Sub btn5_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn5.LostFocus
btn5.BackColor = Color.AntiqueWhite
End Sub
Private Sub btn6_GotFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn6.GotFocus
btn6.BackColor = Color.AliceBlue
End Sub
Private Sub btn6_LostFocus(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btn6.LostFocus
btn6.BackColor = Color.AntiqueWhite
End Sub
End Class
0 komentar:
Posting Komentar