Checking Controls for errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zim Babwe

    #1

    Checking Controls for errors

    I am using the following to see if an error exists on any of my controls on
    my Windows Form (VS 2005)

    ' Check to see if errors exist
    '
    For Each ctrl As Control In Me.Controls
    If ErrorProvider1. GetError(ctrl) <"" Then
    invalidInput = True
    End If
    Next

    I have three group controls on my form and in each group control, there are
    4 textboxes. The above code checks the group control but bypasses the
    textboxes.

    How can I include the textboxes?

    Thanks in advance



  • RobinS

    #2
    Re: Checking Controls for errors


    "Zim Babwe" <zimbabwe@doyou reallythinkthis isreal.comwrote in message
    news:uX33Y1eUHH A.4784@TK2MSFTN GP03.phx.gbl...
    >I am using the following to see if an error exists on any of my controls
    >on my Windows Form (VS 2005)
    >
    ' Check to see if errors exist
    '
    For Each ctrl As Control In Me.Controls
    If ErrorProvider1. GetError(ctrl) <"" Then
    invalidInput = True
    End If
    Next
    >
    I have three group controls on my form and in each group control, there
    are 4 textboxes. The above code checks the group control but bypasses
    the textboxes.
    >
    How can I include the textboxes?
    >
    Thanks in advance
    >
    >
    >
    You can use a recursive routine to access all the controls. Your loop hits
    the GroupControl and doesn't look *inside* it.

    Or you can specifically check the textboxes inside the GroupControl.

    Here's how you could do it recursively. I didn't test this, but it should
    work (famous last words). Call it and pass it the form, like this:

    CheckForErrors( myForm)
    if invalidInput Then
    ...
    End If

    'This needs to be outside the routine.
    Dim invalidInput As Boolean

    Private Sub CheckForErrors( ByVal ctrlContainer As Control)
    For Each ctrl As Control In ctrlContainer.C ontrols
    If TypeOf ctrl Is TextBox _
    AndAlso ErrorProvider1. GetError(ctrl) <String.Empty Then
    invalidInput = True
    Return
    End If
    'If control has children, call this function recursively
    If ctrl.HasChildre n Then
    CheckForErrors( ctrl)
    'If it found any invalid inputs in this container, return.
    If invalidInput Then Return
    End If
    Next
    End Sub

    Good luck.
    Robin S.
    Ts'i mahnu uterna ot twan ot geifur hingts uto.


    Comment

    • Zim Babwe

      #3
      Re: Checking Controls for errors

      Thanks. This is just what I need.


      "RobinS" <RobinS@NoSpam. yah.nonewrote in message
      news:peCdnRPuh7 hPbkjYnZ2dnUVZ_ rOqnZ2d@comcast .com...
      >
      "Zim Babwe" <zimbabwe@doyou reallythinkthis isreal.comwrote in message
      news:uX33Y1eUHH A.4784@TK2MSFTN GP03.phx.gbl...
      >>I am using the following to see if an error exists on any of my controls
      >>on my Windows Form (VS 2005)
      >>
      > ' Check to see if errors exist
      > '
      > For Each ctrl As Control In Me.Controls
      > If ErrorProvider1. GetError(ctrl) <"" Then
      > invalidInput = True
      > End If
      > Next
      >>
      >I have three group controls on my form and in each group control, there
      >are 4 textboxes. The above code checks the group control but bypasses
      >the textboxes.
      >>
      >How can I include the textboxes?
      >>
      >Thanks in advance
      >>
      >>
      >>
      >
      You can use a recursive routine to access all the controls. Your loop hits
      the GroupControl and doesn't look *inside* it.
      >
      Or you can specifically check the textboxes inside the GroupControl.
      >
      Here's how you could do it recursively. I didn't test this, but it should
      work (famous last words). Call it and pass it the form, like this:
      >
      CheckForErrors( myForm)
      if invalidInput Then
      ...
      End If
      >
      'This needs to be outside the routine.
      Dim invalidInput As Boolean
      >
      Private Sub CheckForErrors( ByVal ctrlContainer As Control)
      For Each ctrl As Control In ctrlContainer.C ontrols
      If TypeOf ctrl Is TextBox _
      AndAlso ErrorProvider1. GetError(ctrl) <String.Empty Then
      invalidInput = True
      Return
      End If
      'If control has children, call this function recursively
      If ctrl.HasChildre n Then
      CheckForErrors( ctrl)
      'If it found any invalid inputs in this container, return.
      If invalidInput Then Return
      End If
      Next
      End Sub
      >
      Good luck.
      Robin S.
      Ts'i mahnu uterna ot twan ot geifur hingts uto.
      >
      >

      Comment

      • RobinS

        #4
        Re: Checking Controls for errors

        Cool. Glad I could help.
        Robin S.
        -----------------------------
        "Zim Babwe" <zimbabwe@doyou reallythinkthis isreal.comwrote in message
        news:%23CtCFofU HHA.4784@TK2MSF TNGP03.phx.gbl. ..
        Thanks. This is just what I need.
        >
        >
        "RobinS" <RobinS@NoSpam. yah.nonewrote in message
        news:peCdnRPuh7 hPbkjYnZ2dnUVZ_ rOqnZ2d@comcast .com...
        >>
        >"Zim Babwe" <zimbabwe@doyou reallythinkthis isreal.comwrote in message
        >news:uX33Y1eUH HA.4784@TK2MSFT NGP03.phx.gbl.. .
        >>>I am using the following to see if an error exists on any of my controls
        >>>on my Windows Form (VS 2005)
        >>>
        >> ' Check to see if errors exist
        >> '
        >> For Each ctrl As Control In Me.Controls
        >> If ErrorProvider1. GetError(ctrl) <"" Then
        >> invalidInput = True
        >> End If
        >> Next
        >>>
        >>I have three group controls on my form and in each group control, there
        >>are 4 textboxes. The above code checks the group control but bypasses
        >>the textboxes.
        >>>
        >>How can I include the textboxes?
        >>>
        >>Thanks in advance
        >>>
        >>>
        >>>
        >>
        >You can use a recursive routine to access all the controls. Your loop
        >hits the GroupControl and doesn't look *inside* it.
        >>
        >Or you can specifically check the textboxes inside the GroupControl.
        >>
        >Here's how you could do it recursively. I didn't test this, but it
        >should work (famous last words). Call it and pass it the form, like
        >this:
        >>
        > CheckForErrors( myForm)
        > if invalidInput Then
        > ...
        > End If
        >>
        >'This needs to be outside the routine.
        >Dim invalidInput As Boolean
        >>
        >Private Sub CheckForErrors( ByVal ctrlContainer As Control)
        > For Each ctrl As Control In ctrlContainer.C ontrols
        > If TypeOf ctrl Is TextBox _
        > AndAlso ErrorProvider1. GetError(ctrl) <String.Empty Then
        > invalidInput = True
        > Return
        > End If
        > 'If control has children, call this function recursively
        > If ctrl.HasChildre n Then
        > CheckForErrors( ctrl)
        > 'If it found any invalid inputs in this container, return.
        > If invalidInput Then Return
        > End If
        > Next
        >End Sub
        >>
        >Good luck.
        >Robin S.
        >Ts'i mahnu uterna ot twan ot geifur hingts uto.
        >>
        >>
        >
        >

        Comment

        Working...