Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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?

.nerd
  • Leaders
Posted

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.

Posted (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 by nibsy

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...