Hendra Arifin Siregar
Monday, July 22, 2013
Berikut merupakan program vb net agar textbox hanya menerima angka :
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tombol As Integer = Asc(e.KeyChar)
If Not (((tombol >= 48) And (tombol <= 57)) Or (tombol = 8)) Then
e.Handled = True
End If
If Not (((tombol >= 48) And (tombol <= 57)) Or (tombol = 8)) Then
e.Handled = True
End If
End Sub
End Class
Penjelasan :
Tombol merupakan variabel untuk menerima keyboard yang ditekan
Asc(e.Keychar) merupakan perintah untuk merubah nilai tombol keyboard yang ditekan ke nilai ASCII
Tombol >=48 agar tombol diatas sama dengan 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Tombol <=57 merupakan tombol yang sama atau di bawah angka 9
Tombol = 8 merupakan tombol Backspace
Subscribe to:
Posts (Atom)