Saya Kali Ini Akan Berbagi Ilmu Membuat Kalkulatir Sederhana Dengan Vb.net Segala Versi Mau Vb.net2010 Atau Vb.net 2008. Ok Kita LangSung Eksekusi Aja Ya Teman !!
Kalkulator Sederhana Dengan Kode Nya , Ini Untuk Latihan Yang masih Awam Tentang Vb.net Termasuk Juga Saya :
1. Buka Microsoft Visual Studio 2010 ultimate/2008
2. Pilih Windows Form Application
3. Buat 17 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
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 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
7. 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
8. 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
9. 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
10. 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
11. 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
12. 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
13. 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
Sekian Teman Semoga Bermanfaat bagi Teman _ Teman Semuanya... Salam M.R.P :v
0 comments:
Post a Comment