Checking fields ComboBox, TextBox MaskedTextBox [closed]

0

How to create a method to check if all fields "ComboBox, TextBox, MaskedTextBox" are populated? Mine does not work

public bool CampoVazio()
{
    bool ok = false;
    foreach(Control ctrl in this.Controls)
      {
          if(ctrl is TextBox)
            {
              if(string.IsNullOrEmpty(ctrl.Text))
                 {
                    ok= true;
                    break;
                 }
            }
        }
    return ok;
}
    
asked by anonymous 24.05.2017 / 21:29

2 answers

4

You basically need to validate all fields that are TextBoxBase (this includes all types of TextBox ) or ComboBox or if they have "children", in this case you must call the function again by passing the control as a parameter.

The method returns true if all fields have been filled in.

private bool VerificaCamposPreenchidos(Control parent = null)
{
    if (parent == null)
        parent = this;

    foreach (Control control in parent.Controls)
    {
        if (control is TextBoxBase)
        {
            var txt = (TextBoxBase)control;
            if (string.IsNullOrEmpty(txt.Text))
                return false;
        }

        if (control is ComboBox)
        {
            var cmb = (ComboBox)control;
            if (cmb.SelectedValue == null)
                return false;
        }

        if (control.HasChildren)
        {
            if (!VerificaCamposPreenchidos(control))
                return false;
        }
    }

    return true;
}
    
24.05.2017 / 21:49
0

Use a recursive function, so you can take control of your children:

    public static bool CamposVazios(Control _ctrl)
    {
        foreach (Control c in _ctrl.Controls)
        {
            if (c is TextBox)
            {
                if (String.IsNullOrEmpty(((TextBox)c).Text))
                    return true;
            }
            else if (c is ComboBox)
            {
                if (((ComboBox)c).SelectedValue == null)
                    return true;
            }
            else if (c.HasChildren)
            {
                if (CamposVazios(c))
                    return true;
            }
        }

        return false;

    }

To call it, inside a Form:

if (CamposVazios(this))
{
  //Há algum campo vazio.
}

Suggestion:

In my case, I put a Exception when the field is blank, because then I can tell the user which field is invalid, highlight the field or anything. Ex:

    public static void CamposVazios(Control _ctrl)
    {
        foreach (Control c in _ctrl.Controls)
        {
            if (c.HasChildren)
            {
                CamposVazios(c);
            }
            else if (c is TextBox)
            {
                if (String.IsNullOrEmpty(((TextBox)c).Text))
                {
                    throw new Exception("Campo "+ c.Name + " está vazio");
                }
            }
            else if (c is ComboBox)
            {
                if (((ComboBox)c).SelectedValue == null)
                {
                    throw new Exception("Campo "+ c.Name + " está vazio");
                }
            }
        }
    }

and at the time of calling the function:

try 
{
   CamposVazios(this);
   //Processar / Gravar / etc...
} 
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
    
24.05.2017 / 21:48