因为我想让我的 textbox只能输入字符,所以用了正则表达式 if(Regex.IsMatch(textbox1.text,"[a-zA-Z,]+"))
{
return true;
}
else
{
return false;
}现在问题是,如果我第一位输入的数字,报错---正确如果第一位输入的是字符,第二位是数字,就不报错?如何判断我当前输入的字符?
{
return true;
}
else
{
return false;
}现在问题是,如果我第一位输入的数字,报错---正确如果第一位输入的是字符,第二位是数字,就不报错?如何判断我当前输入的字符?
Regex rx = new Regex(@"^[a-zA-Z]+$");
string s = Console.ReadLine();
while (s != "exit")
{
Match m = rx.Match(s);
if (m.Success)
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
s = Console.ReadLine();
}
这样,我判断的就是textbox1.text里的所有数值那就会出现我所说的问题,如果我第一位输入的数字,报错---正确如果第一位输入的是字符,第二位是数字,就不报错?如何判断我当前输入的字符?
private void textbox1_TextChanged(){
if(Regex.IsMatch(textbox1.text,"[a-zA-Z,]+"))
{
return true;
}
else
{
return false;
}
}
string s = Console.ReadLine();
while (s != "exit")
{
Match m = rx.Match(s);
if (m.Success)
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
s = Console.ReadLine();
}
//0~9數字范圍。
private int start = 48;
private int end = 57;
private void Mask1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
try
{
int input = e.KeyChar ;
if ( input.Equals( 8 ) )
{
return ;//空格
}
if ( e.KeyChar.Equals('.') && !Mask1.Text.Length.Equals(0))
{
e.Handled = true;
SendKeys.Send("{tab}");
return ;
}
if ( input < start || input > end )
{
e.Handled = true;
}
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
在textbox的KeyUp事件里,去判断每个按下的键是否符合要求,不符合要求给予出错提示