TUTORIAL CARA MEMBUAT KALKULATOR SEDERHANA PADA VISUAL BASIC 2010

24 Nov

Visual Basic 2010 biasa digunakan para programmer untuk membuat suatu aplikasi yang dapat memudahkan untuk memecahkan suatu masalah. Dan kali ini kami akan membahas cara-cara membuat kalkulator sederhana dengan menggunakan Visual Basic 2010.

  1. Buka VB 2010 >> New Project >> Windows Forms Application >> OK.
  2. Buat desain seperti gambar dengan memasukkan komponen dari toolbox ke dalam form design.

 3. Ubah nama komponen yang telah dibuat dari bagian properties (di kotak yang ada di pinggir kanan)

Toolbox component

Nama

Button 1 1
Button 2 2
Button 3 3
Button 4 4
Button 5 5
Button 6 6
Button 7 7
Button 8 8
Button 9 9
Button 10 0
Button 11 .
Button 12 +
Button 13 -
Button 14 *
Button 15 /
Button 16 C
Button 17 SIN
Button 18 COS
Button 19 TAN
Button 20 ^
Button 21 =
Button 22 ON
Button 23 OFF
Button 24 Akar
Button 25 Keluar
Button 26 +/-
Button 27 1/x
Button 28 %
TextBox1
Label 1 19

4. Klik 2 kali pada form, lalu masukkan listing code untuk masing-masing komponen, yaitu komponen Button, TextBox dan button. Berikut listing kodenya:

  • Form1 :

 Public Class Form1

Dim Angka, Hasil As Double

Const phi As Double = 22 / 7

Dim Rumus As String

  • Button1 :

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

If TextBox1.Text = “” Then

TextBox1.Text = “1″

Else

TextBox1.Text = TextBox1.Text & “1″

End If

End Sub

  • Button 2

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

If TextBox1.Text = “” Then

TextBox1.Text = “2″

Else

TextBox1.Text = TextBox1.Text & “2″

End If

End Sub

  • Button 3

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

If TextBox1.Text = “” Then

TextBox1.Text = “3″

Else

TextBox1.Text = TextBox1.Text & “3″

End If

End Sub

  • Button 4

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

If TextBox1.Text = “” Then

TextBox1.Text = “4″

Else

TextBox1.Text = TextBox1.Text & “4″

End If

End Sub

  • Button 5

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

If TextBox1.Text = “” Then

TextBox1.Text = “5″

Else

TextBox1.Text = TextBox1.Text & “5″

End If

End Sub

  • Button 6

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

If TextBox1.Text = “” Then

TextBox1.Text = “6″

Else

TextBox1.Text = TextBox1.Text & “6″

End If

End Sub

  • Button 7

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

If TextBox1.Text = “” Then

TextBox1.Text = “7″

Else

TextBox1.Text = TextBox1.Text & “7″

End If

End Sub

  • Button 8

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

If TextBox1.Text = “” Then

TextBox1.Text = “8″

Else

TextBox1.Text = TextBox1.Text & “8″

End If

End Sub

  • Button 9

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

If TextBox1.Text = “” Then

TextBox1.Text = “9″

Else

TextBox1.Text = TextBox1.Text & “9″

End If

End Sub

  • Button 10

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click

If TextBox1.Text = “” Then

TextBox1.Text = “0″

Else

TextBox1.Text = TextBox1.Text & “0″

End If

End Sub

  • Button 11

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

If TextBox1.Text = “” Then

TextBox1.Text = “.”

Else

TextBox1.Text = TextBox1.Text & “.”

End If

End Sub

  • Button 12

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click

If Angka = Nothing Then

Angka = Val(TextBox1.Text)

Else

Angka = Angka + Val(TextBox1.Text)

End If

Rumus = “+”

TextBox1.Text = Nothing

End Sub

  • Button 13

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click

If Angka = Nothing Then

Angka = Val(TextBox1.Text)

Else

Angka = Angka – Val(TextBox1.Text)

End If

Rumus = “-”

TextBox1.Text = Nothing

End Sub

  • Button 14

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click

If Angka = Nothing Then

Angka = Val(TextBox1.Text)

Else

Angka = Angka * Val(TextBox1.Text)

End If

Rumus = “*”

TextBox1.Text = Nothing

End Sub

  • Button 15

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click

If Angka = Nothing Then

Angka = Val(TextBox1.Text)

Else

Angka = Angka / Val(TextBox1.Text)

End If

Rumus = “/”

TextBox1.Text = Nothing

End Sub

  • Button 16

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click

TextBox1.Text = Nothing

Angka = Nothing

Hasil = Nothing

Rumus = Nothing

End Sub

  • Button 17

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click

TextBox1.Text = Math.Sin(TextBox1.Text * (phi / 180))

End Sub

  • Button 18

    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click

TextBox1.Text = Math.Cos(TextBox1.Text * (phi / 180))

End Sub

  • Button 19

    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click

TextBox1.Text = Math.Tan(TextBox1.Text * (phi / 180))

End Sub

  • Button 20

    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click

If Angka = Nothing Then

Angka = Val(TextBox1.Text)

Else

Angka = Angka ^ Val(TextBox1.Text)

End If

Rumus = “^”

TextBox1.Text = Nothing

End Sub

  • Button 21

    Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click

If Rumus = “+” Then

TextBox1.Text = Angka + Val(TextBox1.Text)

ElseIf Rumus = “-” Then

TextBox1.Text = Angka – Val(TextBox1.Text)

ElseIf Rumus = “*” Then

TextBox1.Text = Angka * Val(TextBox1.Text)

ElseIf Rumus = “/” Then

TextBox1.Text = Angka / Val(TextBox1.Text)

ElseIf Rumus = “^” Then

TextBox1.Text = Angka ^ Val(TextBox1.Text)

End If

End Sub

  • Button 22

Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click

TextBox1.Enabled = True

Button1.Enabled = True

Button2.Enabled = True

Button3.Enabled = True

Button4.Enabled = True

Button5.Enabled = True

Button6.Enabled = True

Button7.Enabled = True

Button8.Enabled = True

Button9.Enabled = True

Button10.Enabled = True

Button11.Enabled = True

Button12.Enabled = True

Button13.Enabled = True

Button14.Enabled = True

Button15.Enabled = True

Button16.Enabled = True

Button17.Enabled = True

Button18.Enabled = True

Button19.Enabled = True

Button20.Enabled = True

Button21.Enabled = True

Button22.Enabled = True

Button23.Enabled = True

Button24.Enabled = True

End Sub

  • Button 23

      Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click

TextBox1.Enabled = False

Button1.Enabled = False

Button2.Enabled = False

Button3.Enabled = False

Button4.Enabled = False

Button5.Enabled = False

Button6.Enabled = False

Button7.Enabled = False

Button8.Enabled = False

Button9.Enabled = False

Button10.Enabled = False

Button11.Enabled = False

Button12.Enabled = False

Button13.Enabled = False

Button14.Enabled = False

Button15.Enabled = False

Button16.Enabled = False

Button17.Enabled = False

Button18.Enabled = False

Button19.Enabled = False

Button20.Enabled = False

Button21.Enabled = False

Button23.Enabled = False

Button24.Enabled = False

End Sub

  • Button 24

   Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click

TextBox1.Text = Math.Sqrt(TextBox1.Text)

End Sub

  • Button 25

    Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click

If MsgBox(“Keluar dulu ya”) Then

Close()

End If

End Sub

  • Button 26

  Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click

TextBox1.Text = -1 * (TextBox1.Text)

End Sub

  • Button 27

  Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button27.Click

TextBox1.Text = 1 / (TextBox1.Text)

End Sub

  • Button 28

  Private Sub Button28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button28.Click

TextBox1.Text = (TextBox1.Text) / 100

End Sub

End Class

5. Berikut ini adalah bentuk akhir dari kalkulator sederhana yang telah dibuat:

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: