www.komputerbanguye.blogspot.com - Cara Membuat Kalkulator Sederhana dengan Visual Basic atau Visual Studio - Pada tutorial kali ini kita akan membuat kalkulator persis seperti aslinya. Tentunya konsep pembuatan atau logika program yang akan kita buat ini bisa diterapkan pada rumus matematika atau fisika, yang pada intinya kita harus mengetahui alur kerja secara manual dari sebuah rumus tersebut sehingga bisa diterjemahkan kedalam bahasa pemograman visual basic.
Langkah - Langkah Pembuatan
1. Design Tampilan
Desain tampilan seperti pada gambar diatas, disini saya cukup menggunakan 2 buah menu tool box, yaitu textbox dan button.
2. Coding
coding nya sebagai berikut:
Untuk coding logika dibawah public class
Public Class Form1
Const phi As Double = 22 / 7
Dim input_angka, hasil As Double
Dim rumus As String
Untuk coding button "="
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
If rumus = "+" Then
TextBox1.Text = input_angka + Val(TextBox1.Text)
ElseIf rumus = "-" Then
TextBox1.Text = input_angka - Val(TextBox1.Text)
ElseIf rumus = "*" Then
TextBox1.Text = input_angka * Val(TextBox1.Text)
ElseIf rumus = "/" Then
TextBox1.Text = input_angka / Val(TextBox1.Text)
End If
End Sub
Untuk coding button "1" sampai "0" dan "."
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text & "1"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text & "2"
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
TextBox1.Text = TextBox1.Text & "0"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text & "3"
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox1.Text = TextBox1.Text & "4"
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox1.Text = TextBox1.Text & "5"
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox1.Text = TextBox1.Text & "6"
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
TextBox1.Text = TextBox1.Text & "7"
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox1.Text = TextBox1.Text & "8"
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
TextBox1.Text = TextBox1.Text & "9"
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
TextBox1.Text = TextBox1.Text & "."
End Sub
Untuk coding button "+"
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
input_angka = Val(TextBox1.Text)
rumus = "+"
TextBox1.Text = Nothing
End Sub
Untuk coding button "-"
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
input_angka = Val(TextBox1.Text)
rumus = "-"
TextBox1.Text = Nothing
End Sub
Untuk coding button "X"
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
input_angka = Val(TextBox1.Text)
rumus = "*"
TextBox1.Text = Nothing
End Sub
Untuk coding button ":"
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
input_angka = Val(TextBox1.Text)
rumus = "/"
TextBox1.Text = Nothing
End Sub
Untuk coding button "√"
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
TextBox1.Text = Math.Sqrt(TextBox1.Text)
End Sub
Untuk coding button "ON"
Private Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Click
TextBox1.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
Button7.Enabled = True
Button8.Enabled = True
Button9.Enabled = True
Button10.Enabled = True
Button11.Enabled = True
Button12.Enabled = True
Button13.Enabled = True
Button14.Enabled = True
Button15.Enabled = True
Button16.Enabled = True
Button17.Enabled = True
Button18.Enabled = True
Button19.Enabled = True
Button20.Enabled = True
Button21.Enabled = True
Button22.Enabled = True
Button23.Enabled = True
Button24.Enabled = True
Button25.Enabled = True
End Sub
Untuk coding button "Cos"
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
TextBox1.Text = Math.Cos(TextBox1.Text * (phi / 180))
End Sub
Untuk coding button "Sin"
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
TextBox1.Text = Math.Sin(TextBox1.Text * (phi / 180))
End Sub
Untuk coding button "OFF"
Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
TextBox1.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
Button10.Enabled = False
Button11.Enabled = False
Button12.Enabled = False
Button13.Enabled = False
Button14.Enabled = False
Button15.Enabled = False
Button16.Enabled = False
Button17.Enabled = False
Button18.Enabled = False
Button19.Enabled = False
Button20.Enabled = False
Button21.Enabled = False
Button22.Enabled = False
Button24.Enabled = False
Button25.Enabled = False
End Sub
Untuk coding button "AC"
Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click
TextBox1.Text = Nothing
input_angka = Nothing
hasil = Nothing
rumus = Nothing
End Sub
Untuk coding button "DEL"
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
TextBox1.Text = Nothing
input_angka = Nothing
hasil = Nothing
rumus = Nothing
End Sub
Untuk coding button "EXIT"
Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
rumus = MsgBox(" Apakah Anda yakin ingin keluar dari Aplikasi ini? ", MsgBoxStyle.Question + MsgBoxStyle.YesNo, " Calculator ")
If rumus = vbYes Then
Me.Close()
End If
End Sub
Untuk coding button "Tan"
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
TextBox1.Text = Math.Tan(TextBox1.Text * (phi / 180))
End Sub
End Class
Sekian tutorial dari saya, jika ada yang error dari program diatas bisa komentar di comment box, terimakasih.
Const phi As Double = 22 / 7
Dim input_angka, hasil As Double
Dim rumus As String
Untuk coding button "="
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
If rumus = "+" Then
TextBox1.Text = input_angka + Val(TextBox1.Text)
ElseIf rumus = "-" Then
TextBox1.Text = input_angka - Val(TextBox1.Text)
ElseIf rumus = "*" Then
TextBox1.Text = input_angka * Val(TextBox1.Text)
ElseIf rumus = "/" Then
TextBox1.Text = input_angka / Val(TextBox1.Text)
End If
End Sub
Untuk coding button "1" sampai "0" dan "."
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text & "1"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text & "2"
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
TextBox1.Text = TextBox1.Text & "0"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text & "3"
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox1.Text = TextBox1.Text & "4"
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox1.Text = TextBox1.Text & "5"
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox1.Text = TextBox1.Text & "6"
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
TextBox1.Text = TextBox1.Text & "7"
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox1.Text = TextBox1.Text & "8"
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
TextBox1.Text = TextBox1.Text & "9"
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
TextBox1.Text = TextBox1.Text & "."
End Sub
Untuk coding button "+"
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
input_angka = Val(TextBox1.Text)
rumus = "+"
TextBox1.Text = Nothing
End Sub
Untuk coding button "-"
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
input_angka = Val(TextBox1.Text)
rumus = "-"
TextBox1.Text = Nothing
End Sub
Untuk coding button "X"
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
input_angka = Val(TextBox1.Text)
rumus = "*"
TextBox1.Text = Nothing
End Sub
Untuk coding button ":"
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
input_angka = Val(TextBox1.Text)
rumus = "/"
TextBox1.Text = Nothing
End Sub
Untuk coding button "√"
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
TextBox1.Text = Math.Sqrt(TextBox1.Text)
End Sub
Untuk coding button "ON"
Private Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Click
TextBox1.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
Button7.Enabled = True
Button8.Enabled = True
Button9.Enabled = True
Button10.Enabled = True
Button11.Enabled = True
Button12.Enabled = True
Button13.Enabled = True
Button14.Enabled = True
Button15.Enabled = True
Button16.Enabled = True
Button17.Enabled = True
Button18.Enabled = True
Button19.Enabled = True
Button20.Enabled = True
Button21.Enabled = True
Button22.Enabled = True
Button23.Enabled = True
Button24.Enabled = True
Button25.Enabled = True
End Sub
Untuk coding button "Cos"
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
TextBox1.Text = Math.Cos(TextBox1.Text * (phi / 180))
End Sub
Untuk coding button "Sin"
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
TextBox1.Text = Math.Sin(TextBox1.Text * (phi / 180))
End Sub
Untuk coding button "OFF"
Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
TextBox1.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
Button10.Enabled = False
Button11.Enabled = False
Button12.Enabled = False
Button13.Enabled = False
Button14.Enabled = False
Button15.Enabled = False
Button16.Enabled = False
Button17.Enabled = False
Button18.Enabled = False
Button19.Enabled = False
Button20.Enabled = False
Button21.Enabled = False
Button22.Enabled = False
Button24.Enabled = False
Button25.Enabled = False
End Sub
Untuk coding button "AC"
Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click
TextBox1.Text = Nothing
input_angka = Nothing
hasil = Nothing
rumus = Nothing
End Sub
Untuk coding button "DEL"
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
TextBox1.Text = Nothing
input_angka = Nothing
hasil = Nothing
rumus = Nothing
End Sub
Untuk coding button "EXIT"
Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
rumus = MsgBox(" Apakah Anda yakin ingin keluar dari Aplikasi ini? ", MsgBoxStyle.Question + MsgBoxStyle.YesNo, " Calculator ")
If rumus = vbYes Then
Me.Close()
End If
End Sub
Untuk coding button "Tan"
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
TextBox1.Text = Math.Tan(TextBox1.Text * (phi / 180))
End Sub
End Class
Sekian tutorial dari saya, jika ada yang error dari program diatas bisa komentar di comment box, terimakasih.