动态生成控件并定位

来源:互联网 发布:轻淘客cms网站 编辑:程序博客网 时间:2024/05/16 10:44
 

Public Class form1

    Private a(0 To 8) As Control
    Private b(0 To 8) As Object

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        For i As Int16 = 0 To 8
            If i < 3 Then
                b(i) = New Int16
                b(i) = 5
                Debug.WriteLine(CType(b(i), Integer) & " 是" & TypeName(b(i)), ToString)
            ElseIf i < 6 Then
                b(i) = New String(CType("Welcome", Char()))
                b(i) = "Welcome"
                Debug.WriteLine(CType("Welcome", Char()) & " 是" & TypeName(b(i)), ToString)
            ElseIf i < 7 Then
                b(i) = New Label With {.BorderStyle = BorderStyle.Fixed3D, .Size = New Size(60, 60), .Location = New Point(60, 220)}
                Controls.Add(CType(b(i), Control))
                If TypeOf b(i) Is Label Then
                    Debug.WriteLine(TypeName(b(i)) & "是标签", ToString)
                End If
            End If
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim a(0 To 8)
        For i As Int16 = 0 To 8
            If i < 3 Then
                a(i) = New Label With {.BorderStyle = BorderStyle.Fixed3D, .Size = New Size(60, 60), .Location = New Point(i * 60, 0)}
                Controls.Add(a(i))
            ElseIf i < 6 Then
                a(i) = New Button With {.Size = New Size(60, 60), .Location = New Point((i - 3) * 60, 70)}
                Controls.Add(a(i))
            ElseIf i < 9 Then
                a(i) = New TextBox With {.Size = New Size(60, 60), .Location = New Point((i - 6) * 60, 150)}
                Controls.Add(a(i))
            End If
        Next
    End Sub
End Class

本程序在 Visual Basic.net 2010下设计通过


原创粉丝点击