您的位置:首页 > 其它

一个简单的TextBox问题,请高手帮忙!!(已经解决)

2007-03-06 17:19 423 查看
我用的 .NET 开发基于 widnows 的程序
TextBox 控件没有像WEB控件那样可以指定输入的字符格式如:日期型,数字型等

请问大家用什么简单的办法可以限制 TextBox 控件只能输入日期字符串??

/// <summary>
/// 根据不同类型指标控制文本框输入格式
/// </summary>
private void tb1_KeyPress(object sender, KeyPressEventArgs e)
{

string[] tbinfo = ((TextBox)sender).Tag.ToString().Split(new char[]{':'});
//MessageBox.Show(e.KeyChar.ToString());
/*此处有不明白地方*/
switch(e.KeyChar)
{
case ',':
e.Handled=true;
break;
default:
switch(tbinfo[1])
{
case "INT": //怎么样控件小数点????????
switch(e.KeyChar)
{
case '-':case '0':case '1':case '2':case '3':case '4':case '5':
case '6':case '7':case '8':case '9':
case (char)Keys.Back:
case (char)Keys.Delete:
e.Handled=false;
break;
default:
e.Handled=true;
break;
}
break;

case "DATETIME6":case "DATETIME":case "NUMERIC":case "FLOAT":
switch(e.KeyChar)
{
case '-':case '0':case '1':case '2':case '3':case '4':case '5':
case '6':case '7':case '8':case '9':
case (char)Keys.Back:
case (char)Keys.Delete:
e.Handled=false;
break;
default:
e.Handled=true;
break;
}
break;

}
break;
}
}

/// <summary>
/// 根据数字字符串转换成指定的日期格式
/// </summary>
private void tb1_Validated(object sender, EventArgs e)
{
TextBox txtSy=(TextBox)sender;
if(txtSy.Text.Trim().Length==0) return;
string strDate=txtSy.Tag.ToString().Trim().Split(new char[]{':'})[1].Trim();
string strFor=string.Empty;
if(strDate=="DATETIME" || strDate=="DATETIME6")
{
strFor=qzclass.FormatSDate(txtSy.Text.Trim());
if(strFor.Length==0)
{
txtSy.Text=strFor;
return;
}
if(strDate=="DATETIME6")
txtSy.Text=Convert.ToDateTime(strFor).ToString("yyyy.MM");
else
txtSy.Text=Convert.ToDateTime(strFor).ToString("yyyy.MM.dd");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐