要文本框中怎样限制汉字的输入
我想在文本框中只能输入数字,结果是除了数字外,汉字也能输入过去.因为汉字是两个字母,并且编码小于0.请问如何控制. 问题点数:50、回复次数:5Top
1 楼mynewpc(水中日月)回复于 2002-10-27 20:48:59 得分 0
只能在textchange事件里进行判断。Top
2 楼neu2002(wangminghai)回复于 2002-10-28 08:26:46 得分 0
Dim tempNum As Long
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If IsNumeric(TextBox1.Text) = False Then
Try
TextBox1.Text = tempNum
Catch ex As System.Exception
End Try
Else
tempNum = TextBox1.Text
End If
Top
3 楼kuailexq2000()回复于 2002-10-28 10:08:22 得分 0
Private Sub txtAuthorID_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAuthorID.Validating
'It is must be first Validate second button_click,why gctlflagarry(2)
Try
ValidateNumeric(txtAuthorID)
Catch ex As Exception
' Cancel the event and select the text to be corrected by the user.
e.Cancel = True
txtAuthorID.Select(0, txtAuthorID.Text.Length)
' Set the ErrorProvider error with the text to display.
Me.ErrorProvider1.SetError(txtAuthorID, ex.Message)
End Try
End Sub
Public Sub ValidateNumeric(ByVal ctlControl As Control)
' The second validate
' Confirm there is text in the control.
' Not remove the sub to modFunLib ,because ErrorProvider1 is private.(after change is to public,protect)
If ctlControl.Text.Length = 0 Then
Throw New Exception("The value is a required field")
Else
If Not IsNumeric(ctlControl.Text) Then
'set the error
Throw New Exception("Please enter a numeric value.")
Else
Me.ErrorProvider1.SetError(ctlControl, "")
End If
End If
End Sub
---------------------------------------------------------------
没什么呀!?Top
4 楼luhanzhang(风雨无阻)回复于 2002-10-28 10:13:20 得分 0
正则表达式:\dTop
5 楼hcz10(方肖.net)回复于 2002-11-02 02:57:16 得分 50
Dim tempNum As String
Dim wei As Integer
Dim flg As Boolean
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "" Then
Exit Sub
End If
If flg = False Then
TextBox1.Text = tempNum
TextBox1.SelectionStart = wei
End If
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
flg = True
If wei = -1 Then wei = 0
tempNum = TextBox1.Text
Dim x As Keys = e.KeyCode
If x = Keys.Back Or x = Keys.Left Then
wei = TextBox1.SelectionStart - 1
ElseIf (x >= Keys.D0 And x <= Keys.D9) Or (x >= Keys.NumPad0 And x <= Keys.NumPad9) Or x = Keys.Right Then
wei = wei + 1
ElseIf x <> Keys.Delete Then
flg = False
End If
End SubTop




