如何让我的textbox的输入方式是按照如下格式呢?
XXX-XXX-XXXX,要求都是数字

解决方案 »

  1.   

    正则   ^\d\d\d-\d\d\d-\d\d\d\d$
      

  2.   

    第一种就是用三个TextBox和两个Label实现上述效果,
    第二种就是用一个TextBox实现,但是你自己要在Text_Changed 事件处理器中监控当前的用户输入,适时地输入"-"号输入符号,并移动输入的光标。
    我觉得第一种办法比较好,你不妨试试
      

  3.   

    你可以自己写个类,用这个
    class MyTextBox:TextBox
    {
    private string m_strOld="";
    protected override void OnTextChanged(EventArgs e)
    {
    if (Text.Length>12)//超过的恢复之前内容
    {
    Text = m_strOld;
    this.SelectionStart = Text.Length;
    return;
    }
    if (Text.Length == 0)//空的就不处理了
    {
    base.OnTextChanged (e);
    return;
    }
    string strRegex = "";
    if (Text.Length<4)//1~3
    {
    strRegex = string.Format("\\d{{{0}}}",Text.Length);
    }
    else if (Text.Length==4)
    {
    strRegex = string.Format("\\d{{3}}[-]");
    }
    else if (Text.Length<8)//6~9
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{{0}}}",Text.Length-5);
    }
    else if (Text.Length==8)
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{3}}[-]");
    }
    else
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{3}}[-]\\d{{{0}}}",Text.Length-9);
    } System.Text.RegularExpressions.Regex myRegex = 
    new System.Text.RegularExpressions.Regex(strRegex);
    System.Text.RegularExpressions.Match mtResult = myRegex.Match(Text);
    if (mtResult!=null)
    {
    if (mtResult.Value.Length>0)
    {
    m_strOld = Text;
    base.OnTextChanged (e);
    return;
    }
    }

    //错误处理
    Text = m_strOld;
    this.SelectionStart = Text.Length;
    }
      

  4.   

    class MyTextBox:TextBox
    {
    private string m_strOld="";
    protected override void OnTextChanged(EventArgs e)
    {
    if (Text.Length>12)//超过的恢复之前内容
    {
    Text = m_strOld;
    this.SelectionStart = Text.Length;
    return;
    }
    if (Text.Length == 0)//空的就不处理了
    {
    base.OnTextChanged (e);
    return;
    }
    string strRegex = "";
    if (Text.Length<4)//1~3
    {
    strRegex = string.Format("\\d{{{0}}}",Text.Length);
    }
    else if (Text.Length==4)
    {
    strRegex = string.Format("\\d{{3}}[-]");
    }
    else if (Text.Length<8)//6~9
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{{0}}}",Text.Length-5);
    }
    else if (Text.Length==8)
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{3}}[-]");
    }
    else
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{3}}[-]\\d{{{0}}}",Text.Length-9);
    } System.Text.RegularExpressions.Regex myRegex = 
    new System.Text.RegularExpressions.Regex(strRegex);
    System.Text.RegularExpressions.Match mtResult = myRegex.Match(Text);
    if (mtResult!=null)
    {
    if (mtResult.Value.Length>0)
    {
    m_strOld = Text;
    base.OnTextChanged (e);
    return;
    }
    }

    //错误处理
    Text = m_strOld;
    this.SelectionStart = Text.Length;
    }
    }
    重新排版了一下
      

  5.   

    我按照大家的写法是这样写的,为什么不行呢?我的textbox叫做buttonEdit1
    private void buttonEdit1_EditValueChanged(object sender, System.EventArgs e)
    {
    string strReg= @"^\d\d\d-\d\d\d-\d\d\d\d$";
    System.Text.RegularExpressions.Regex myReg=new System.Text.RegularExpressions.Regex(strReg);
    System.Text.RegularExpressions.Match mResult=myReg.Match(buttonEdit1.Text);
    if(mResult.Success)
    {
    base.OnTextChanged(e);
    }
    }
      

  6.   

    把我的代码复制去你程序,窗体上画一个textbox,然后修改TextBox1为
    TextBox1 = new MyTextBox
      

  7.   

    按照wuyazhe(我的宝贝叫阿刺) 这位朋友的方法,我则只能输入3位数字
      

  8.   

    在TextChanged 事件里写方法
    当Text的Length = 3 =6+1 等等 时
    this.textBox1.AppendText("-");
    至于是数字 写方法 或重载TextBox
      

  9.   

    using System;
    using System.Windows.Forms;namespace TestTextBoxMask
    {
    /// <summary>
    /// MaskTextBox 的摘要说明。
    /// </summary>
    class MyTextBox:TextBox
    {
    private string m_strOld="";
    private bool m_blDel;
    protected override void OnTextChanged(EventArgs e)
    {
    if (Text.Length>12)//超过的恢复之前内容
    {
    Text = m_strOld;
    this.SelectionStart = Text.Length;
    return;
    }
    if (Text.Length == 0)//空的就不处理了
    {
    base.OnTextChanged (e);
    return;
    }
    string strRegex = "";
    if (Text.Length<4)//1~3
    {
    strRegex = string.Format("\\d{{{0}}}",Text.Length);
    }
    else if (Text.Length==4)
    {
    strRegex = string.Format("\\d{{3}}[-]");
    }
    else if (Text.Length<8)//6~9
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{{0}}}",Text.Length-4);
    }
    else if (Text.Length==8)
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{3}}[-]");
    }
    else
    {
    strRegex = string.Format("\\d{{3}}[-]\\d{{3}}[-]\\d{{{0}}}",Text.Length-8);
    } System.Text.RegularExpressions.Regex myRegex = 
    new System.Text.RegularExpressions.Regex(strRegex);
    System.Text.RegularExpressions.Match mtResult = myRegex.Match(Text);
    if (mtResult!=null)
    {
    if (mtResult.Value.Length>0)
    {
    m_strOld = Text;
    if ((Text.Length==3 || Text.Length==7) && !m_blDel)
    {
    Text+="-";
    }
    if (m_blDel) m_blDel = false;
    this.SelectionStart = Text.Length;
    base.OnTextChanged (e);
    return;
    }
    }

    //错误处理
    Text = m_strOld;
    this.SelectionStart = Text.Length;
    } protected override void OnKeyDown(KeyEventArgs e)
    {
    if (e.KeyData == Keys.Back)
    {
    if (Text.Length == 5 || Text.Length == 9)
    {
    m_blDel = true;
    Text = Text.Substring(0,Text.Length-2);
    e.Handled = true;
    }
    else if (Text.Length == 4 || Text.Length == 8)
    {
    m_blDel = true;
    Text = Text.Substring(0,Text.Length-1);
    e.Handled = true;
    }
    return;
    }
    if (e.KeyData == Keys.Left || e.KeyData == Keys.Right || 
    e.KeyData == Keys.Up || e.KeyData == Keys.Down)
    {
    base.OnKeyDown (e);
    }
    else if (e.KeyValue < '0' || e.KeyValue > '9' || Text.Length>12)
    {
    e.Handled = true;
    return;
    }
    base.OnKeyDown (e);
    }
    }
    }
      

  10.   

    楼主,上面这个根据你说的,“-”自动填补上去,并注意了删除,方向键的细节,del键太麻烦你自己写吧。应该解决楼主的问题了吧?