Kali
ini saya akan memposting tentang membuat kalkulator dengan VB.net / Visual
Studio 2010.
Langkah
- langkah membuat Calculator dengan VB.net :
1. Buka Microsoft Visual Studio 2010
1. Buka Microsoft Visual Studio 2010
2. Pilih New Project->Windows Form
Application
3.
Buat 20 buah Button dan 1 buah Textbox pada Form
4.
Edit Text property pada tiap Button sehingga tampilan seperti gambar dibawah.
5.
Double klik pada form lalu masukkan kode berikut :
Public Class Form1
Dim operand1 As Double
Public Class Form1
Dim operand1 As Double
Dim [operator] As String
Dim baru As Boolean = True
Dim temp As Double
Dim operand2 As Double
6. Double klik pada button 0 ketikkan kode
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click, Button11.Click, Button12.Click, Button6.Click, Button7.Click, Button8.Click, Button1.Click, Button2.Click, Button3.Click, Button10.Click
TextBox1.Text = TextBox1.Text & sender.text
End Sub
7.
Masukkan kode berikut pada button Tambah
Private Sub Buttom9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Private Sub Buttom9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If baru() = True Then
operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[operator] = "+"
ElseIf baru() = False Then
If [operator] = "+" Then
temp = operand1 + Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "+"
ElseIf [operator] = "-" Then
temp = operand1 - Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "+"
ElseIf [operator] = "x" Then
temp = operand1 * Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "+"
ElseIf [operator] = ":" Then
temp = operand1 / Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "+"
End If
End If
baru = False
End Sub
8.
Masukkan Kode Berikut Pada Button Kurang
Private Sub Buttom14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If baru() = True Then
operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[operator] = "-"
ElseIf baru() = False Then
If [operator] = "+" Then
temp = operand1 + Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "-"
ElseIf [operator] = "-" Then
temp = operand1 - Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "-"
ElseIf [operator] = "x" Then
temp = operand1 * Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "-"
ElseIf [operator] = ":" Then
temp = operand1 / Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "-"
End If
End If
baru = False
End Sub
9.
Kode Berikut pada button Kali
Private Sub Buttom19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
Private Sub Buttom19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
If baru() = True Then
operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[operator] = "x"
ElseIf baru() = False Then
If [operator] = "+" Then
temp = operand1 + Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "x"
ElseIf [operator] = "-" Then
temp = operand1 - Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "x"
ElseIf [operator] = ";" Then
temp = operand1 / Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "x"
ElseIf [operator] = "x" Then
temp = operand1 * Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = "x"
End If
End If
baru = False
End Sub
10.
Kode Berikut Pada button Bagi
Private Sub Buttom4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Private Sub Buttom4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If baru() = True Then
operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
[operator] = ":"
ElseIf baru() = False Then
If [operator] = "+" Then
temp = operand1 + Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = ":"
ElseIf [operator] = "-" Then
temp = operand1 - Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = ":"
ElseIf [operator] = "x" Then
temp = operand1 * Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = ":"
ElseIf [operator] = ":" Then
temp = operand1 / Val(TextBox1.Text)
operand1 = temp()
TextBox1.Text = ""
[operator] = ":"
End If
End If
baru = False
End Sub
11.
Kode berikut pada button hasil
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
Dim hasil As Double
operand2 = Val(TextBox1.Text)
Select Case [operator]
Case "+"
hasil = operand1 + operand2
TextBox1.Text = hasil.ToString
baru = True
Case "-"
hasil = operand1 - operand2
TextBox1.Text = hasil.ToString
Case ":"
hasil = operand1 / operand2
TextBox1.Text = hasil.ToString
Case "x"
hasil = operand1 * operand2
TextBox1.Text = hasil.ToString
baru = True
End Select
TextBox1.Text = hasil.ToString
End Sub
12. Kode berikut pada button titik
Private Sub Buttom17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If InStr(TextBox1.Text, ".") > 0 Then
Exit Sub
Else
TextBox1.Text = TextBox1.Text & "."
End If
End Sub
13.
Kode berikut pada button Clear
Private Sub Buttom5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
TextBox1.Text = ""
operand1 = 0
temp = 0
baru = True
End Sub
14.
Pada button Pangkat
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button10.Click
If TextBox1.Text = "" Or TextBox1.Text = "" Then
MsgBox("Maaf, nilai belum diinput")
Else
TextBox1.Text = Val(TextBox1.Text) * Val(TextBox1.Text)
End If
End Sub
15.
Terakhir kode pada button Decimal
Private
Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button15.Click
Dim convert As Single
If TextBox1.Text <> 0 Then
convert = 1 / Val(TextBox1.Text)
TextBox1.Text = convert
End If
End Sub
Contoh
gambar Coding :
14.
Click Start Debugging dan lihat hasilnya
0 comments:
Post a Comment