C#:使用Keypress事件在Windows窗体中进行文本框验证 [英] C# : textbox validation in windows forms using Keypress Event

查看:88
本文介绍了C#:使用Keypress事件在Windows窗体中进行文本框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查Textbox是否为空以及是否为空应该使用Textbox KeyPress事件显示错误,并且不应该移动到下一个控件

解决方案

< pre lang =c#> private void textBox1_KeyDown( object sender,System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if String .IsNullOrEmpty(textBox1.Text))
{
MessageBox( 文本框不能为空 标题,MessageBoxButtons.OK,MessageBoxIcon.Error);
textBox1.Focus();
}
其他
{
// < span class =code-comment>做某事
}
}
}
}


How to check whether Textbox empty and if it is empty should display error using Textbox KeyPress Event and shouldn't be able to move to next control

解决方案

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
 {
 if(e.KeyCode == Keys.Enter)
   {
      if(String.IsNullOrEmpty(textBox1.Text))
         {
            MessageBox("Textbox cannot be empty","Title", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textBox1.Focus();
         }
    else
        {
             //do something
        }  
   }                      
 }
}


这篇关于C#:使用Keypress事件在Windows窗体中进行文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆