Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Text Box entries quickly

Status
Not open for further replies.

rpk2006

Technical User
Apr 24, 2002
225
IN
How to validate Text Box Entries quickly? Is it good to validate when the user types in or by calling a Validate Function when the operation starts?

In my application I have a Tabbed Control with five tabbed pages. Each tabbed page contains same set of Text Boxes, but having different names.

For example, In Tab Page 1, a txtName1, similarly, in Tab page 2, txtName2.

The same set is repeated on all Tabbed pages but some pages contain one or two different options. This is done so as to categorized the entries and free the user to mess up in selecting options.

I want to Validate the Text Boxes before Saving the Record. The Save Record Function is also same for all Tabs.

Please let me know how can I write only one routine which can automatically handle Text Box Names and the code need not be typed again and again.

 
The controls should have CausesValidation set to true. Write a sub like:

Private sub ValidateControls(byval sender as system.object, byval e as system.eventargs) handles txtname1.validated,txtname2.validated, .....

select case sender.GetType.ToString
case "System.Windows.Forms.TextBox"
' validate the textbox
case "System.Windows. ..."
' validate other type
end select
End sub


 
There is a powerfull FAQ written by chrissie1: faq796-5698 which you could use.

Or, you can loop the TabPages and all existing textboxes:
Code:
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
	For Each T As TabPage In TabControl1.TabPages
		LoopTabCtr(T)
	Next
End Sub

Private Sub LoopTabCtr(ByVal TabCtr As TabPage)
	For Each ctr As Control In TabCtr.Controls
		If TypeOf ctr Is TextBox Then
			ValidateTextBox(ctr)
		End If
	Next
End Sub

Private Sub ValidateTextBox(ByVal TB As TextBox)
	If TB.Text = "" Then
		MsgBox(TB.Name & " is Empty")
	End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top