Assalamualaikum wr. wb
haddeeehh,, akhirnya selesai juga ne,,,
buat temen yang hobi belajaar program, kali ini saya postingkan gmana caranya kita buat program kalkulator sendiri dari VB.Net 2008. Nah, untuk itu mari kita lihat dan pahami bagaimana cara buatnya,,
pertama buka VB.Net 2008 dan desain seperti form berikut :
ini dia listing Program Kalkulator dari VB.Net 2008, cekidott,,,
1. Double klik pada form lalu masukkan kode berikut :
Public Class Form1
Dim operand1 As Double
Dim [operator] As String
Dim baru As Boolean = True
Dim temp As Double
Dim operand2 As Double
2. Double klik pada button 0 ketikkan kode
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
TextBox1.Text = TextBox1.Text & sender.text
End Sub
3. Masukkan kode berikut pada button Tambah
Private Sub btntmbh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntmbh.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
4. Masukkan Kode Berikut Pada Button Kurang
Private Sub btnkrg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkrg.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
5. Kode Berikut pada button Kali
Private Sub Btnkali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkali.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
6. Kode Berikut Pada button Bagi
Private Sub Btnbagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbagi.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
7. Kode berikut pada button hasil
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
8. Kode berikut pada button titik
Private Sub btntitik_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntitik.Click
If InStr(TextBox1.Text, ".") > 0 Then
Exit Sub
Else
TextBox1.Text = TextBox1.Text & "."
End If
End Sub
9. Dan terakhir pada button Clear
Private Sub btnclr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclr.Click
TextBox1.Text = ""
operand1 = 0
temp = 0
baru = True
End Sub
Nah, itulah contoh Program Kalkulator menggunakan VB.Net yang saya buat, semoga bermanfaat..
Wassalamualaikum,,,,