注册 登录
编程论坛 VB.NET论坛

想一次将窗体中所有TextBox清空,遇到问题了。

杀虫剂 发布于 2007-12-25 13:17, 3328 次点击
为了达到一次清空所有TextBox的目的,查了很多资料。
MSDN上是这样的:
' Visual Basic 2005
Private Sub ClearText(ByVal container As Control)
    Dim ctrl As Control
    For Each ctrl In container.Controls
        If TypeOf (ctrl) Is TextBox Then
            ctrl.Text = ""
        End If
        If ctrl.HasChildren Then
             ClearText(ctrl)
        End If
    Next
End Sub

我的按钮中的代码是根据上面代码改的:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf (ctrl) Is TextBox Then
                ctrl.Text = ""
            End If
        Next
    End Sub

但是结果是清除不了,根本没有反应。
到底哪里错了呢?
谢谢!
4 回复
#2
杀虫剂2007-12-25 13:41
回复 1# 的帖子
我为了美观,所有TextBox都是在GroupBox中分组好的,试了一下,TextBox在窗口里直接创建可以清除,在GroupBox中创建就无法清除。
如果我要将GroupBox中的TextBox批量清除,该怎么做呢?
谢谢!
#3
杀虫剂2007-12-25 14:16
解决了,结贴。
自己想出来了!呵呵!

       Dim ctrl As Control
        Dim ctrlChild As Control
        For Each ctrl In Me.Controls
            For Each ctrlChild In ctrl.Controls
                If TypeOf (ctrlChild) Is TextBox Then
                    ctrlChild.Text = ""
                End If
            Next
        Next
    End Sub
#4
hezhifang2007-12-25 14:53
dddddddddd我也学习了
#5
fairy42007-12-25 15:21
Dim ctrl As Control
        Dim ctrlChild As Control
        For Each ctrl In Me.Controls
            If TypeOf (ctrl) Is TextBox Then
                ctrl.Text = ""
            End If
            If ctrl.HasChildren Then
                For Each ctrlChild In ctrl.Controls
                    If TypeOf (ctrlChild) Is TextBox Then
                        ctrlChild.Text = ""
                    End If
                Next
            End If
           
        Next
這個清除所有textbox里面的text
1