Pada kesempatan kali ini saya akan memberikan tutorial membuat aplikasi Kalkulator sederhana menggunakan VB.Net. Kalkulator ini dapat melakukan operasi penjumlahan, pengurangan, perkalian dan pembagian. Berikut adalah Screenshot nya.
Komponen yang dibutuhkan, dan ubah namanya
Komponen | Name | Text |
Label1 | - | Input A |
Label2 | - | Input B |
Textbox1 | txtA | |
Textbox2 | txtB | |
Button1 | btnTambah | + |
Button2 | btnKurang | - |
Button3 | btnBagi | / |
Button4 | btnKali | * |
Button5 | btnClear | Clear |
Button6 | btnExit | Exit |
Sedangkan Sourcodenya dapat dilihat sebagai berikut
Public Class Form1
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtA.Text = ""
txtB.Text = ""
txtHasil.Text = ""
End Sub
Private Sub btnTambah_Click(sender As Object, e As EventArgs) Handles btnTambah.Click
'ini adalah proses penjumlahan, Val untuk konversi ke angka
txtHasil.Text = Val(txtA.Text) + Val(txtB.Text)
End Sub
Private Sub btnKurang_Click(sender As Object, e As EventArgs) Handles btnKurang.Click
txtHasil.Text = txtA.Text - txtB.Text
End Sub
Private Sub btnBagi_Click(sender As Object, e As EventArgs) Handles btnBagi.Click
'txtHasil.Text = txtA.Text / txtB.Text
'Ini adalah Proses Bagi
Try
If (txtA.Text.Trim = "") Then
MsgBox("A tidak boleh Kosong")
Exit Sub
ElseIf (txtB.Text.Trim = "") Then
MsgBox("B tidak boleh Kosong")
Exit Sub
End If
If (IsNumeric(txtA.Text) = False) Then
MsgBox("A harus Angka")
Exit Sub
ElseIf (IsNumeric(txtB.Text) = False) Then
MsgBox("B harus Angka")
Exit Sub
End If
Dim a As Double
Dim b As Double
Dim c As Double
a = Convert.ToInt32(txtA.Text)
b = Convert.ToDouble(txtB.Text)
If (b = 0) Then
txtHasil.Text = "Tak Terhingga"
MsgBox("Tak Terhingga", MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical, "Peringatan")
ElseIf (b <> 0) Then
c = a / b
txtHasil.Text = CStr(c)
End If
Catch ex As Exception
MsgBox("Terjadi Kesalahan")
End Try
End Sub
Private Sub btnKali_Click(sender As Object, e As EventArgs) Handles btnKali.Click
If ((IsNumeric(txtA.Text) = False) Or (IsNumeric(txtB.Text) = False)) Then
MsgBox("Input harus Angka", MsgBoxStyle.Information)
Exit Sub
End If
txtHasil.Text = txtA.Text * txtB.Text
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Dim hsl
hsl = MsgBox("Anda Yakin Keluar?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Konfirmasi")
If (hsl = MsgBoxResult.Yes) Then
End
ElseIf (hsl = MsgBoxResult.No) Then
End If
End Sub
Private Sub txtA_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtA.KeyPress
If (("0123456789.-" & Chr(8)).Contains(e.KeyChar.ToString) = False) Then
e.KeyChar = Chr(0)
End If
End Sub
Private Sub txtB_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtB.KeyPress
If (("0123456789.-" & Chr(8)).Contains(e.KeyChar.ToString) = False) Then
e.KeyChar = Chr(0)
End If
End Sub
Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
About.Show()
End Sub
End Class
Untuk Lebih jelasnya silahkan download project + source codenya langsung, Disini
Sekian, Terima Kasih ^^
No comments:
Post a Comment