I have a small window program that I use to quickly add information. But now I'm trying to improve it. I needed a more effective option for checking empty text fields, and if the box was empty to find what it was, and set the focus back only to this field. I am currently reviewing all of them and checking to see if there was an empty field if it just displays a message. But you need to see in which field the text is missing. Here is the code:
bool txtCompleted = true; string errorMessage = "One or more items were missing from the form"; foreach(Control c in Controls) { if (c is TextBox) { if (String.IsNullOrEmpty(c.Text)) { txtCompleted = false; } } } if (txtCompleted == false) { MessageBox.Show(errorMessage); }
source share