if (comboBox2.Text.Trim().Length <= 0)
            {
                MessageBox.Show("收据号没有写", "错误", MessageBoxButtons.OK);
                    return ;            }
            if (comboBox2 .Items .Contains (comboBox2 .Text  ))
            {
                MessageBox .Show ("收据编号重复了","错误",MessageBoxButtons .OK );
                return;
            }
            if (comboBox1.Text.Trim().Length <= 0)
            {
                MessageBox.Show("姓名没有写", "错误", MessageBoxButtons.OK);
                return;            }
            if (comboBox1.Items.Contains(comboBox1.Text.Trim()))
            {
                MessageBox.Show("没有这个人", "错误", MessageBoxButtons.OK);
                return;
            }
            if (maskedTextBox2.Text.Trim().Length <= 0)
            {
                MessageBox.Show("金额没有写", "错误", MessageBoxButtons.OK);
                return;
            }
            if (maskedTextBox3.Text.Trim().Length <= -0)
            {
                MessageBox.Show("刷卡金额没有写", "错误", MessageBoxButtons.OK);
                    return ;
            }
            else
            {
                //得到值内容
     
                this.学员报名收入TableAdapter1.InsertQuery报名收入(收据编号, Convert.ToString(开票日期), 姓名, 项目, 金额, 刷卡金额, 销售);
            }
请问检查输入字段怎么能写一个过程或是函数更简单的调用,因为有很多窗体都要做输入检查

解决方案 »

  1.   

    自己写一个方法  返回 bool型咯然后调用
      

  2.   

            #region 检测指定文本控件是否为空
            /// <summary>
            /// 检测指定文本控件是否为空
           /// </summary>
           /// <param name="txtName">控件名称</param>
           /// <param name="str">文本提示</param>
           /// <returns></returns>
            public static bool jctxt(Control txtName,string str)
            {
               bool Flag=false;
                if (txtName.Text.Trim().Length == 0)
                {
                    MessageBox.Show(str,"提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    txtName.Focus();
                    Flag=true;
                }
                return Flag;
            }
     
            #endregion
      

  3.   

    最近看了MVP设计模式,把窗体的一个部分类作为一个对象传递给下一个类接收,这样就可以不用考虑窗体控件的名称了。楼主是这个意思吧。。
      

  4.   

    如果有很多窗体要用相同的验证就写成函数,参数就是string,返回值bool,比如你要验证收据是否合理
    public bool ValidateShouJu(string str)
    {
        if (str.IsNullOrEmpty()) return false;    string val = str.Trim();
        ...
       return true;
    }用的时候 bool b = ValidateShouJu(Combobox1.text);
      

  5.   

    委托
    public delegate void myDelegate(string str1,comboBox b2);public void 收据(string str1,comboBox b2)
            {
                if (str1.Trim().Length <= 0)
      {
      MessageBox.Show("收据号没有写", "错误", MessageBoxButtons.OK);
      return ;  }
      if (b2 .Items .Contains (comboBox2 .Text ))
      {
      MessageBox .Show ("收据编号重复了","错误",MessageBoxButtons .OK );
      return;
      }
            }
    public void 姓名(string str1,comboBox b2)
            {
                判断....同上
            }
    实现:
                myDelegate gd1 = new myDelegate(收据);
                myDelegate gd2 = new myDelegate(姓名);
                (gd1 + gd2)(传值);
    需要就往上加不需要就往下减.
    因为你的报错信息不同.所以要把错信息都定义出来.
      

  6.   

    委托
    public delegate void myDelegate(string str1,comboBox b2);public void 收据(string str1,comboBox b2)
            {
                if (str1.Trim().Length <= 0)
      {
      MessageBox.Show("收据号没有写", "错误", M……