如何验证更多文本框 [英] How To Validate More TextBoxes

查看:76
本文介绍了如何验证更多文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过在这些文本框上设置errorprovider来一次验证多个文本框。

i想在点击事件上使用它。

How Can i validate many text boxes at a time by set errorprovider on those textboxes.
i want to use it on butten click event.

推荐答案

嗨......你可以这样做......



Hi... You can do it by this way...

public bool CheckTextBoxNull(TextBox txt)
       {
           if (txt.Text.Trim() == "" || txt.Text==string.Empty())
           {
               txt.BackColor = Color.FromArgb(255, 192, 192);
               txt.Focus();
               return false;
           }
           else
           {
               return true;
           }
       }


private bool ValidateFn()
        {
           #region Object Validate Function

           if (CheckTextBoxNull(textbox1) == false) { return false; }

           if (CheckTextBoxNull(textbox2) == false) { return false; }
           return true;
        }

private void btn_Save_Click(object sender, EventArgs e)
        {
            #region Save Funtion
            if (ValidateFn() == true)
            {
                // you Code here...
            #endregion
        }


StringBuilder errorMessage = new StringBuilder();
foreach(Control c in Page.Controls) 
{ 
    if (c is TextBox) 
    { 
        if(ur conditions)
        {
          errorMessage.Append(c.ID.ToString()+"Has Error");
        }        
    } 
}


您可以为所有类似的控件创建下面给出的方法。您可以通过传递控制数组来调用此方法,即在您的情况下,您可以像下面给出的那样调用(传递所有必须经过验证的文本框)



You can create method given below for all similar kind of controls. You can call this method by passing array of control i.e in your case you can call like given below (pass all textboxs which has to validated)

TextBox[] listTextBox = {textBox1,textBox2};







public bool CheckTextBoxNull(TextBox[] listTextBox)
   {
       for (int i = 0; i <= listTextBox.Length - 1; i++)
       {
           if (listTextBox[i].Text.Trim().Length == 0)
           {
               listTextBox[i].BackColor = System.Drawing.Color.FromArgb(255, 192, 192);
               listTextBox[i].Focus();
               return false;
           }
       }
       return true;
   }


这篇关于如何验证更多文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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