patterncsharpModerate
Checking whether textboxes have data
Viewed 0 times
whethercheckingdatatextboxeshave
Problem
I always end up creating a quick return statement above a code block if the simple condition of textbox null fields are found. Naturally, these tend to build up the more controls are added. Now I'm staring at this mess of code, and I'm looking for advice as to avoid this very common mistake in the future.
This is the spaghetti code of a small winform C# project that I'm sure will trigger more than a few people:
Surely, there is an easier way to do this that allows the code to be modular and concise that permits the addition of new textbox controls. This happens more often than I'd like to admit and I wish to get rid of this habit once and for all.
Note that, I do have another solution, it's a linq block that iterates through ALL textbox controls in a given form. But what I'm looking for
This is the spaghetti code of a small winform C# project that I'm sure will trigger more than a few people:
if (txtFirst.Text == "" || txtLast.Text == "" || txtGross.Text == "" ||
txtLessTNT.Text == "" || txtTCI.Text == "" || txtADDTI.Text == "" || txtGTI.Text == "" ||
txtLessTE.Text == "" || txtLessPPH.Text == "" || txtLessNTI.Text == "" || txtTD.Text == "" ||
txtTWCE.Text == "" || txtTWPE.Text == "" || txtTATW.Text == "" ||
txtFirst.Text == " " || txtLast.Text == " " || txtGross.Text == " " ||
txtLessTNT.Text == " " || txtTCI.Text == " " || txtADDTI.Text == " " || txtGTI.Text == " " ||
txtLessTE.Text == " " || txtLessPPH.Text == " " || txtLessNTI.Text == " " || txtTD.Text == " " ||
txtTWCE.Text == " " || txtTWPE.Text == " " || txtTATW.Text == " " || txtID.Text == "" ||
txtID.Text == " " || txtTIN.Text == "" || txtTIN.Text == " " || txtFrom.Text == " " || txtFrom.Text == "" || txtTo.Text == " " || txtTo.Text == "" || txtCTC.Text == " " || txtCTC.Text == "" || txtCTC.Text == " " || txtPOI.Text == "" || txtPOI.Text == " " || txtDOI.Text == "" || txtDOI.Text == " " || txtDOI.Text == "" || txtAMT.Text == " " || txtAMT.Text == ""
)
{
MessageBox.Show("Cannot enter null values!");
return;
}Surely, there is an easier way to do this that allows the code to be modular and concise that permits the addition of new textbox controls. This happens more often than I'd like to admit and I wish to get rid of this habit once and for all.
Note that, I do have another solution, it's a linq block that iterates through ALL textbox controls in a given form. But what I'm looking for
Solution
Why so complicated?
One possible approach is:
A second approach is:
One possible approach is:
var f = new Form();
if (f.Controls.OfType().Any(x => string.IsNullOrWhiteSpace(x.Text)))
{
/*your stuff*/
}A second approach is:
var TextBox1 = new TextBox();
var TextBox2 = new TextBox();
var textboxes = new[] { TextBox1, TextBox2 };
if (textboxes.Any(x => string.IsNullOrWhiteSpace(x.Text)))
{
/*your stuff*/
}Code Snippets
var f = new Form();
if (f.Controls.OfType<TextBox>().Any(x => string.IsNullOrWhiteSpace(x.Text)))
{
/*your stuff*/
}var TextBox1 = new TextBox();
var TextBox2 = new TextBox();
var textboxes = new[] { TextBox1, TextBox2 };
if (textboxes.Any(x => string.IsNullOrWhiteSpace(x.Text)))
{
/*your stuff*/
}Context
StackExchange Code Review Q#153641, answer score: 12
Revisions (0)
No revisions yet.