How to add textboxes, labels and buttons dynamically at runtime in VB?

后端 未结 5 1480
滥情空心
滥情空心 2021-01-06 19:25

How to create a form with a button add_subjects which adds one textbox and a corresponding label on each click,3 buttons - Add, Edit and Dele

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 19:30

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Call AddTextBox()
    End Sub
    
    Sub AddTextBox()
    
        Dim i As Integer = 1
    
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is TextBox Then
                i = i + 1
                'MsgBox(i)
            End If
        Next ctrl
    
        Dim Label As New Label
        Label.Name = "Label" & i
        Label.Size = New Size(170, 20)
        Label.Location = New Point(200, (20 + (i * 55)))
        Label.Text = "Lbl" & i
    
        Dim Textbox As New TextBox
        Textbox.Name = "Textbox" & i
        Textbox.Size = New Size(170, 20)
        Textbox.Location = New Point(200, (38 + (i * 55)))
        Me.Controls.Add(Label)
        Me.Controls.Add(Textbox)
    
    
    End Sub
    

提交回复
热议问题