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
给你个匹配正则表达式的例子:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
// Define a regular expression for currency values.
Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
// Define some test strings.
string[] tests = {"-42", "19.99", "0.001", "100 USD"};
// Check each test string against the regular expression.
foreach (string test in tests)
{
if (rx.IsMatch(test))
{
Console.WriteLine("{0} is a currency value.", test);
}
else
{
Console.WriteLine("{0} is not a currency value.", test);
}
}