VB.NET - Iterating through controls in a container object

后端 未结 10 1319
别那么骄傲
别那么骄傲 2020-12-03 05:18

I have a form with a \"Clear\" button.

When the user clicks \"Clear\", I want to clear the value of all the visible elements on the form. In the case of date contro

10条回答
  •  爱一瞬间的悲伤
    2020-12-03 05:59

    This comes straight from an article discussing techniques to use now that Control Arrays have been done away with going from VB6 to VB.NET.

    Private Sub ClearForm(ByVal ctrlParent As Control)
        Dim ctrl As Control
        For Each ctrl In ctrlParent.Controls
            If TypeOf ctrl Is TextBox Then
               ctrl.Text = ""
            End If
            ' If the control has children, 
            ' recursively call this function
            If ctrl.HasChildren Then
                ClearForm(ctrl)
            End If
        Next
    End Sub
    

提交回复
热议问题