展开全部
'只能输入时间格式只可输入格式为“××-××-××(日期格式)”
Private Sub Text1_Change()
t = Text1.Text
If Len(t) > 8 Then t = Left(t, 8): Text1.Text = t '超过8为无效
For i = 1 To Len(t) '逐字比较
p = Mid(t, i, 1)
If i = 1 Then If Not (p >= "0" And p <= "2") Then Text1.Text = "": Exit Sub '第一位数字0,1,2
If i = 4 Or i = 7 Then If Not (p >= "0" And p <= "5") Then Text1.Text = Left(t, i - 1): Exit Sub
If i = 2 Or i = 5 Or i = 8 Then If Not (p >= "0" And p <= "9") Then Text1.Text = Left(t, i - 1): Exit Sub
If i = 3 Or i = 6 Then If Not (p = "-" Or p = "." Or p = ":") Then Text1.Text = Left(t, i - 1): Exit Sub '间隔
Next i
Text1.SelStart = Len(Text1.Text) '光标定位到最后
End Sub
Private Sub Text1_Change()
t = Text1.Text
If Len(t) > 8 Then t = Left(t, 8): Text1.Text = t '超过8为无效
For i = 1 To Len(t) '逐字比较
p = Mid(t, i, 1)
If i = 1 Then If Not (p >= "0" And p <= "2") Then Text1.Text = "": Exit Sub '第一位数字0,1,2
If i = 4 Or i = 7 Then If Not (p >= "0" And p <= "5") Then Text1.Text = Left(t, i - 1): Exit Sub
If i = 2 Or i = 5 Or i = 8 Then If Not (p >= "0" And p <= "9") Then Text1.Text = Left(t, i - 1): Exit Sub
If i = 3 Or i = 6 Then If Not (p = "-" Or p = "." Or p = ":") Then Text1.Text = Left(t, i - 1): Exit Sub '间隔
Next i
Text1.SelStart = Len(Text1.Text) '光标定位到最后
End Sub
展开全部
有好多方法
在TextBox控件的KeyPress事件里边检测键盘按键是不是有效字符:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string availability_Chars = "1234567890-."; //有效字符
if (!availability_Chars.Contains(e.KeyChar))
{
e.Handled = true; //非法字符,不处理
}
}
在TextChanged事件里边,检测格式是否正确.
regex = new Regex(@"^\d{2}([.|-]\d{2}){1,3}$", RegexOptions.IgnoreCase);
if( !regex.IsMatch((TextBox)sender).Text))
{//格式错误
}
在TextBox控件的KeyPress事件里边检测键盘按键是不是有效字符:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string availability_Chars = "1234567890-."; //有效字符
if (!availability_Chars.Contains(e.KeyChar))
{
e.Handled = true; //非法字符,不处理
}
}
在TextChanged事件里边,检测格式是否正确.
regex = new Regex(@"^\d{2}([.|-]\d{2}){1,3}$", RegexOptions.IgnoreCase);
if( !regex.IsMatch((TextBox)sender).Text))
{//格式错误
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询