vb.net panel里的控件如何遍历

我直接遍历画面中的textbox没问题,可是画面中panel中的textbox就遍历不到'功能遍历所有comboboxtextbox,并清空值DimctAsControl... 我直接遍历画面中的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
展开
 我来答
lrh3321
2015-04-01 · TA获得超过2182个赞
知道大有可为答主
回答量:2296
采纳率:75%
帮助的人:2061万
展开全部
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里,你只遍历窗体的控件是不够的。

追问
Public Sub ReSetText(Control ctrl)    程序有问题    ,能定义一个直接写在我代码里的吗
追答

抱歉最近一般用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)
匿名用户
2015-04-01
展开全部
容器不同 你用for each 找panel里的控件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式