Question validatechildren always returning false.

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
Attached is my code for this portion. This is a windows for application. I have three controls that has causes validation to true. validatechildren is always returning true, even if one of the controls fails validation or if they all pass. This happening on a save event.

VB.NET:
#Region "Validation"

    Private Sub Main_program_idComboBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles Main_program_idComboBox.Validating
        If IsNothing(Me.Main_program_idComboBox.SelectedValue) Then
            e.Cancel = True
            ErrorProvider1.SetError(Me.Main_program_idComboBox, "Required")
        Else
            e.Cancel = False
        End If
    End Sub

    Private Sub Main_program_idComboBox_Validated(sender As System.Object, e As System.EventArgs) Handles Main_program_idComboBox.Validated
        ErrorProvider1.SetError(Me.Main_program_idComboBox, "")
    End Sub

    Private Sub Flight_numberTextBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles Flight_numberTextBox.Validating
        If IsDBNull(Me.Flight_numberTextBox.Text) Or Me.Flight_numberTextBox.Text = "" Then
            e.Cancel = True
            ErrorProvider1.SetError(Me.Flight_numberTextBox, "Required")
        Else
            e.Cancel = False
        End If
    End Sub

    Private Sub Flight_numberTextBox_Validated(sender As System.Object, e As System.EventArgs) Handles Flight_numberTextBox.Validated
        ErrorProvider1.SetError(Me.Flight_numberTextBox, "")
    End Sub

    Private Sub Current_program_idComboBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles Current_program_idComboBox.Validating
        If IsNothing(Me.Current_program_idComboBox.SelectedItem) Then
            e.Cancel = True
            ErrorProvider1.SetError(Me.Current_program_idComboBox, "Required")
        Else
            e.Cancel = False
        End If
    End Sub

    Private Sub Current_program_idComboBox_Validated(sender As System.Object, e As System.EventArgs) Handles Current_program_idComboBox.Validated
        ErrorProvider1.SetError(Me.Current_program_idComboBox, "")
    End Sub

#End Region

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        If Me.ValidateChildren() Then
            Me.FlightplanBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.FlightTestApp2DataSet)
        Else
            Exit Sub
        End If
   .....
   End Sub

What am i doing wrong?
 
First things first, The Text of a TextBox is type String, so IsDBNull is never going to return True and it's pointless calling it.

Secondly, CausesValidation is irrelevant in this case. CausesValidation determines whether, when a control gets focus, the previous control raises its Validating and Validated events.

As for the issue, your thread title says one thing and your post says the opposite, so it's hard to know exactly what the issue is. That said, I suggest that you use some break points and step through your code to see exactly what's happening.
 
Ok, so I fixed the isdbnull. I understand now what causes validation mean.

I am not sure what you mean they are different. My issue is that on button7 click I call validate children. So it steps through those three items that I am validating. So, if all three are validated, then validatechildren should be true and continue with saving. If one of those controls is not validated, then it should exit button7_click. But it is not doing that. It is always exiting, meaning validatechildren is always returning false. So what am I doing wrong that validate children isnt working correctly.
 
Back
Top