日期:2008-07-09  浏览次数:20562 次

private void tbDay_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)


{

if (this.DesignMode)

return ;

TextBox obj = (TextBox)sender ;

obj.SelectedText = "" ;

string strValue = obj.Text.Trim() ;

int iLen= strValue.ToString().Length ;

int key = (int)(e.KeyChar) ;

switch(key)

{

case 48://0

case 49://1

case 50://2

case 51://3

case 52://4

case 53://5

case 54://6

case 55://7

case 56://8

case 57://9

if (iLen == 0)

e.Handled = false ;

else if (iLen == 1)//已经输入了一位

{

e.Handled = true ; //默认不处理,后面分析需要处理的情况

if (key==48||key==49||key==50)//现在输入的是0,1,2

{

if(obj.SelectionStart == 0)//插入

{

if (strValue != "0")//已经输入的不是0

e.Handled = false ;

if (strValue == "0" &&(key==49||key==50))

e.Handled = false ;

}

else

{

if (strValue == "0"||strValue == "1"||strValue == "2"||strValue == "3")//追加且已经输入了0,1,2,3

e.Handled = false ;

if (key==48&&strValue == "0")

e.Handled = true ;

if (key==50&&strValue == "3")

e.Handled = true ;

}

}

else//输入的是非0,1,2数字

{

if (strValue == "0"||strValue == "1"||strValue == "2")//已经输入了一个0,1,2

{

if (obj.SelectionStart == 0)//前面插入数字

{

if (key==51&&strValue != "2")//只能插入3

e.Handled = false ;

}

else

e.Handled = false ;



}



}

}

else

e.