posts - 48, comments - 225, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

Some useful TextBox Validations

Posted on 2006-04-21 22:34 i.Posei 阅读(221) 评论(1)  编辑 收藏 所属分类: 网络搜刮
Numeric TextBox
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
{
e.Handled = true;
}
}

Numeric TextBox with Decimals
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsDigit( e.KeyChar) || char.IsControl( e.KeyChar ) ||(e.KeyChar== (char )46)) )
{
e.Handled = true;
}
}

TextBox Allowing Characters Only
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsLetter( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
{
e.Handled = true;
}
}

TextBox Allowing Upper Case Characters Only
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsUpper( e.KeyChar ) || char.IsControl( e.KeyChar )) )
{
e.Handled = true;
}
}

TextBox Allowing Lower Case Characters Only
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsLower( e.KeyChar ) || char.IsControl( e.KeyChar )) )
{
e.Handled = true;
}
}

Check For Unfilled TextBox
// Call this function and pass the Textbox as parameter to this function
public static bool ChkEmpty(params System.Windows.Forms.TextBox[ ] tb)
{
int i;
for (i = 0; i < tb.Length; i++)
{
if (tb[i].Text.Trim() == "")
{
MessageBox.Show("Don't keep field empty");
tb[i].Focus();
return false;
}
}
return true;
}


http://www.codeproject.com/useritems/tips.asp#tip3.4.3
 

Feedback

#1楼    回复  引用    

2006-06-04 12:33 by gg [未注册用户]
mid395@126.com

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-10-18 21:45 编辑过


相关链接:

历史上的今天:
2005-04-21 程序员的十种级别