c#windows窗体上有一个textbox,要求在里面输入日期(如:20081028形式)且只能输8个字符;
限制:年份不限,月份不大于12,日不大于31;若想输大于12的月份或大于31的日,则输不进去;
当鼠标点击textbox以外的地方,或按回车,日期则变成2008-10-28的形式

解决方案 »

  1.   

    使用MaskedTextBox吧
    掩码设成0000-00-00
      

  2.   

    建议LZ换一角度思考:
    当textbox内容不是规定的格式时,光标就不能移到其他控件就行了。
    这样的话,问题就变得简单了,用正则表达式判断就OK了。
    写Leave事件,输入内容不合法,则textbox.Focus();
    合法,则将其变成yyyy-MM-dd的形式。
      

  3.   

    直接用DateTimePicker设定格式即可
      

  4.   

            
    没人解决吗?
    哈哈,你把我下面的代码放到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
      

  5.   

    为什么不换成DateTimePicker控件呢?
      

  6.   

    有个小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
      

  7.   

    呵呵,正好我也有用,收藏了,LEARNING!!!
      

  8.   


    怎么不行了?我是vb.net的代码,你改成c#就OK了。把你邮箱告诉我,我把我的工程发给你。
      

  9.   

    private string pattern = @"^[0-9]*$";
    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的数字
    其他的你就简单了
      

  10.   

    看看,是C#板块怎么有Delphi的代码?