LyessD
LyessD

Reputation: 221

How to dynamically position textboxes one below the other in vba

I am working on a Userform where I want to add textboxes every time I click a commandButton. The below code does this :

Private Sub CommandButton1_Click()
    Dim editBox As Control   
    Set editBox = UserForm1.Controls.Add("Forms.TextBox.1")
End Sub

However I think I only add one textbox on click, am I right ? If so, is there a way to add as many textbox as I click on commandButton ?

Also my big issue is to position each textboxes added below each other like in column. Is there a way to do it ?
Or do you suggest me to change the way to add textboxes ?

Thank you

Upvotes: 1

Views: 2161

Answers (1)

T.M.
T.M.

Reputation: 9948

Arrange textboxes created on the fly

Each click creates a new textBox, without assigning a (changing) value to the .Top property they got overlapped. Without a name you won't be in the position to refer to them later.

Private Sub CommandButton2_Click()
    Static i
    Dim editBox As MSForms.Control
    Set editBox = Me.Controls.Add("Forms.TextBox.1")
    i = i + 1
    editBox.Name = "myBox" & i
    editBox.Top = i * editBox.Height
End Sub

Upvotes: 2

Related Questions