PRA KATA
Seringkali pada saat membuat program kita menginginkan hanya angka saja yang boleh di ketik pada suatu TextBox. hal yang sederhana namum cukup membuat kita kehabisan waktu untuk mengexplorenya.. apalagi bagi para pemula.. :).
Starting...
Buatlah project baru, pada form tambahkan sebuah TextBox
Codenya..
Kemudian copy paste kode program berikut ini..
Private nonNumberEntered As Boolean = False
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.Text = Replace(TextBox1.Text, ".", "")
End Sub
Private Function CekIfNonNumber(ByVal Kc As System.Windows.Forms.KeyEventArgs) As Boolean
' Determine whether the keystroke is a number from the top of the keyboard.
If Kc.KeyCode <> Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If Kc.KeyCode <> Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If Kc.KeyCode <> Keys.Back Then
' A non-numerical keystroke was pressed.
' Set the flag to true and evaluate in KeyPress event.
Return True
End If
End If
End If
End Function
' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyDown
' Initialize the flag to false.
nonNumberEntered = CekIfNonNumber(e)
End Sub 'textBox1_KeyDown
' This event occurs after the KeyDown event and can be used
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
e.Handled = nonNumberEntered
End Sub 'textBox1_KeyPress
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
TextBox1.Text = FormatNumber(TextBox1.Text, 0)
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
MsgBox(e.KeyCode)
End Sub
Finally
Ok That's all, selamat mencoba dan mempelajari semoga bermanfaat..
Sumber..
1. Someone di internet.. sudah lama sekali lupa jadinya.. :)
Tidak ada komentar:
Posting Komentar