Jumat, 22 Februari 2013

cara membuat kalkulator melalui visual basic bersama sugeng

cara membuat kalkulator melalui visual basic

Program kalkulator adalah program standar yang sudah tersedia di Microsoft Windows. Tetapi, tak ada salahnya jika kita mencoba membuat kalkulator sendiri dengan Microsoft Visual Basic 6.0.
Pertama-tama, buatlah sebuah form sesuai selera Anda, yang bentuknya kurang lebih seperti
kalkulator standar Windows, seperti ini:




Untuk properties dari masing-masing objek, saya contohkan seperti dibawah ini, dengan letak
masing-masing objek disesuaikan dengan gambar diatas.
Textbox 1:
(Name) : txtDisplay
Alignment: 1-Right Justify
Text : (kosongkan)
Maxlength : 12

Command Button 1:
(Name) : cmdAngka
Index : 1
Caption : 1

Command Button 2:
(Name) : cmdAngka
Index : 2
Caption : 2

Command Button 3:
(Name) : cmdAngka
Index : 3
Caption : 3

Command Button 4:
(Name) : cmdAngka
Index : 4
Caption : 4

Command Button 5:
(Name) : cmdAngka
Index : 5
Caption : 5

Command Button 6:
(Name) : cmdAngka
Index : 6
Caption : 6

Command Button 7:
(Name) : cmdAngka
Index : 7
Caption : 7

Command Button 8:
(Name) : cmdAngka
Index : 8
Caption : 8

Command Button 9:
(Name) : cmdAngka
Index : 9
Caption : 9

Command Button 10:
(Name) : cmdAngka
Index : 0
Caption : 0

Command Button 11:
(Name) : cmdPlusMinus
Caption : +/-

Command Button 12:
(Name) : cmdKoma
Caption : .

Command Button 13:
(Name) : cmdOperator
Index : 1
Caption : +

Command Button 14:
(Name) : cmdOperator
Index : 2
Caption : -

Command Button 15:
(Name) : cmdOperator
Index : 3
Caption : x

Command Button 16:
(Name) : cmdOperator
Index : 4
Caption : /

Command Button 17:
(Name) : cmdClearEntry
Caption : CE

Command Button 18:
(Name) : cmdClear
Caption : C

Command Button 19:
(Name) : cmdSamaDengan
Caption : =

Berikutnya, Anda tinggal membuat coding dari kalkulator tersebut. Jika Anda mengikuti semua
setting properties seperti diatas, maka codingnya saya contohkan seperti ini:

Option Explicit
Private Hasil As Double
Private Const opNol = 0
Private Const opTambah = 1
Private Const opKurang = 2
Private Const opKali = 3
Private Const opBagi = 4
Private Operator As Integer
Private NilaiBaru As Boolean

' untuk menghapus karakter terakhir
Private Sub Hapus()
Dim txt As String
Dim min_len As Integer
txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If
If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub

' hapus angka, hasil terakhir dan operator
Private Sub cmdClear_Click()
cmdClearEntry_Click
Hasil = 0
Operator = opNol
End Sub
' hapus angka
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub
' menambahkan koma (desimal)
Private Sub cmdKoma_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NilaiBaru Then
txtDisplay.Text = "."
NilaiBaru = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub

' Menghitung
Private Sub cmdSamaDengan_Click()
Dim HasilBaru As Double
If txtDisplay.Text = "" Then
HasilBaru = 0
Else
HasilBaru = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNol
Hasil = HasilBaru
Case opTambah
Hasil = Hasil + HasilBaru
Case opKurang
Hasil = Hasil - HasilBaru
Case opKali
Hasil = Hasil * HasilBaru
Case opBagi
'Tidak bisa dibagi nol
If HasilBaru = 0 Then
MsgBox "Tidak bisa dibagi 0", vbOKOnly + vbCritical, "Kalku ERROR"
Call cmdClear_Click
Else
Hasil = Hasil / HasilBaru
End If
End Select
Operator = opNol
NilaiBaru = True
txtDisplay.Text = Format$(Hasil)
End Sub

' menuliskan angka
Private Sub cmdAngka_Click(Index As Integer)
If NilaiBaru Then
txtDisplay.Text = Format$(Index)
NilaiBaru = False
Else
txtDisplay.Text = _
txtDisplay.Text & Format$(Index)
End If
End Sub
' tombol tambah/kurang/bagi/kali
Private Sub cmdOperator_Click(Index As Integer)
cmdSamaDengan_Click
Operator = Index
NilaiBaru = True
End Sub

' merubah tanda +/-
Private Sub cmdPlusMinus_Click()
If NilaiBaru Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub

' filter untuk angka saja yg dapat diketikkan
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
' supaya kursor tetap di kanan
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub

' untuk mengetikkan angka di keyboard
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdAngka_Click 0
Case "1"
cmdAngka_Click 1
Case "2"
cmdAngka_Click 2
Case "3"
cmdAngka_Click 3
Case "4"
cmdAngka_Click 4
Case "5"
cmdAngka_Click 5
Case "6"
cmdAngka_Click 6
Case "7"
cmdAngka_Click 7
Case "8"
cmdAngka_Click 8
Case "9"
cmdAngka_Click 9
Case "*", "x", "X"
cmdOperator_Click opKali
Case "+"
cmdOperator_Click opTambah
Case vbCrLf, vbCr, "="
cmdSamaDengan_Click
Case "-"
cmdOperator_Click opKurang
Case "."
cmdKoma_Click
Case "/"
cmdOperator_Click opBagi
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub

' untuk ketikan angka di numpad
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdAngka_Click 0
Case vbKeyNumpad1
cmdAngka_Click 1
Case vbKeyNumpad2
cmdAngka_Click 2
Case vbKeyNumpad3
cmdAngka_Click 3
Case vbKeyNumpad4
cmdAngka_Click 4
Case vbKeyNumpad5
cmdAngka_Click 5
Case vbKeyNumpad6
cmdAngka_Click 6
Case vbKeyNumpad7
cmdAngka_Click 7
Case vbKeyNumpad8
cmdAngka_Click 8
Case vbKeyNumpad9
cmdAngka_Click 9
Case vbKeyMultiply
cmdOperator_Click opKali
Case vbKeyAdd
cmdOperator_Click opTambah
Case vbKeySeparator
cmdSamaDengan_Click
Case vbKeySubtract
cmdOperator_Click opKurang
Case vbKeyDivide
cmdOperator_Click opBagi
Case vbKeyDecimal
cmdKoma_Click
Case vbKeyBack, vbKeyDelete
Hapus
End Select
KeyCode = 0
End Sub

0 comments:

Posting Komentar

Labels

Harga Laptop Terbaru Ponsel Notebook Tips dan Trik Tablet Aksesoris Komputer tips and trik informasi LENOVO Download Lirik Lagu NETBOOK Harga Samsung blogger Daftar Harga Tutorial HP trik facebook download software ASUS GADGET APPLE SAMSUNG TABLET PC TOSHIBA ACER Berita Hari Ini Kamera SONY Harga Tablet samsung Review Tutorial Blog AXIOO Printer Acer Android Apple Harga Acer Harga Lenovo Motherboard berita terkini library science AKSESORIS LAPTOP E-book FUJITSU Harga Blackberry Harga Nokia Harga VGA Processor SOFTWARE Samsung Galaxy Software Teknik Komputer dan Jaringan Tips Komputer buku panduan seo software ANDROID Belajar Web design DELL Gadget Harga Harddisk Harga Laptop Acer Harga Laptop Lenovo SEO situs internet ALIENWARE Dell Facebook Harga Kamera Harga Laptop Asus Harga Nokia Lumia Hp Java Nama Bayi teknologi Advan Harga HP Mito Harga Hp Cross Harga Kamera DSLR Canon Harga Laptop Harga Laptop Fujitsu Harga Laptop Toshiba Harga Processor Intel Teknologi ZYREX trik komputer Blog Tool Harga LCD Monitor Harga Laptop Dell Harga Laptop Samsung Harga Laptop Sony Vaio Harga Mobil Harga Sembako Harga Smartfren Harga Speaker Aktif Harga Tablet Asus Harga Tablet Mito Huawei IPHONE Internet LG Laptop Nokia SMART PHONE Samsung Speaker info pengalaman ADAPTER Archos BATERRY LAPTOP BLACKBERRY Blogging Canon Google Harga HP Harga Laptop Apple Harga Power Supply Harga Printer Epson Harga Printer HP Harga Processor AMD Harga Sony Xperia Harga Tablet Acer Harga Tablet Advan Lowongan Kerja Materi Komputer News Technology Samsung Ativ Smartfren Tablet Murah motivasi widget blog $ Dollar Gratiss ACER ASPIRE Bisnis Online BlackBerry Blackberry Chatting Cross Education Game HARDWARE Hard Disk Harga HP Baru Harga Hp Android Harga Kamera DSLR Nikon Harga Kamera DSLR Sony Harga Kamera SLR Nikon Harga Laptop Advan Harga Laptop Baru Harga Mobil Baru Harga Mobil Bekas Harga Motherboard AMD AM2 Harga Motherboard AMD AM3 Harga Motherboard Intel 1156 Harga Motherboard Intel 1366 Harga Motherboard Intel LGA Harga Nokia Asha Harga Power Bank Harga Printer Canon Harga iPhone Hisense KATA-KATA Kata-kata lucu Kontes SEO Lenovo MEMORY LAPTOP MSI Mito Nikon Otomotif Pengetahuan Sony Toshiba ULTRABOOK Unik Zodiak linux musik terbaru tips dan trick ATI Acer Iconia W4 Al Quran Mp3 Artikel Asus Baterai Black2 Bola Mania CD Burner Carrefour Display ECS EUROCOM Enermax Fakta Film Fujitsu Game PC Gigabyte Harga ATK Harga Archos 97b Platinum Harga Asus Memo Pad Smart Harga CCTV Harga Emas Harga HP Bekas Harga HP Terbaru Harga Hp LG Harga IMO Harga Laptop HP Harga Lenovo A706 Harga Micromax Harga Oppo Harga PlayStation 4 Harga Ponsel Zopo Harga Printer Brother Harga Printer Murah Harga Produk Pakai Harga Promo Harga Samsung Galaxy S4 Nexus Harga Stabilizer Harga TV LED Harga Tabklet Archos Arnova Harga Tablet Cyrus Harga Tablet Esia AirTab Harga Tablet Maxtron Harga Tablet Microsoft Surface Harga Tablet Smartfren Harga Tablet lenovo Harga UPS Harga Ultrabook Islam Islami Jadwal Imsakiyah Ramadhan Kingston Flash Drive USB 64 GB LumiTab MODECOM Musik Online Origin PC Ou Sheng Pantech Pemrograman PlayStation Port USB Prestigio Puisi Ramos i10 Pro Razer Sebuah Pemikiran Sharp Sharp RW-16G1 Sonny Ericsson Sony Xperia Z Ultra Speaker Aktif Tablet Firefox OS Tablet Imo Tablet Zinox Thosiba Tips Trik Tips n Trik Gratis Tutorial Wordpress Ultrabook VELOCE VGA Asus VGA Digital Alliance VGA Inno 3D VGA PixelView Voyo WeWi XiaoMi ZBOX 01520 game kuliah link exchange music pendidikan pkl seo blogger tips-triks trick blogger