Kalkulator Sederhana



 Di sini saya akan menjelaskan cara membuat kalculator sederhana, pertama kita buat form, label, teksbox, dan button, setelah itu kita susun button tersebut, Stelah itu coba jalan kan program yang telah di buat.
Berikut cara membuat Program Kalkulator  sederhana di atas menggunakan Visual Basic 2010:
1.       Buka Visual Basic 2010, New Project à Windows Forms Aplication à OK.
2.      Buat desain kalkulator seperti gambar di bawah ini dengan memasukkan komponen dari toolbox ke dalam form design.
3.      Komponen yang digunakan adalah sebagai berikut :
Komponen
Properties
Keterangan
Form 1
Name
Text
Form 1

Textbox1
Name
Text
Textbox1
Button 1
Name
Text
Button 1
Angka 1
Button 2
Name
Text
Button 2
Angka 2
Button 3
Name
Text
Button 3
Angka 3
Button 4

Name
Text
Button 4
Angka 4
Button 5
Name
Text
Button 5
Angka 5
Button 6
Name
Text
Button 6
Angka 6
Button 7
Name
Text
Button 7
Angka 7
Button 8
Name
Text
Button 8
Angka 8
Button 9
Name
Text
Button 9
Angka 9
Button 10
Name
Text
Button 10
Angka 0
Button 11
Name
Text
Button 11
Operator plus
Button 12
Name
Text
Button 12
Operator minus
Button 13
Name
Text
Button 13
Operator kali
Button 14
Name
Text
Button 14
Operator bagi
Button 15
Name
Text
Button 15
Operator sama dengan
Button 16
Name
Text
Button 16
Operator AC
Button 17
Name
Text
Button 17
Operator pangkat
Button 18
Name
Text
Button 18
Operator akar
Button 19
Name
Text
Button 19
Operator SIN
Button 20
Name
Text
Button 20
Operator COS
Button 21
Name
Text
Button 21
Operator TAN

4. Masukkan listing untuk komponen Button1, Button2, dan seterusnya sampai Button25. Caranya tekan F7 atau double klik di setiap button masing-masing lalu masukkan listing berikut:

Public Class Form1
Dim nilai1, nilai2, hasil As Decimal
Dim sharedoperator As String
Dim phi As Decimal = 3.1415926535897944
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = “” Then
TextBox1.Text = 1
Else : TextBox1.Text = TextBox1.Text & 1
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = “” Then
TextBox1.Text = 2
Else : TextBox1.Text = TextBox1.Text & 2
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text = “” Then
TextBox1.Text = 3
Else : TextBox1.Text = TextBox1.Text & 3
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If TextBox1.Text = “” Then
TextBox1.Text = 4
Else : TextBox1.Text = TextBox1.Text & 4
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If TextBox1.Text = “” Then
TextBox1.Text = 5
Else : TextBox1.Text = TextBox1.Text & 5
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If TextBox1.Text = “” Then
TextBox1.Text = 6
Else : TextBox1.Text = TextBox1.Text & 6
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If TextBox1.Text = “” Then
TextBox1.Text = 7
Else : TextBox1.Text = TextBox1.Text & 7
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If TextBox1.Text = “” Then
TextBox1.Text = 8
Else : TextBox1.Text = TextBox1.Text & 8
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If TextBox1.Text = “” Then
TextBox1.Text = 9
Else : TextBox1.Text = TextBox1.Text & 9
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If TextBox1.Text = “” Then
TextBox1.Text = 0
Else : TextBox1.Text = TextBox1.Text & 0
End If
TextBox1.Text = Int(TextBox1.Text)
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
If nilai1 = 0 Then
nilai1 = Val(TextBox1.Text)
Else
nilai1 = nilai1 + Val(TextBox1.Text)
End If
sharedoperator = “+”
TextBox1.Text = 0
End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
If nilai1 = 0 Then
nilai1 = Val(TextBox1.Text)
Else
nilai1 = nilai1 – Val(TextBox1.Text)
End If
sharedoperator = “-”
TextBox1.Text = 0
End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
If nilai1 = 0 Then
nilai1 = Val(TextBox1.Text)
Else
nilai1 = nilai1 * Val(TextBox1.Text)
End If
sharedoperator = “*”
TextBox1.Text = 0
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If nilai1 = 0 Then
nilai1 = Val(TextBox1.Text)
Else
nilai1 = nilai1 / Val(TextBox1.Text)
End If
sharedoperator = “/”
TextBox1.Text = 0
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
If sharedoperator = “+” Then
hasil = nilai1 + Val(TextBox1.Text)
TextBox1.Text = hasil
ElseIf sharedoperator = “-” Then
hasil = nilai1 – Val(TextBox1.Text)
TextBox1.Text = hasil
ElseIf sharedoperator = “*” Then
hasil = nilai1 * Val(TextBox1.Text)
TextBox1.Text = hasil
ElseIf sharedoperator = “/” Then
hasil = nilai1 / Val(TextBox1.Text)
TextBox1.Text = hasil
ElseIf sharedoperator = “^” Then
hasil = nilai1 ^ Val(TextBox1.Text)
TextBox1.Text = hasil
ElseIf sharedoperator = “akar” Then
hasil = nilai1 ^ 0.5
TextBox1.Text = hasil
End If
nilai1 = 0
End Sub

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
TextBox1.Text = “”
sharedoperator = “”
End Sub


Komentar

Postingan populer dari blog ini

Contoh program Penerapan Function pada Microsoft Visual Studio 2010

termometer digital dengan sensor lm35