Vb.net – In vb.net, how to force validating controls within a (winform) datarepeater after the data is populated

vb.netwinforms

I have a form with a datarepeater that contains various controls (i.e. datetimepickers, text boxes, combo box) that are populated via a binding source. I also have other controls on the form that are not part of the data repeater.

I would like to force validating all controls after the data is populated. I have successfully forced validating the non-datarepeater controls using Me.ValidateChildren() at the end of my load event. However, it does not fire the validating events for the controls within the data repeater.

I have unsuccessfully tried many different attempts to set and move focus within the datarepeater controls trying to get the validating events kicked off. I am not sure where would be the best place (e.g. in drawItem? in ItemCloned?) to place the code and what it should be exactly. Here was my latest attempt:

Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) _
        Handles DataRepeater1.DrawItem

        For i = 0 To e.DataRepeaterItem.Controls.Count - 1
            e.DataRepeaterItem.Controls.Item(i).Focus()
            e.DataRepeaterItem.Controls.Item(0).Focus()
        Next

Note: I successfully handled the validating events in the data repeater caused by user input errors. However, I have the unusual situation that the data coming into my form is already bad for some of the controls. The purpose of the form is to validate the data coming in to it and from user-input.

Thanks in advance for any help. I am newbie with vb.net.

Best Answer

Have you tried calling

DataRepeater1.ValidateChildren()

after calling form's Me.ValidateChildren()

MSDN link

EDIT:

Can you try this

Private Shared Function ValidateAllChildern(cc As ContainerControl) As Boolean
    Return cc.ValidateChildren() And cc.Controls.OfType(Of ContainerControl)().[Select](Function(c) ValidateAllChildern(c)).Aggregate(True, Function(x, y) x And y)
End Function

and call

ValidateAllChildren(Me)
Related Topic