I know that the .Focus() allows you to focus on a text input field, or wherever you wish your eye to be directed, but I am wondering, is there a way to use Focus to direct the person’s view to whatever field they left empty?
So in other words, as an example:
Try
Code to input Field A
Code to input Field B
Code to input Field C
Catch
'Error Message Box for one or more of the empty Class Ticket Fields.
MessageBox.Show("One or More of the Fields are empty! Please Enter a Value!")
txt_ClassA.Focus()
End Try
Now lets say that someone forgot to enter text into field B, is there a way except to write a try/catch for each individual input field, to have the .Focus() actually focus on B automatically?
I’ll try to answer both the technical part of your question in a bit, but I would to start with pointing out that validating a form in its entirety before providing (limited) feedback about the found issues is a bit outdated.
You can read up on the latest insights in usability (and related field) on the dedicated Stack Exchange site. Note though that any of the newer techniques will probably prevent your current problem from occurring, so it may be wise to read up before continueing with your current solution.
However, you may be stuck with some older code or have specific requirements to implement the form as you did so I will also attempt to answer your question from a technical point of view.
Note that the Focus method is not so only about drawing the attention to a specific control on your form.
It also (or, in my opinion, instead) makes a control the active control. This will allow your program to find out on which control a key press or mouse click should to be executed.
You are looking for a way to check if all required fields have values without creating duplicate code. This is a good attitude!
If I would be asked to do this, I would try to find the commonalities (the parts that are duplicate).
In your example, this would be the check on whether or not a text box contains a value, and the reporting of the problem if a problem is found.
I would then create a method where I would store the common code and ‘inject’ the non-common code, which in you example are the different text boxes.
I have not written any VB since VB 6, so I hope the pseudo VB code below will get the point across.
The method:
Private Function TextBoxContainsValue(textBoxToCheck as TextBox, labelText String) as Boolean
If textBoxToCheck.Text = "" Then
textBoxToCheck.Focus()
MessageBox.Show("The text box labelled " + labelText + " is required. Please enter a valid value.");
TextBoxContainsValue = False
Else
TextBoxContainsValue = True
End If
End Function
Your current code can then change to something like:
If Not TextBoxContainsValue(txt_ClassA, "Class A") Then
' No need to check the other text boxes since we can put the focus on just one control.
Exit Sub
End If
If Not TextBoxContainsValue(txt_ClassB, "Class B") Then
Exit Sub
End If
If Not TextBoxContainsValue(txt_ClassC, "Class C") Then
Exit Sub
End If
Again, this just addresses the technical part of your question (even adds a bit more information about which text box was found empty).
Searching for “form validation” on the User Experience Stack Exchange site (found at: ) resulted in over 50 pages of questions.
Do yourself and your users a big favor a check out at least some of them to get an idea on how to improve your users experience.