在 C# 中
private bool ValidChar(string _char)
{
string Lista = @" ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ";
return Lista.IndexOf(_char.ToUpper()) != -1;
}
private void textBoxDescripcion_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!ValidChar(e.Text))
e.Handled = true;
}
在VB中
Private Function ValidChar(_char As String) As Boolean
Dim Lista As String = " ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
Return Lista.IndexOf(_char.ToUpper()) <> -1
End Function
Private Sub textBoxDescripcion_PreviewTextInput(sender As Object, e As TextCompositionEventArgs)
If Not ValidChar(e.Text) Then
e.Handled = True
End If
End Sub