nibsy Posted July 17, 2003 Posted July 17, 2003 I am using the following code to validate input for two textboxes, Private Sub GenericNotEmpty(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _ Handles txtDays.Validating, txtName.Validating 'Validate mandatory input. Dim ctlSender As Control Dim sErrText As String ctlSender = CType(sender, Control) If ctlSender.Text.Trim = "" Then e.Cancel = True ErrProv.SetError(ctlSender, sErrText) End If End Sub I wish to make the error message intelligent, so that it is pertinent to whichever text box is being validated. Is there something obvious I could test to achieve this? Quote
Heiko Posted July 17, 2003 Posted July 17, 2003 Sorry, what does pertinent mean? The Error text should stay with the ctlSender until you blank it out. However in your example sErrText is always empty. Could that be the problem? Quote .nerd
Leaders dynamic_sysop Posted July 17, 2003 Leaders Posted July 17, 2003 you want to do this i guess? : Private WithEvents TextBox As TextBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler TextBox1.Validating, AddressOf TextBox_Validating AddHandler TextBox2.Validating, AddressOf TextBox_Validating End Sub Private Sub TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox.Validating Try If sender.text = "" Then e.Cancel = True Dim x As Exception = New Exception("oops " & sender.name & " doesnt have any text in it!") Throw x End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub both textboxes get handled properly by 1 sub and the message tells the user which box is empty. Quote
nibsy Posted July 17, 2003 Author Posted July 17, 2003 (edited) Heiko: Pertinent means 'Relevant' dynamic_sysop: That is just what I was looking for! Thank you both for your replies Code for future reference: Private Sub GenericNotEmpty(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _ Handles txtDays.Validating, txtName.Validating 'Validate mandatory input. Dim ctlSender As Control Dim sErrText As String ctlSender = CType(sender, Control) If ctlSender.Text.Trim = "" Then e.Cancel = True If sender.name = "txtDays" Then sErrText = "Number of Days" Else sErrText = "A name" End If ErrProv.SetError(ctlSender, sErrText & " must be entered.") End If End Sub Edited July 17, 2003 by nibsy Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.