Membuat Calculator di VB.NET 10

 Bismillah

Hai sahabat baiq, bagaimana kabarnya? Kali ini kembali mimin mau posting tentang pemrograman. Mohon maaf jika sudah beberapa lama mimin tak muncul di dunia maya, bukan karena apapun, tapi karena kesibukan yang semakin memadat.

Kali ini mimin mau posting terkait dengan pemrograman VB.NET. Kita akan belajar bersama untuk membuat aplikasi Calculator dengan VB.Net. Selama mencoba, semoga bermanfaat.


Langkah-langkah membuat calculator seperti gambar di atas adalah sebagai berikut:
1. Desain Form
    Adapun objek-objek yang digunakan untuk mendesain calculator seperti gambar di atas adalah:
  • Textbox1
  • Button (tombol angka 0 - 9, CE, ON, Close, Operator)
  • Groupbox
Pemberian nama objek silahkan disesuaikan dengan coding/script program.

2. Coding/Script Program

Public Class Form1
    Dim nilai, hasil As Double
    Dim rumus As String
    
    Sub hidup()
        Text1.Enabled = True
        GroupBox1.Enabled = True
        Text1.Focus()
    End Sub

    Sub mati()
        Text1.Enabled = False
        GroupBox1.Enabled = False
    End Sub

Klik dua kali pada objek Button Clear

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Text1.Text = Nothing
        nilai = Nothing
        hasil = Nothing
        rumus = Nothing
        Text1.Focus()
    End Sub

Klik dua kali pada objek Button Nol

    Private Sub btnnol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnol.Click
        If Text1.Text = "0" Then
            Text1.Text = "0"
        Else : Text1.Text = Text1.Text & "0"

        End If
    End Sub

Klik dua kali pada objek Button Satu

    Private Sub btnsatu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsatu.Click
        If Text1.Text = "0" Then
            Text1.Text = "1"
        Else : Text1.Text = Text1.Text & "1"

        End If
    End Sub

Klik dua kali pada objek Button Dua

    Private Sub btndua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndua.Click
        If Text1.Text = "0" Then
            Text1.Text = "2"
        Else : Text1.Text = Text1.Text & "2"

        End If
    End Sub

Klik dua kali pada objek Button Empat

    Private Sub btntiga_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntiga.Click
        If Text1.Text = "0" Then
            Text1.Text = "3"
        Else : Text1.Text = Text1.Text & "3"

        End If
    End Sub

Klik dua kali pada objek Button Empat

    Private Sub btnempat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnempat.Click
        If Text1.Text = "0" Then
            Text1.Text = "4"
        Else : Text1.Text = Text1.Text & "4"

        End If
    End Sub

Klik dua kali pada objek Button Lima

    Private Sub btnlima_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlima.Click
        If Text1.Text = "0" Then
            Text1.Text = "5"
        Else : Text1.Text = Text1.Text & "5"

        End If
    End Sub

Klik dua kali pada objek Button Enam

    Private Sub btnenam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenam.Click
        If Text1.Text = "0" Then
            Text1.Text = "6"
        Else : Text1.Text = Text1.Text & "6"

        End If
    End Sub

Klik dua kali pada objek Button Tujuh

    Private Sub btntujuh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntujuh.Click
        If Text1.Text = "0" Then
            Text1.Text = "7"
        Else : Text1.Text = Text1.Text & "7"

        End If
    End Sub

Klik dua kali pada objek Button Delapan

    Private Sub btndelapan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelapan.Click
        If Text1.Text = "0" Then
            Text1.Text = "8"
        Else : Text1.Text = Text1.Text & "8"

        End If
    End Sub

Klik dua kali pada objek Button Sembilan

    Private Sub btnsembilan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsembilan.Click
        If Text1.Text = "0" Then
            Text1.Text = "9"
        Else : Text1.Text = Text1.Text & "9"

        End If
    End Sub

Klik dua kali pada objek Button Koma

    Private Sub btnkoma_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkoma.Click
        If Text1.Text = "0" Then
            Text1.Text = "."
        Else : Text1.Text = Text1.Text & "."

        End If
    End Sub

Klik dua kali pada objek Button Off

    Private Sub btnoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnoff.Click
        If btnoff.Text = "&ON" Then
            hidup()
            btnoff.Text = "&OFF"
            btnoff.BackColor = Color.Red
            btnoff.ForeColor = Color.White
        Else
            mati()
            btnoff.Text = "&ON"
            btnoff.BackColor = Color.Green
            btnoff.ForeColor = Color.White
        End If
    End Sub

Klik dua kali pada objek Button Plus (Tambah)

    Private Sub btnplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus.Click
        If nilai = 0 Then
            nilai = Val(Text1.Text)
        Else : nilai = nilai + Val(Text1.Text)
        End If
        rumus = "+"
        Text1.Text = Nothing
        Text1.Focus()
    End Sub

Klik dua kali pada objek Button Minus (Pengurangan)

    Private Sub btnmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmin.Click
        If nilai = 0 Then
            nilai = Val(Text1.Text)
        Else : nilai = nilai - Val(Text1.Text)
        End If
        rumus = "-"
        Text1.Text = Nothing
        Text1.Focus()
    End Sub

Klik dua kali pada objek Button Bagi

    Private Sub btnbagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbagi.Click
        If nilai = 0 Then
            nilai = Val(Text1.Text)
        Else : nilai = nilai / Val(Text1.Text)
        End If
        rumus = "/"
        Text1.Text = Nothing
        Text1.Focus()
    End Sub

Klik dua kali pada objek Button Kali

    Private Sub btnkali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkali.Click
        If nilai = 0 Then
            nilai = Val(Text1.Text)
        Else : nilai = nilai * Val(Text1.Text)
        End If
        rumus = "*"
        Text1.Text = Nothing
        Text1.Focus()
    End Sub

Klik dua kali pada objek Button pangkat

    Private Sub btnpangkat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpangkat.Click
        If nilai = 0 Then
            nilai = Val(Text1.Text)
        Else : nilai = nilai ^ Val(Text1.Text)
        End If
        rumus = "^"
        Text1.Text = Nothing
        Text1.Focus()
    End Sub

Klik dua kali pada objek Button Akar

    Private Sub btnakar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnakar.Click
        Text1.Text = Math.Sqrt(Text1.Text)
    End Sub

Klik dua kali pada objek button sama dengan

    Private Sub btnsamadengan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsamadengan.Click
        If rumus = "+" Then
            hasil = nilai + Val(Text1.Text)
            Text1.Text = hasil
        ElseIf rumus = "-" Then
            hasil = nilai - Val(Text1.Text)
            Text1.Text = hasil
        ElseIf rumus = "*" Then
            hasil = nilai * Val(Text1.Text)
            Text1.Text = hasil
        ElseIf rumus = "/" Then
            hasil = nilai / Val(Text1.Text)
            Text1.Text = hasil
        ElseIf rumus = "^" Then
            hasil = nilai ^ Val(Text1.Text)
            Text1.Text = hasil

        End If
    End Sub

Klik dua kali pada objek Button1 (Close)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Close()
    End Sub

Klik dua kali pada objek Form kemudian rubah eventnya menjadi Activated

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        mati()
        btnoff.BackColor = Color.Green
        btnoff.ForeColor = Color.White
    End Sub

Klik dua kali pada objek Text1.text kemudian rubah eventnya menjadi keypress

    Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
        If (e.KeyChar = Chr(13)) Then
            If rumus = "+" Then
                hasil = nilai + Val(Text1.Text)
                Text1.Text = hasil
            ElseIf rumus = "-" Then
                hasil = nilai - Val(Text1.Text)
                Text1.Text = hasil
            ElseIf rumus = "*" Then
                hasil = nilai * Val(Text1.Text)
                Text1.Text = hasil
            ElseIf rumus = "/" Then
                hasil = nilai / Val(Text1.Text)
                Text1.Text = hasil
            ElseIf rumus = "^" Then
                hasil = nilai ^ Val(Text1.Text)
                Text1.Text = hasil
            End If
        End If
    End Sub

End Class

Demikian cara membuat aplikasi Calculator di VB.NET, terimakasih sudah berkunjung, semoga bermanfaat.
Membuat Calculator di VB.NET 10 Membuat Calculator di VB.NET 10 Reviewed by My Profile on 5:58 PM Rating: 5

No comments:

Note: Only a member of this blog may post a comment.

Powered by Blogger.