学习首页 百科 人生课堂 办公软件 英语学习 操作系统 故事会 编程资料 软件学习 设计
铭瑶网 >> 学习首页 >> 软件 >> VB.NET中让Textbox只能输入数字
标题:VB.NET中让Textbox只能输入数字

【字体: 】 时间:2008-4-11 来源:互联网 作者:study

VB.NET中让Textbox只能输入数字

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp If (e.Keyvalue > 47 And e.Keyvalue < 58) Or (e.Keyvalue > 95 And e.Keyvalue < 106) Or (e.Keyvalue = 8) Or (e.Keyvalue = 45) Or (e.Keyvalue = 46) Then str = TextBox1.Text Else TextBox1.Text = str TextBox1.Focus() End If End Sub  


限制文本框的输入(只输入数字)


文本框防止非法字符输入: 
只输入整数: Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Asc("0") To Asc("9"), vbKeyBack 'nop Case Else KeyAscii = 0 End Select End Sub 
只输入小数: 
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Asc("0") To Asc("9"), vbKeyBack 'nop 
case Asc(".")'允许一个小数点 If InStr(1, Text1.Text, ".") > 0 Then KeyAscii = 0 
Case Else KeyAscii = 0 
End Select End Sub 
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim num As Integer = 0 Dim temp As String = "" While num < TextBox1.Text.Length temp += "[0-9]" num += 1 End While 
If Not (TextBox1.Text Like temp) Then TextBox1.Text = str TextBox1.Focus() Else str = TextBox1.Text End If End Sub End Class 

查看/参与:讨论/评论 相关文章:Vb.net