Visual Basic 帮助 - Visual Basic 文本框中的文本限制,以防止输入某些字符
Visual Basic Help - Text limitations in a visual basic text box to prevent certain characters being entered
这是一件简单的事情,我已经尝试了一段时间,现在却开始烦恼了。我只想在按下按钮时只允许某些值出现在文本框中。这是什么意思,例如,仅允许在文本框中使用 "abc123!",如果说 "w" 等值,则清除文本框。
我已经尝试过 'If Not Regex.Match' 之类的方法,但它只会导致我出错。
请帮忙 ;)
您可能想要使用白名单。您允许的字符列表将比现有的所有其他字符小得多。您可以通过几种方式做到这一点。您可以处理文本框上的按键事件,如果该值是任何值,则执行您的代码。您可以执行此操作的另一种方法(假设它是一个 winforms 应用程序)是从文本框继承并将您的代码放在那里(然后您可以重新使用此控件)。下面是一个只允许数字输入的文本框示例:
''' <summary>
''' Text box that only accepts numeric values.
''' </summary>
''' <remarks></remarks>
Public Class NumericTextBox
Inherits TextBox
Private Const ES_NUMBER As Integer = &H2000
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
params.Style = params.Style Or ES_NUMBER
Return params
End Get
End Property
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
'Prevent pasting of non-numeric characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData = (Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String = CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not Char.IsNumber(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
ElseIf keyData = (Keys.Control Or Keys.A) Then
' Process the select all
Me.SelectAll()
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
End Class
如果您只想使用一个 TextBox 和一个 KeyPress 事件,您可以这样做。我的白名单中只有两个字符,你想要包含所有你想要包含的字符:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
' Test white list, this is only 0 and 1 which are ASCII 48 and 49
Dim allowedChars() As String = {Chr(48), Chr(49)}
If allowedChars.Contains(e.KeyChar) Then
' Setting handled to true stops the character from being entered, remove this or execute your code
' here that you want
e.Handled = True
End If
End Sub
如果您想要字符代码列表,可以在此处获取它们:
希望这对您有所帮助。 ;-)
这是一件简单的事情,我已经尝试了一段时间,现在却开始烦恼了。我只想在按下按钮时只允许某些值出现在文本框中。这是什么意思,例如,仅允许在文本框中使用 "abc123!",如果说 "w" 等值,则清除文本框。
我已经尝试过 'If Not Regex.Match' 之类的方法,但它只会导致我出错。
请帮忙 ;)
您可能想要使用白名单。您允许的字符列表将比现有的所有其他字符小得多。您可以通过几种方式做到这一点。您可以处理文本框上的按键事件,如果该值是任何值,则执行您的代码。您可以执行此操作的另一种方法(假设它是一个 winforms 应用程序)是从文本框继承并将您的代码放在那里(然后您可以重新使用此控件)。下面是一个只允许数字输入的文本框示例:
''' <summary>
''' Text box that only accepts numeric values.
''' </summary>
''' <remarks></remarks>
Public Class NumericTextBox
Inherits TextBox
Private Const ES_NUMBER As Integer = &H2000
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
params.Style = params.Style Or ES_NUMBER
Return params
End Get
End Property
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
'Prevent pasting of non-numeric characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData = (Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String = CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not Char.IsNumber(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
ElseIf keyData = (Keys.Control Or Keys.A) Then
' Process the select all
Me.SelectAll()
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
End Class
如果您只想使用一个 TextBox 和一个 KeyPress 事件,您可以这样做。我的白名单中只有两个字符,你想要包含所有你想要包含的字符:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
' Test white list, this is only 0 and 1 which are ASCII 48 and 49
Dim allowedChars() As String = {Chr(48), Chr(49)}
If allowedChars.Contains(e.KeyChar) Then
' Setting handled to true stops the character from being entered, remove this or execute your code
' here that you want
e.Handled = True
End If
End Sub
如果您想要字符代码列表,可以在此处获取它们:
希望这对您有所帮助。 ;-)