txtBox Error checking

ricio2000

Member
Joined
May 2, 2005
Messages
18
Location
Hialeah, FL
Programming Experience
1-3
Hello Fellow Developers and Computer Science Junkies, I had a situation where I am not sure how to go about implementing. I have several txt boxes that will be updated into a database. All are strings and in the database will be char values. I don't want a user to be able to leave anything blank. I wanted to do something on the leave event to check to see if the the Text property of each individual text box is not equal to blank string => "". I am open to any other suggestions as to how to handle this type of error checking. I want a message box to tell the user hey, you must not leave this blank. IN the leave event, what property must i set in order to leave the blinker the current text box until the user enters some values? Hope somebody wise can help me with this dilema.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
Use the Validating event, which is intended for this purpose, instead of the LostFocus event:
VB.NET:
Private Sub Text1_Validating(...) Handles Text1.Validating
	If Me.Text1.Text.Trim() = Nothing
		MessageBox("Please enter a value in the current field.")
		e.Cancel = True
	End If
End Sub
The Validating event is raised first. If the Control passes validation, i.e. e.Cancel remains False, the LostFocus event is raised. Note that there are certain instances where these events are not raised until after an action is taken, like when a ToolbarButton is clicked. In this case, the ButtonClick event of the Toolbar is raised before the Validating event of the focused Control. In this case, you must explicitly call Me.Validate() in the ButtonClick event handler of the Toolbar.
 

ricio2000

Member
Joined
May 2, 2005
Messages
18
Location
Hialeah, FL
Programming Experience
1-3
I decided to go with the error control approach. In the leave event, I would set the control's errortext to somethign meaning ful, and if when they hit save at the end, the error control is not blank. Then display an error telling to please see above and fix the errors as requiered. The error control if they do a mouse over, it tells them whats seems to be the problem. BUT I'VE SEEN YOUR APPROACH, THE VALIDATING EVENT IS GREAT AS WELL, BUT THE PROBLEM WITH THAT IS THAT USERS MAY WANT TO FILL THAT AT THE END. BUT THEY ARE FORCED TO ENTER SOMETHING. KIND OF GETS ANNOYING. THANKS FOR YOUR HELP BUDDY.
 
Top Bottom