没人解决吗? 哈哈,你把我下面的代码放到textbox1的KeyPress事件中去,你的问题就解决了,自己跟代码,自己理解去。 Try Dim iKey As Integer If e.KeyChar = "" Then Exit Sub End If If IsNumeric(e.KeyChar) = True Then iKey = Val(e.KeyChar) Select Case TextBox1.Text.Length Case 0 If iKey <> 2 Then e.Handled = True Case 1, 2 If iKey <> 0 Then e.Handled = True Case 3 If iKey < 0 Or iKey > 9 Then e.Handled = True Case 4 If iKey < 0 Or iKey > 1 Then e.Handled = True Case 5 If CInt(TextBox1.Text.Substring(4)) = 1 Then If iKey < 0 Or iKey > 2 Then e.Handled = True Else If iKey < 0 Or iKey > 9 Then e.Handled = True End If Case 6 If CInt(TextBox1.Text.Substring(4, 2)) = 2 Then If iKey < 0 Or iKey > 2 Then e.Handled = True Else If iKey < 0 Or iKey > 3 Then e.Handled = True End If Case 7 If TextBox1.Text.Substring(7, 1) = 3 Then If iKey < 0 Or iKey > 1 Then e.Handled = True Else If iKey < 0 Or iKey > 9 Then e.Handled = True End If Case 8 e.Handled = True End Select Else e.Handled = True End If Catch ex As Exception e.Handled = True End Try
为什么不换成DateTimePicker控件呢?
有个小bug,改了一下: Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter And TextBox1.Text.Length = 8 Then MsgBox(TextBox1.Text.Substring(0, 4) & "-" & TextBox1.Text.Substring(4, 2) & "-" & TextBox1.Text.Substring(6, 2)) End If End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Try Dim iKey As Integer If AscW(e.KeyChar) = "8" Then Exit Sub ElseIf AscW(e.KeyChar) = "13" Then Exit Sub End If If IsNumeric(e.KeyChar) = True Then iKey = Val(e.KeyChar) Select Case TextBox1.Text.Length Case 0 If iKey <> 2 Then e.Handled = True Case 1, 2 If iKey <> 0 Then e.Handled = True Case 3 If iKey < 0 Or iKey > 9 Then e.Handled = True Case 4 If iKey < 0 Or iKey > 1 Then e.Handled = True Case 5 If CInt(TextBox1.Text.Substring(4)) = 1 Then If iKey < 0 Or iKey > 2 Then e.Handled = True Else If iKey < 0 Or iKey > 9 Then e.Handled = True End If Case 6 If CInt(TextBox1.Text.Substring(4, 2)) = 2 Then If iKey < 0 Or iKey > 2 Then e.Handled = True Else If iKey < 0 Or iKey > 3 Then e.Handled = True End If Case 7 If TextBox1.Text.Substring(6, 1) = 3 Then If iKey < 0 Or iKey > 1 Then e.Handled = True Else If iKey < 0 Or iKey > 9 Then e.Handled = True End If Case 8 e.Handled = True End Select Else e.Handled = True End If Catch ex As Exception e.Handled = True End Try End Sub
掩码设成0000-00-00
当textbox内容不是规定的格式时,光标就不能移到其他控件就行了。
这样的话,问题就变得简单了,用正则表达式判断就OK了。
写Leave事件,输入内容不合法,则textbox.Focus();
合法,则将其变成yyyy-MM-dd的形式。
没人解决吗?
哈哈,你把我下面的代码放到textbox1的KeyPress事件中去,你的问题就解决了,自己跟代码,自己理解去。
Try
Dim iKey As Integer
If e.KeyChar = "" Then
Exit Sub
End If
If IsNumeric(e.KeyChar) = True Then
iKey = Val(e.KeyChar)
Select Case TextBox1.Text.Length
Case 0
If iKey <> 2 Then e.Handled = True
Case 1, 2
If iKey <> 0 Then e.Handled = True
Case 3
If iKey < 0 Or iKey > 9 Then e.Handled = True
Case 4
If iKey < 0 Or iKey > 1 Then e.Handled = True
Case 5
If CInt(TextBox1.Text.Substring(4)) = 1 Then
If iKey < 0 Or iKey > 2 Then e.Handled = True
Else
If iKey < 0 Or iKey > 9 Then e.Handled = True
End If
Case 6
If CInt(TextBox1.Text.Substring(4, 2)) = 2 Then
If iKey < 0 Or iKey > 2 Then e.Handled = True
Else
If iKey < 0 Or iKey > 3 Then e.Handled = True
End If
Case 7
If TextBox1.Text.Substring(7, 1) = 3 Then
If iKey < 0 Or iKey > 1 Then e.Handled = True
Else
If iKey < 0 Or iKey > 9 Then e.Handled = True
End If
Case 8
e.Handled = True End Select
Else
e.Handled = True
End If Catch ex As Exception
e.Handled = True
End Try
If e.KeyCode = Keys.Enter And TextBox1.Text.Length = 8 Then
MsgBox(TextBox1.Text.Substring(0, 4) & "-" & TextBox1.Text.Substring(4, 2) & "-" & TextBox1.Text.Substring(6, 2))
End If
End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Try
Dim iKey As Integer
If AscW(e.KeyChar) = "8" Then
Exit Sub
ElseIf AscW(e.KeyChar) = "13" Then
Exit Sub
End If
If IsNumeric(e.KeyChar) = True Then
iKey = Val(e.KeyChar)
Select Case TextBox1.Text.Length
Case 0
If iKey <> 2 Then e.Handled = True
Case 1, 2
If iKey <> 0 Then e.Handled = True
Case 3
If iKey < 0 Or iKey > 9 Then e.Handled = True
Case 4
If iKey < 0 Or iKey > 1 Then e.Handled = True
Case 5
If CInt(TextBox1.Text.Substring(4)) = 1 Then
If iKey < 0 Or iKey > 2 Then e.Handled = True
Else
If iKey < 0 Or iKey > 9 Then e.Handled = True
End If
Case 6
If CInt(TextBox1.Text.Substring(4, 2)) = 2 Then
If iKey < 0 Or iKey > 2 Then e.Handled = True
Else
If iKey < 0 Or iKey > 3 Then e.Handled = True
End If
Case 7
If TextBox1.Text.Substring(6, 1) = 3 Then
If iKey < 0 Or iKey > 1 Then e.Handled = True
Else
If iKey < 0 Or iKey > 9 Then e.Handled = True
End If
Case 8
e.Handled = True End Select
Else
e.Handled = True
End If Catch ex As Exception
e.Handled = True
End Try
End Sub
怎么不行了?我是vb.net的代码,你改成c#就OK了。把你邮箱告诉我,我把我的工程发给你。
private string param1 = null;
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
Match m = Regex.Match(this.textBox1.Text, pattern); // 匹配正则表达式 if (!m.Success) // 输入的不是数字
{
this.textBox1.Text = param1; // textBox内容不变 // 将光标定位到文本框的最后
this.textBox1.SelectionStart = this.textBox1.Text.Length;
}
else // 输入的是数字
{
if(this.textBox1.Text =="0")
{
this.textBox1.Text ="";
this.textBox1.SelectionStart = this.textBox1.Text.Length;
} if(this.textBox1.Text !="" && int.Parse(this.textBox1.Text)>12)
{
this.textBox1.Text = param1; // textBox内容不变
this.textBox1.SelectionStart = this.textBox1.Text.Length;
}
else
param1 = this.textBox1.Text; // 将现在textBox的值保存下来
}
}上边的代码能实现文本框小于12的数字
其他的你就简单了