Reputation: 143
Im trying to Create a method that can look at all text boxes in my program and tell me when the next blank one is so far. this is what i have come up with and cannot get it to work
public void CheckBox()
{
string[] itemBoxArray = new string[] { "itemBox1", "itemBox2", "itemBox3", "itemBox4", "itemBox5", "itemBox6",
"itemBox7", "itemBox8", "itemBox9", "itemBox10","itemBox11","itemBox12","itemBox13","itemBox14","itemBox15","itemBox16",};
for (int i = 0; i < itemBoxArray.Length; i++)
{
if (itemBoxArray[i] == string.Empty)
{
MessageBox.Show(" " + itemBoxArray[i] + " Is empty");
}
else
{
MessageBox.Show("Item Box is full");
}
}
}
Upvotes: 0
Views: 177
Reputation: 66439
You could use something like this to find the first TextBox
that has an empty Text
value:
var emptyTextBox = Controls.OfType<TextBox>().First(x => x.Text == string.Empty);
MessageBox.Show(string.Format(" {0} Is empty", emptyTextBox.Name));
You'll probably have to beef this up a bit.
It'll fail if no TextBox
is empty. Use FirstOrDefault
and test for null
if that's a concern.
Also, it'll fail to find TextBox
controls inside of a GroupBox
or Panel
. That may not matter depending on how your form is designed.
If you only have a set number of TextBox
controls to check for, your code will work with a little adjustment, but if you're going to keep adding more TextBox
es, it's going to be a pain to maintain.
Upvotes: 0
Reputation: 63065
create array of TextBox
like below
public void CheckBox()
{
TextBox[] itemBoxArray = new TextBox[] { itemBox1, itemBox2, ........};
for (int i = 0; i < itemBoxArray.Length; i++)
{
if (String.IsNullOrEmpty(itemBoxArray[i].Text))
{
MessageBox.Show(" " + itemBoxArray[i].Name + " Is empty");
}
else
{
MessageBox.Show("Item Box is full");
}
}
}
Upvotes: 2