HENDIKA

Sunday, 5 May 2013

Assalamu'alaikum Wr Wb

Dalam Proses Pembelajaran Pembuatan Program Aplikasi Komputer. Biasanya para newbie ditugaskan untuk membuat aplikasi sederhana seperti membuat kalkulator. pada posting ini saya akan memberikan contoh untuk membuat aplikasi kalkulator menggunakan VB.Net.

Langkah - langkahnya yaitu :

1. Buka Microsoft Visual Studio 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

0 komentar:

Post a Comment