主题:[原创]关于Vb6.0中keyascii在.net中的底层实现
做了一个控件.....作用是只可以输入数字.点号和"-"号
Public Class MyNumerricTextBox'----声明自已的控件类
Inherits System.Windows.Forms.TextBox'----从textbox继承
Private Sub MyNumerricTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
'--------自定义事件
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57
Case 45
'If Microsoft.VisualBasic.Strings.InStr(Me.Text) <> 0 Then
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
End Class
Public Class MyNumerricTextBox'----声明自已的控件类
Inherits System.Windows.Forms.TextBox'----从textbox继承
Private Sub MyNumerricTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
'--------自定义事件
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57
Case 45
'If Microsoft.VisualBasic.Strings.InStr(Me.Text) <> 0 Then
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
End Class