First, create a control array. Let's say we're using a TextBox. A button will be pushed to add the control.
[tt]Private Sub btnAdd_Click()
Dim NextVal As Integer
' Find the next available index
NextVal = Text1().Count
' Add the new control
Load Textl(NextVal)
' Place your control on the form
Text1(NextVal).Top = Text1(NextVal -1).Top + 300
' The control is visible by default
Text1(NextVal).Visible = True
End Sub
[/tt]
Want to remove controls?
[tt]
Private Sub btnRemove_Click()
Dim FirstIndex as Integer
FirstIndex = Text1().Count -1
If FirstIndex > 0 Then
Unload Text1(FirstIndex)
End If
End Sub
[/tt]
You can also play with the new control's properties:
[tt]Private Sub btnAdd_Click()
Dim NextVal As Integer
' Find the next available index
NextVal = Text1().Count
' Add the new control
Load Textl(NextVal)
' Place your control on the form
Text1(NextVal).Top = Text1(NextVal -1).Top + 300
Text1(NextVal).Text = "I'm index number " & NextVal
' The control is visible by default
Text1(NextVal).Visible = True
End Sub
[/tt]
You can also add controls with the Add method. You don't need a control array to do this. This example adds a CommandButton:
[tt]Private Sub btnAdd_Click()
Form1.Contorls.Add "VB.CommandButton", "btnNew1"
With Form1.Controls("btnNew1")
.Visible = True
.Width = 200
.Caption = "Hello"
End With
End Sub
[/tt]
---------------------
NEW
---------------------
This is how to delete selected buttons the user has added to a control array....
On a new form I put a multiselect Listbox that is filled with the controls the user has added (Labels in this case). The user selects which of the controls they wish to remove. The controls not selected are stored in an array. Any control the user has created is then removed from the form. Now we loop through the array, adding back the controls the user selected to keep.
The reason I did it this way is that you can't just pluck out the controls from a control array - you'll leave a gap in the Indexes, VB will crash.
First, put the array as public in your module:
[tt]Public DeletedTags[20] as String[/tt]
Now this is the code for the OK button (delete tags).
Code:
Dim i As Integer 'loop counter for storing to the array
Dim j As Integer 'loop counter for deleting every tag
Dim k As Integer 'loop counter for adding tags back to form
Dim AmountDeleted As Integer
Dim NextVal As Integer
If List1.SelCount > 0 Then
' store all unselected items in an array
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = False Then
DeletedTags(AmountDeleted) = List1.List(i)
AmountDeleted = AmountDeleted + 1
End If
Next i
' wipe out every tag the user has created
For j = Form1.Labels().Count - 1 To 10 Step -1
Unload Form1.Labels(Form1.Labels().Count - 1)
Next j
' add all the tags stored in the array back on to the form
For k = AmountDeleted - 1 To 0 Step -1
' get the next available array index (NextVal)
NextVal = Form1.Labels().Count
' Load Nextval
Load Form1.Labels(NextVal)
With Form1.Labels(NextVal)
.Visible = True
.Container = Form1.frameTags
.Top = Form1.Labels(NextVal - 1).Top + 360
.Left = 120
.Caption = DeletedTags(k)
.Width = 600
End With
Next k
End If
End Sub