请问大家,如何在winform中对textbox控件进行非空验证呢?只要验证是否非空就可哈。不用其他的验证。

解决方案 »

  1.   

     if (textbox.Text.Trim() == "")
       {
          MessageBox.Show("textbox不能为空!");
       }
      

  2.   

    string.IsNullOrEmptywinform没有现成的像asp.net下的那样验证控件,除非你自己写或者使用别人的控件
      

  3.   

    if (textBox1.Text.Equals(""))
    {
        //代码
    }字符的最好这样写,用“==”不太好
      

  4.   

    那如果一次有好多个文本框,有没有简洁一点的办法?总不能这样写吧:
    if(textbox1.text.equals("")||textbox2.text.equals("")||......)
      

  5.   

    简单点一个个的判断,要不然看一下CSLA框架,把规则抽象出来,用反射进行判断。
      

  6.   

     private void textBox1_Leave(object sender, EventArgs e)
            {
                if (((TextBox)sender).Text.Trim().Equals(""))
                    MessageBox.Show("不能为空");        }
    然后把所有需要验证的TextBox控件的Leave事件,知道这个处理方法上来。
      

  7.   

    if(this.textbox.Text.length == 0)
    {
        MessageBox.Show("不能为空");
    }
    楼主好像应该看书吧.
      

  8.   

    自已写个控件,继承Textbox,然后想怎么玩怎么玩
      

  9.   

    新建一个控件,代码,应该够用了
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace nxCommonClass
    {
        public partial class NxTextBox : TextBox
        {
            public NxTextBox()
            {
                InitializeComponent();
                this.Leave += new EventHandler(NxTextBox_Leave);        }        void NxTextBox_Leave(object sender, EventArgs e)
            {
                float result;
                if(string.IsNullOrEmpty(this.Text))
                {
                  MessageBox.Show("Should be not null!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
                //校验是否是数字
                if(!float.TryParse(this.Text, out result))
                {
                   MessageBox.Show("error input!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
                this.Focus();
            }
        }
    }
      

  10.   

    string.IsNullOrEmpty(textbox.Text)
    if (textbox.Text.Trim() == "") 
      { 
          MessageBox.Show("textbox不能为空!"); 
      }
    以上两个都是可以直接解决的;
      

  11.   

    原来winform没有验证控件。呵呵。就要知道这个。
      

  12.   


    这个事件似乎不是很好,一定要鼠标移入textbox事件才能触发