Results 1 to 5 of 5

Thread: [RESOLVED] Form Validation Issues

Hybrid View

  1. #1
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Form Validation Issues

    O so where is any thing being saved? I can't see any code that saves any thing.
    My Github - 1d3nt

  2. #2

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

    Re: Form Validation Issues

    Nothing is being saved right now. I am in the process of validating the form before I write the code to save the form to a database.

    I have it working now, I cleaned the solution which fixed that issue. However, I have come up against another issue. I have ComboBoxes in my form that are editable, but they are populated with records from an SQL database. If I do not have a value selected, or I type the value which AutoAppends and AutoSuggests, the validation succeeds, but, when I only put in one character instead of two (for the state), I get a NullReferenceException. I want to make sure the drop downs have the valid value in them. My code is below which is working right now, except if you delete a character from the ComboBox, it is not telling me two characters required, it gives a NullReferenceException on this line:

    Code:
    If String.IsNullOrEmpty(CType(ControlName, ComboBox).SelectedItem.ToString) Then
    These are my Validated and Validating events.
    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 TheTextBox As TextBoxBase = CType(sender, TextBoxBase)
    
            If TypeOf TheTextBox Is MaskedTextBox Then
                CType(TheTextBox, MaskedTextBox).TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
            End If
    
            If Not ValidateForm(TheTextBox, ErrorMessage) Then
                e.Cancel = True
                TheTextBox.Select(0, TheTextBox.Text.Length)
                ErrorProvider.SetError(TheTextBox, ErrorMessage)
            End If
        End Sub
    
        Private Sub CompanyState_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles CompanyState.Validated, CompanyStateRemit.Validated
            ErrorProvider.SetError(CType(sender, ComboBox), String.Empty)
        End Sub
    
        Private Sub CompanyState_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CompanyState.Validating, CompanyStateRemit.Validating
            Dim TheCombobox As ComboBox = CType(sender, ComboBox)
    
            If Not ValidateForm(TheCombobox, ErrorMessage) Then
                e.Cancel = True
                TheCombobox.Select(0, TheCombobox.Text.Length)
                ErrorProvider.SetError(TheCombobox, ErrorMessage)
            End If
        End Sub
    This is what I have for the validating portion:
    Code:
        Public ErrorMessage As String = String.Empty
    
        Public Function ValidateForm(ByVal ControlName As Control, ByRef ErrorMessage As String) As Boolean
            Try
                With ControlName
                    If TypeOf ControlName Is TextBoxBase Then
                        If String.IsNullOrEmpty(.Text.ToString) Then
                            ErrorMessage = "This is a required field."
                            Return False
                        ElseIf .Name.Contains("Phone") AndAlso .Text.Length <> 10 Then
                            ErrorMessage = "The phone number must be 10 digits long."
                            Return False
                        ElseIf .Name.Contains("ZipCode") AndAlso .Text.Length < 5 Then
                            ErrorMessage = "The ZIP code must be a minimum of five digits long."
                            Return False
                        ElseIf .Name.Contains("ZipCode") AndAlso .Text.Length > 5 AndAlso .Text.Length < 9 Then
                            ErrorMessage = "The ZIP + 4 format for the ZIP code must be nine digits long."
                            Return False
                        Else
                            ErrorMessage = String.Empty
                            Return True
                        End If
                    ElseIf TypeOf ControlName Is ComboBox Then
                        If String.IsNullOrEmpty(CType(ControlName, ComboBox).SelectedItem.ToString) Then
                            ErrorMessage = "The state must be selected."
                            Return False
                        ElseIf .Name.Contains("State") AndAlso .Text.Length <> 2 Then
                            ErrorMessage = "The state must contain two characters."
                            Return False
                        Else
                            ErrorMessage = String.Empty
                            Return True
                        End If
                    End If
                End With
            Catch InvalidCastEx As InvalidCastException
                MessageBox.Show(InvalidCastEx.Message, _
                                "Invalid Cast Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            Catch NullRefEx As NullReferenceException
                MessageBox.Show(NullRefEx.Message, _
                                "Null Reference Exception", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error, _
                                MessageBoxDefaultButton.Button1)
            End Try
        End Function

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