遍历控件 vb.net 2005

本文介绍了一个使用Visual Basic编写的程序,该程序能够遍历窗体上的所有控件,并根据控件类型显示不同的消息框提示。此外,程序还通过运行时类型检查来确定控件的具体类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Cont As Control
        Dim i as integer
        txtbox1.Text = "txt1"
        txtbox2.Text = "txt2"

        'Controls(0).Text = "Hello"
        Controls.Item(0).Text = "Hello"
        For i = 0 To Controls.Count - 1
            Debug.WriteLine(Controls(i).GetType)

            'Me.Controls(ii).GetType.Name()
        Next

        i = 0
        Dim T As Object
        For Each Cont In Me.Controls

            Select Case Cont.GetType.Name
                Case "CheckBox"
                    MsgBox("is checkbox")
                Case "TextBox"
                    MsgBox("is textbox")
                Case "Label"
                    MsgBox("Label")
                Case "Button"
                    MsgBox("is Button")
            End Select

            i += 1
            If TypeOf Cont Is TextBox Then
                MessageBox.Show("The control is a textbox." & _
                       " Control:" & i & vbCrLf & Cont.Text)
            ElseIf TypeOf Cont Is Button Then
                MsgBox("The control is a button." & " Control:" & i)
            ElseIf TypeOf Cont Is CheckBox Then
                MsgBox("The control is a checkbox." & " Control:" & i)
            ElseIf TypeOf Cont Is Label Then
                MsgBox("The control is a Label." & " Control:" & i)

            End If
          
        Next Cont
    End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值