- coba buat new preject di Visual Studio
- New Windows Application di Visual Basic nya
- Tambahkan satu textbox pada form dan beri nama txtHarga
- Double klik textboxnya atau beri event TextChange coding ini di dalam event textchage nya :
1: If txtHarga.Text.Length > 0 Then
2: If txtHarga.Text.Substring(0, 1) = "." Then txtHarga.Text = txtHarga.Text.Replace(".", "")
3:
4: Dim x As Double = CDbl(txtHarga.Text.Replace(",", ""))
5: Dim s As String = Strings.FormatNumber(x, 0)
6:
7: If txtHarga.Text <> s Then
8: Dim pos As Integer = txtHarga.Text.Length - txtHarga.SelectionStart
9:
10: txtHarga.Text = s
11:
12: If ((txtHarga.Text.Length - pos) < 0) Then
13: txtHarga.SelectionStart = 0
14: Else
15: txtHarga.SelectionStart = txtHarga.Text.Length - pos
16: End If
17: End If
18: End if
- Tambahkan event Keypress juga pada txtHarga dan coding ini di dalam event textKeyPress nya :
1: e.Handled = ValidAngka(e) - lalu kita buat Function ValidAngka :
1: Private Function ValidAngka(ByVal e As System.Windows.Forms.KeyPressEventArgs) As Boolean
2: Dim strValid As String = "0123456789"
3:
4: If Strings.InStr(strValid, e.KeyChar) = 0 And Not (e.KeyChar = Strings.Chr(Keys.Back)) Then
5: Return True
6: Else
7: Return False
8: End If
9: End Function
Nah itulah yang dapat di share pada kesempatan ini, [:D]

0 komentar:
Posting Komentar