INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Visual Basic (Microsoft) Versions 5/6 FAQ

Run Time

How do I add/remove controls at runtime? by JESTAR
Posted: 17 Jul 03 (Edited 14 Apr 04)

First, create a control array. Let's say we're using a TextBox. A button will be pushed to add the control.

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


Want to remove controls?


Private Sub btnRemove_Click()
    Dim FirstIndex as Integer

    FirstIndex = Text1().Count -1
    If FirstIndex > 0 Then
        Unload Text1(FirstIndex)
    End If
End Sub


You can also play with the new control's properties:

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


You can also add controls with the Add method. You don't need a control array to do this. This example adds a CommandButton:

Private Sub btnAdd_Click()
    Form1.Contorls.Add "VB.CommandButton", "btnNew1"
    With Form1.Controls("btnNew1")
         .Visible = True
         .Width = 200
         .Caption = "Hello"
    End With
End Sub


---------------------
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:

Public DeletedTags[20] as String

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

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close