Field check Combobox, Textbox Maskedtextbox

Asked

Viewed 1,339 times

0

How to create a method to check that all "Combobox, Textbox Maskedtextbox" fields are filled in? 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;
}
  • 1

    "Among others" can hold a lot of things, man. Please specify which others are. Incidentally, your code makes very little sense.

  • That’s why I’m asking for help.

  • "Among others" can hold a lot of things, man. Please specify which others are

  • Among others would be text entries. Just follow the examples cited.

  • Combobox is not text input...

  • I will explain further. I have a layout, and I want to check that all fields are properly filled.

  • Sorry, you are correct. Anyway need to get the value of the selected item in it.

  • Okay, young man. That’s right. What I want to know is, what controls do you intend to validate in this method? Just TextBoxes and ComboBoxes?

  • I have 3 elements I need to check, Combobox, Textbox and Maskedtextbox.

  • Right now!!!!!!!

  • Sorry, beginner here, do not want to put a conditional for each element the code would be too big, if there is another way to do thank you.

  • Unfortunately I was not able to solve, I found a solution that is to check the object passed to the database, so I will do so and continue studying the options presented until I understand 100% what happens.

Show 7 more comments

2 answers

4

Basically you 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 need to call the function again by passing the control as parameter.

The method returns true if all fields have been duly completed.

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;
}
  • I might know what’s wrong?

  • For me here presented error when loading Parent = this;

  • I’ve already edited @Ronivaldoroner

  • I’m studying your method here, but the way it is, it only returns true, even if all are filled or not.

  • No, @Ronivaldoroner. He only returns true when all are completed.

  • Well, I did tests and ta returning false even all filled fields, add Messagbox to analyze each step and realized that it is leaving 2 fields without checking, one of them is a Maskedtextbox, and another Textbox with Multiline = true. Funny thing is that he’s seeing other Maskedtexbox and Textbox, I’ll keep analyzing, I believe I’m close to the solution.

  • He’s leaving without checking how? Going straight through them?

  • Exactly. Add a Messagebox in each -if- to print control.Text and view the contents of each element. and 2 elements are simply ignored.

  • You cannot debug the code?

  • I’m trying here.

Show 5 more comments

0

Use a recursive function, so that you go through the control and 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 her, inside a Form:

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

Suggestion:

In my case, I put one Exception when the field is empty, because then I can inform the user which field is invalid, highlight the field or anything like that. 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 when calling the function:

try 
{
   CamposVazios(this);
   //Processar / Gravar / etc...
} 
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
  • Your answer is wrong. If you have a panel with a textbox inside it and a textbox outside it, only the last return will be valid. I mean, the comeback will be false and should be true.

  • I got it right, thank you

  • 1

    Another thing that is wrong is the use of exceptions to indicate that there are empty fields,

  • I’ll withdraw the negative vote. Now it works, but I think it’s a bad idea to use exceptions.

  • was just a suggestion, which I use. Is there a specific reason not to use the exceptions? An observation that I use this code as a function in another class and all the application forms can access it.

  • Exceptions should be used for exceptional situations and not for flow control. Instead of launching an exception the method CamposVazios() should return a list with the names of the empty fields. So, after calling it, it is possible to check whether or not there are empty fields and if yes build a message with their name.

  • @ramaral ok, thanks for the info.

  • I did some tests and only returns true This having empty fields or not. I will study on your method to understand better.

  • This error was made yesterday in the edition

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.