如何让我的textbox的输入方式是按照如下格式呢?
XXX-XXX-XXXX,要求都是数字
解决方案 »
- dwgdirectX中axOdaViewer1.NewDevice如何设置参数
- 这个API在C#里怎么定义啊?
- 为什么点击有些网页上内容的时候不会在webbrowser中显示出来……
- 再问:C#,webapplication,在页面动态添加控件的问题
- 一个正则表达式的问题
- 水晶报表10,转PDF,excel的问题,高分求救。
- 高分求, 希望把一个 FileDialog 固定到我的 dialog 中, 难道没有简单的方法?
- 如何让TextBox响应回车键??多谢各位朋友指教
- 财鸟请教:try {} catch 语句的问题?
- 从Form1打开两个Form(Form2,Form3),现在我想从Form3重绘Form2里的控件,怎么办?
- 在csdn问的问题从来都没有人回答,不知道是问题太奇葩了还是太简单了大神不屑来回答
- 悄悄地我来了,悄悄地被拒绝~~~~~~
第二种就是用一个TextBox实现,但是你自己要在Text_Changed 事件处理器中监控当前的用户输入,适时地输入"-"号输入符号,并移动输入的光标。
我觉得第一种办法比较好,你不妨试试
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;
}
{
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;
}
}
重新排版了一下
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);
}
}
TextBox1 = new MyTextBox
当Text的Length = 3 =6+1 等等 时
this.textBox1.AppendText("-");
至于是数字 写方法 或重载TextBox
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);
}
}
}