กำหนดให้ TextBox รับค่าจาก Keyboard เป็นตัวเลขเท่านั้น
Private Sub txt_price_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_price.KeyPress
Select Case Asc(e.KeyChar)
Case 48 To 57
e.Handled = False
Case 8, 3, 22 ' Backspace = 8, Enter = 13, Delete = 46 ,Ctrl+C=3,Ctrl+V=22
e.Handled = False
Case 13
e.Handled = True 'SendKeys.Send("{TAB}")
Case 46 'Delete = 46 ,.
If txt_price.Text.IndexOf(".") <> -1 Then
e.Handled = True
Else
e.Handled = False
End If
Case Else
e.Handled = True
End Select
End Sub
Private Sub txt_price_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_price.Leave
If txt_price.Text <> "" Then
txt_price.Text = Convert.ToDouble(txt_price.Text).ToString("#,###.#0")
End If
End Sub
Private Sub txt_price_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_price.KeyPress
Select Case Asc(e.KeyChar)
Case 48 To 57
e.Handled = False
Case 8, 3, 22 ' Backspace = 8, Enter = 13, Delete = 46 ,Ctrl+C=3,Ctrl+V=22
e.Handled = False
Case 13
e.Handled = True 'SendKeys.Send("{TAB}")
Case 46 'Delete = 46 ,.
If txt_price.Text.IndexOf(".") <> -1 Then
e.Handled = True
Else
e.Handled = False
End If
Case Else
e.Handled = True
End Select
End Sub
Private Sub txt_price_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_price.Leave
If txt_price.Text <> "" Then
txt_price.Text = Convert.ToDouble(txt_price.Text).ToString("#,###.#0")
End If
End Sub