我直接遍历画面中的textbox没问题,可是画面中panel中的textbox就遍历不到'功能遍历所有comboboxtextbox,并清空值DimctAsControlForEachctInMe.ControlsIf(TypeOfctIsTextBox)Thenct.... 我直接遍历画面中的textbox没问题,可是画面中panel中的textbox就遍历不到

'功能 遍历 所有 combobox textbox,并清空值

Dim ct As Control
For Each ct In Me.Controls
If (TypeOf ct Is TextBox) Then
ct.Text = ""
ElseIf (TypeOf ct Is ComboBox) Then
Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
cb.SelectedIndex = -1
End If
Next
Public Sub ReSetText(Control ctrl)        
        Dim ct As Control
        For Each ct In ctrl.Controls
            Try
                For Each ct2 As Control In ct.Controls
                    ReSetText(ct2)
                Next
            Catch
            
            End Try

            If (TypeOf ct Is TextBox) Then
                ct.Text = ""
            ElseIf (TypeOf ct Is ComboBox) Then
                Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
                cb.SelectedIndex = -1
            End If
        Next
End Sub

因为textbox在窗体里的panel里,你只遍历窗体的控件是不够的。

抱歉最近一般用c#把语法弄混了,解决这个问题要靠递归,所以写成一个方法比较好。

     Public Sub ReSetText( ctrl As Control)        
        Dim ct As Control
        For Each ct In ctrl.Controls
            Try
                For Each ct2 As Control In ct.Controls
                    ReSetText(ct2)
                Next
            Catch
             
            End Try
 
            If (TypeOf ct Is TextBox) Then
                ct.Text = ""
            ElseIf (TypeOf ct Is ComboBox) Then
                Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
                cb.SelectedIndex = -1
            End If
        Next
End Sub

用的时候就直接是

ReSetText(Me)