Results 1 to 5 of 5

Thread: [RESOLVED] Form Validation Issues

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Form Validation Issues

    I am writing an application for my Notary business. I am a Notary Public in Iowa.

    I have many types of control on the form. ComboBox (that can be modified), TextBox and MaskedTextBox. I know I can use the TextBoxBase class to cover the TextBox and also the MaskedTextBox, but my validation code is not doing anything. Nothing happens.

    I have an ErrorProvider on the form called ErrorProvider. It has the "AutoValidate" option set to "EnableAllowFocusChange". The BlinkStyle is set to AlwaysBlink. All other properties are default.

    I have three buttons on the form, too. Reset, Save and Close. Both the Reset and Close buttons are set to CausesValidation = False and the Save button is set to CausesValidation = True.

    I need to make sure the "required" fields have a value when the Save button is clicked.

    So, I figured I would use the Validated and Validating events of the controls. But, when clicking Save, nothing happens. The ErrorProvider doesnt even show. If I leave the control empty, still nothing happens.

    This is my code:

    Code:
    Private Sub Company_Validated(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Company.Validated, CompanyAddress.Validated, CompanyCity.Validated, CompanyZipCode.Validated, CompanyPhone.Validated, CompanyAddressRemit.Validated, CompanyCityRemit.Validated, CompanyZipCodeRemit.Validated
        ErrorProvider.SetError(CType(sender, TextBoxBase), String.Empty)
    End Sub
    
    Private Sub Company_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles Company.Validating, CompanyAddress.Validating, CompanyCity.Validating, CompanyZipCode.Validating, CompanyPhone.Validating, CompanyAddressRemit.Validating, CompanyCityRemit.Validating, CompanyZipCodeRemit.Validating
        Dim ErrorMessage As String = String.Empty
        Dim TheField As TextBoxBase = CType(sender, TextBoxBase)
    
        If TypeOf TheField Is MaskedTextBox Then
            CType(TheField, MaskedTextBox).TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
        End If
    
        If Not IsValid(TheField, ErrorMessage) Then
            e.Cancel = True
            TheField.Select(0, TheField.Text.Length)
            ErrorProvider.SetError(TheField, ErrorMessage)
        End If
    End Sub
    
    Private Function IsValid(ByVal ControlName As TextBoxBase, ByRef ErrorMessage As String) As Boolean
        If String.IsNullOrEmpty(ControlName.Text.ToString) Then
            ErrorMessage = "This is a required field."
            Return False
        Else
            ErrorMessage = String.Empty
            Return True
        End If
    End Function
    Any ideas on what I am doing wrong?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width