Public Function CheckDigitOnly(ByVal index As Integer) As Boolean
Select Case index
Case 47 To 58 ' เลข 0 - 9
CheckDigitOnly = False
Case 8, 13, 46 ' Backspace = 8, Enter = 13, Delete = 46
CheckDigitOnly = False
Case Else
CheckDigitOnly = True
End Select
End Function
'Use
TextBox1_KeyPress
e.Handled = CheckDigitOnly(Asc(e.KeyChar))
'เป็นฟังก์ชันครับ สามารถสร้าง Class ได้แล้วให้เรียกใช้เอานะครับ
Option Explicit On
Option Strict On
Public Class CurrencyTextBox
Public Shared Function CurrencyOnly(ByVal TargetTextBox As TextBox, ByVal CurrentChar As Char) As Boolean
If IsNumeric(CurrentChar) = True Then
Return False
End If
If CBool(((Convert.ToString(CurrentChar) = "." AndAlso CBool(InStr(TargetTextBox.Text, "."))))) Then
Return True
End If
If Convert.ToString(CurrentChar) = "." OrElse CurrentChar = vbBack Then
Return False
End If
Return True
End Function
Public Shared Function StringOnly(ByVal TargetTextBox As TextBox, ByVal CurrentChar As Char) As Boolean
If IsNumeric(CurrentChar) = True Then
Return True
End If