free counters
Diberdayakan oleh Blogger.

Rabu, 09 Mei 2012

Mengubah Text di Textbox ke dalam Format Uang secara Real Time

by Unknown  |  in Visual Basic 6 at  Rabu, Mei 09, 2012

Tutorial singkat ini menggunakan VB6.

Untuk mengubah text di textbox ke dalam format uang ternyata sangatlah mudah dilakukan. Hanya perlu beberapa baris kode di procedure KeyPress dan Change.
Ok, mari kita mulai.

1. buat form kosong di project vb.
2. tambahkan component textbox dan beri nama sebagai txtPrice
3. Masuk ke bagian kode, dan buat Procedure  txtPrice_KeyPress dan  txtPrice_Change seperti koding di bawah.



Private Sub txtPrice_Change()
    If (Len(txtPrice.Text) > 0) Then
      txtPrice.Text = FormatNumber(OriginalNum(txtPrice.Text), 0)
   End If
   txtPrice.SelStart = Len(txtPrice.Text)
End Sub



Private Sub txtPrice_KeyPress(KeyAscii As Integer)
   If (txtPrice.Text = "" Or txtPrice.Text = "0") Then
      txtPrice.Text = ""
   End If
   If (KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = vbKeyBack Or KeyAscii = vbKeyDelete) And Not (KeyAscii = 13) And Not (KeyAscii = 46) Then


   Else
      KeyAscii = 0
   End If

   If (Len(txtPrice.Text) > 0) Then
      If (InStr(txtPrice.Text, ".") <> 0) Then
         txtPrice.Text = Replace$(txtPrice.Text, ".", "")
      End If
   End If
End Sub




4. Tambahkan fungsi OriginalNum



Public Function OriginalNum(str) As String
   'test dulu
   test = "100"
   testfmt = FormatNumber(test, 2)
   
   temp = str
   If InStr(testfmt, ",") > 0 Then 'format indonesia
      OriginalNum = Replace(temp, ".", "")
   ElseIf InStr(testfmt, ".") > 0 Then 'format english
      OriginalNum = Replace(temp, ",", "")
   End If
End Function



5. Kemudian test.
6. Finish.


Selamat Berkoding Ria....:D.

2 komentar:

  1. alhamdulillah nemu juga script nya :D mkasi yaaa :) pokonya sukses terusssssss ^_^

    BalasHapus
    Balasan
    1. Sama-sama... Ada beberapa kekurangan dari code di atas, tapi saya gak ingat apa. Gak megang VB6 lagi sekarang, udah megang Qt. :)

      Hapus

Silahkan tinggalkan komentar anda: