Recursive function help

[b][red]This message was edited by DaveR at 2006-2-6 14:51:16[/red][/b][hr]
[b][red]This message was edited by DaveR at 2006-2-6 14:20:54[/red][/b][hr]
why is this not working
[code]
Public Sub ClearAll(ByVal ctl As Control)

For Each ctl In ctl.Controls
If TypeOf ctl Is ContainerControl Then
ClearAll(ctl)
ElseIf TypeName(ctl) = "TextBox" Then
DirectCast(ctl, TextBox).Clear()
End If
Next
End Sub
[/code]

when i call this function i call it like
clearall(me)

i want it to when it gets to a container control to call it self like
clearall(groupbox)

Comments


  • You have passed the same control name to the function. How does it suppose to clear the control? The below statement should clear the current control and then you can recursively call the function.
    If TypeOf ctl Is ContainerControl Then
    ClearAll(ctl) <--- before you that you should perform an action for the current control otherwise the loop won't progress.

    Check it out!

  • ok, the problem is that the line

    If TypeOf ctl Is ContainerControl Then

    dose not recognize a groupbox or any other containercontrol for that matter as a containercontrol. and the loop will progress just fine. try it out. i have working code for this i am just wondering why my intial thoughts did not work. more specifically why that line of code is not recognizing a groupbox as a containercontrol

    here is the working code

    [code]
    Public Sub ClearAll(ByVal ctl As Control)

    For Each ctl In ctl.Controls
    If ctl.Controls.Count > 0 Then
    ClearAll(ctl)
    ElseIf TypeName(ctl) = "TextBox" Then
    DirectCast(ctl, TextBox).Clear()
    End If
    Next
    End Sub
    [/code]

    works perfectly for all container controls. my question is why dosen't my intial thought on it work.

    :
    : You have passed the same control name to the function. How does it suppose to clear the control? The below statement should clear the current control and then you can recursively call the function.
    : If TypeOf ctl Is ContainerControl Then
    : ClearAll(ctl) <--- before you that you should perform an action for the current control otherwise the loop won't progress.
    :
    : Check it out!
    :
    :

  • [b][red]This message was edited by DrMarten at 2006-2-9 10:21:55[/red][/b][hr]
    : ok, the problem is that the line
    :
    : If TypeOf ctl Is ContainerControl Then
    :
    : dose not recognize a groupbox or any other containercontrol for that matter as a containercontrol. and the loop will progress just fine. try it out. i have working code for this i am just wondering why my intial thoughts did not work. more specifically why that line of code is not recognizing a groupbox as a containercontrol
    :
    : here is the working code
    :
    : [code]
    : Public Sub ClearAll(ByVal ctl As Control)
    :
    : For Each ctl In ctl.Controls
    : If ctl.Controls.Count > 0 Then
    : ClearAll(ctl)
    : ElseIf TypeName(ctl) = "TextBox" Then
    : DirectCast(ctl, TextBox).Clear()
    : End If
    : Next
    : End Sub
    : [/code]


    ====================================================================

    I reckon it would be easier to have a separate Public Sub to
    CLEAR ALL THE CONTROLS, or have a Sub which takes a string input.
    If the string is say "C" then ClearAllTheControls ELSE clear only the controls you need to clear. You could use SELECT CASE statements instead.

    Why not use this approach?

    '-----------Start of code------------------------------------------

    Private Sub ClearControls(ByVal clrControl As String)

    clrControl=clrControl.ToUpper

    If clrControl="C" then

    'Insert your code here to clear controls of EVERY TYPE

    ElseIf clrControl = "T" Then
    DirectCast(ctl, TextBox).Clear()
    End If

    End Sub

    '---------End of code------------------------------------

    In another example i have seen on here i think you can put double quotes around commands to insert a variable name

    so instead of

    ClearAll(ctl) try putting in>>

    Dim controlname As String
    controlname="*"
    "DirectCast(ctl, controlname).Clear()"

    Try it and see, there's no harm in trying it, is there?

    Regards,

    Dr M.

  • : [b][red]This message was edited by DrMarten at 2006-2-9 10:21:55[/red][/b][hr]
    : : ok, the problem is that the line
    : :
    : : If TypeOf ctl Is ContainerControl Then
    : :
    : : dose not recognize a groupbox or any other containercontrol for that matter as a containercontrol. and the loop will progress just fine. try it out. i have working code for this i am just wondering why my intial thoughts did not work. more specifically why that line of code is not recognizing a groupbox as a containercontrol
    : :
    : : here is the working code
    : :
    : : [code]
    : : Public Sub ClearAll(ByVal ctl As Control)
    : :
    : : For Each ctl In ctl.Controls
    : : If ctl.Controls.Count > 0 Then
    : : ClearAll(ctl)
    : : ElseIf TypeName(ctl) = "TextBox" Then
    : : DirectCast(ctl, TextBox).Clear()
    : : End If
    : : Next
    : : End Sub
    : : [/code]
    :
    :
    : ====================================================================
    :
    : I reckon it would be easier to have a separate Public Sub to
    : CLEAR ALL THE CONTROLS, or have a Sub which takes a string input.
    : If the string is say "C" then ClearAllTheControls ELSE clear only the controls you need to clear. You could use SELECT CASE statements instead.
    :
    : Why not use this approach?
    :
    : '-----------Start of code------------------------------------------
    :
    : Private Sub ClearControls(ByVal clrControl As String)
    :
    : clrControl=clrControl.ToUpper
    :
    : If clrControl="C" then
    :
    : 'Insert your code here to clear controls of EVERY TYPE
    :
    : ElseIf clrControl = "T" Then
    : DirectCast(ctl, TextBox).Clear()
    : End If
    :
    : End Sub
    :
    : '---------End of code------------------------------------
    :
    : In another example i have seen on here i think you can put double quotes around commands to insert a variable name
    :
    : so instead of
    :
    : ClearAll(ctl) try putting in>>
    :
    : Dim controlname As String
    : controlname="*"
    : "DirectCast(ctl, controlname).Clear()"
    :
    : Try it and see, there's no harm in trying it, is there?
    :
    : Regards,
    :
    : Dr M.
    :
    :
    I understand what you are saying about clearing certain controls dr m. i started this sub of small and am going to build upon it.

    i'm not at my computer right now so i'll try that later.

    i am going to progam it to be an overloaded sub using the optional statement. the optional will be the tag property of the control. for example they will call the procedure like

    ClearAll(me, "+C")

    where me is the form and +C is the tag property of the control they want cleared.

    i like to start off small and debug small amounts of code rather then program a whole lot and debug alot.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories