tags:

views:

65

answers:

4

In my form, I have 8 textboxes. All of these need to be filled in, and I wanted to check if they are filled in before going to run the rest of the code.

The only way I've done it thus far is 8 nested IF statements, and this looks unorganised and seems like a horrible way to do it.

Can someone tell me a better way to do this? If there is one, of course.

Thanks :)

+1  A: 

Is this winforms or webforms?

If Winforms, look at the ErrorProvider control.
If Webforms, look at the validation controls (specifcally, RequiredFieldValidator).

In either case, I would likely create a custom control that wraps the validation together with the textbox that you can just drop on your form where you need it.

Joel Coehoorn
It's Winforms, and i'll take a look at the ErrorProvider control now. Thanks.
Whitey
Hold on, how do you create a control again? Or at least, how would you create this control (textbox+validation)?
Maxim Zaslavsky
Hi.The control is available from the tool box in the left of the Visual Studio window.Here's some code samples :)If txtName.Text = "" Then frmBookErr.SetError(txtName, "This field cannot be left blank.") Else frmBookErr.SetError(txtName, "") End If
Whitey
But as for creating a custom control...? Very interesting question! I have this problem too!
Maxim Zaslavsky
A: 

Simply use RequiredFieldValidator along with the ValidatorSummary field to display the validation messages.

Let me know if that helps.

Pratik

Pratik Talati
RequiredFieldValidator is for web-apps. This is question is about WinForms.
Grammarian
+1  A: 

Hmm, how about grouping the 8 textboxes into 1 Panel and looping through each of them instead? Something like this:

bool text = true;

foreach(Control ctrl in Panel)
{
    TextBox textbox = ctrl as TextBox;
        if (box.Text.Length == 0)
        {
            DisplayErrorMsg();
            text = false;
        }
}

if(text) ExecuteCode();
Kronon
+1  A: 

At form load time, make a collection of all the textboxes that cannot be empty. Then simply loop through them. This is similar to @Kronon answer, but without the Panel. I like changing the background color to indicate which fields should not be empty.

public partial class MainForm
{
 private List<TextBox> mandatoryTextBoxes;

    private void MainForm_Load(object sender, EventArgs e) {
        mandatoryTextBoxes = new List<TextBox>();
        mandatoryTextBoxes.Add(this.textBox1);
        // add other textboxes1
    }

    private bool CheckMandatoryFields() {
        bool allFieldsPresent = true;
        foreach (TextBox tb in this.mandatoryTextBoxes) {
            if (tb.Text.Length == 0) {
                tb.BackColor = Color.Yellow;
                allFieldsPresent = false;
            } else {
                tb.BackColor = Color.White;
            }
        }
        return allFieldsPresent;
    }

    private void DoWork() {
        if (!this.CheckMandatoryFields()) {
            this.SetError("Indicated fields cannot be empty");
            return;
        }
        // do real work here
    }
}
Grammarian