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

后端 未结 5 1478
滥情空心
滥情空心 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
    
    0 讨论(0)
  • 2021-01-06 19:31

    Create dynamic Textbox]

    Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
            Dim textbox1 As New TextBox
            textbox1.Name = "Textbox1"
            textbox1.Size = New Size(170, 20)
            textbox1.Location = New Point(167, 32)
            GroupBox1.Controls.Add(textbox1)
      End Sub
    

    Create Dynamic Label]

    Private Sub lblCreateLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCreateLabel.Click
        Dim label1 As New Label
        label1.Name = "label1"
        label1.Text = "Enter Name"
        label1.AutoSize = True
        label1.Location = New Point(80, 33)
        GroupBox1.Controls.Add(label1)
    End Sub
    

    Refer Here

    Source

    0 讨论(0)
  • 2021-01-06 19:37
    Private Property number as Integer=1
    
    Private Sub add_subject_Click(sender As Object, e As EventArgs) Handles add_subject.Click
        Dim tb As New TextBox
        tb.Name="TextBox"+number.ToString
        tb.Position = New Point(number*40,10) ' change this if you want
        Me.Controls.Add(tb)
        Dim lb As New Label
        lb.Name="Label"+number.ToString
        lb.Position = New Point(number*40,50) ' change this if you want
        Me.Controls.Add(lb)
        Dim add As New Button
        add.Name="AddButton"+number.ToString
        add.Position = New Point(number*40,100) ' change this if you want
        AddHandler(add.Click, AdressOf(add_Click))
        Me.Controls.Add(add)
        Dim edit As New Button
        edit.Name="EditButton"+number.ToString
        edit.Position = New Point(number*40,150) ' change this if you want
        AddHandler(edit.Click, AdressOf(edit_Click))'you have to make edit_Click
        YourForm.Controls.Add(edit)
        Dim delete As New Button
        delete.Name="DeleteButton"+number.ToString
        delete.Position = New Point(number*40,200) ' change this if you want
        AddHandler(delete.Click, AdressOf(delete_Click))'you have to make delete_Click
        Me.Controls.Add(delete)
        number+=1
    End Sub
    

    So we make all controls, dynamically make names, change positions, add handlers and add controls to form.

    Private Sub add_Click(sender As Object, e As EventArgs)
        Ctype(Me.Controls.Find("Label"+sender.Name.Substring(9),True).First,Label).Text = Ctype(Me.Controls.Find("TextBox"+sender.Name.Substring(9),True).First,TextBox).Text 
    End Sub
    

    Here we find Label And TextBox using sender's number(sender.Name.Substring(9) will remove AddButton and leave number) and change Label.Text to TextBox.Text.

    Get all label values and insert them in database:

    Private Sub save(sender As Object, e as EventArgs) Handles button_save_subjects.Click
    For i = 1 to number
        Dim value As String
        value = CType(Me.Controls.Find("Label"+number.ToString).First,Label).Text
        'insert into database
    Next
    End Sub
    
    0 讨论(0)
  • 2021-01-06 19:37

    You can use the same code as above and, in the end, use the parent property of control. Because the control (TextBox, Buttom, etc.) is "inside" of a "container" (form, groupbox, etc.). Like this...

      ...
      Dim textbox1 As New TextBox
      textbox1.Name = "Textbox1" 'or other
      ...
      textbox1.parent = Me 'Me = the form
      ...
    
    0 讨论(0)
  • 2021-01-06 19:45

    A control like a textbox is just an object of the class Textbox. In order for the form to display this object it needs to be added to the form's Controls property. To create a new textbox all you need to do is

    Dim newTB as New Textbox
    newTB.Name = "tbNew"
    'Set location, size and so on if you like
    Me.Controls.Add(newTB)
    

    If you want your control to be able to respond to events you need to add an event handler for the event you want to the control. This handler refers the event to a method of your choice.

    Public Class Form1
    
      Sub CreateTB
        Dim NewTB as New Textbox
        newTB = New Textbox
        newTB.Name = "tbNew"
        AddHandler newTB.TextChanged, AddressOf HandleTextChanged
        Me.Controls.Add(newTB)
      End Sub
    
    
      Private Sub HandleTextChanged(sender as Object, e as EventArgs)
        'Handle the event
      End Sub
    End Class
    

    You should make sure that the names are unique if you are creating the controls or you might run into trouble.

    You can also store your created controls in an array or list as a global variable. That way you can easily access them afterwards.

    0 讨论(0)
提交回复
热议问题