Ive got a Form application in VB.NET.
I have many text boxes on one form (about 20). Is there anyway to check them all at once to see if they are empty instead of writing out a massive line of code to check each one individually such as
If txt1.text = "" Or txt2.text="" Then
msgbox("Please fill in all boxes")
That just seems like a long way around it?
You could also use LINQ:
The interesting method is Enumerable.OfType
The same in query syntax(more readable in VB.NET):
If
TextBox
field is empty then the message box will appear saying "Complete Entry!".I would recommend using the Validating event of the TextBox controls, with an error provider control (just add one to your form):
Then you can just call:
The nice thing about this is that the user is informed about which textbox is missing and required. This works with other controls besides textboxes, so you can provide a more complete solution. Also, if you get to a later point where one or two textboxes don't need to have values, you simply do not validate them instead of having to add special cases in your loops.
Finally, if you don't want to type out all of the controls, then you could do this in form load:
A very simplistic approach would be to gather all the
TextBox
controls in a sequence using the Enumerable.OfType LINQ method and then iterate through it in a For Each loop:Public Class freestyle
End Class
I found this, perhaps you can modify it to check if all textboxes are clear rather than what it currently does which is just clear all textboxes