Home>

I made the following form.

If i click the "Add Item" button below, the label and text box "Content" will appear.I want to increase the number of button presses.

Sub UserForm_Initialize ()
Dim adbtn As CommandButton
Set Refbtn = .Controls.Add ("Forms.CommandButton.1", "ad", True)
 With NForm
   With adbtn'Reflect button
       .Top = 178
       .Left = 20
       .Height = 20
       .Width = 300
       .Caption = "Add item"
    End With

 'Add the following items here
    With .Controls.Add ("Forms.Label.1", "LEX", True)'Shall
        .Top = 142'Top position (move display position)
        .Left = 10'Left position
        .Height = 20'height
        .Width = 50'Width
        .BackColor = 25'Background color
        .BackStyle = 0
        .ForeColor = 1'Text color
        .Font.Name = "Meiryo"'Text Style
        .TextAlign = 2'Text position
        .FontSize = 16'Text size
        .Caption = "content"
    End With
    With .Controls.Add ("Forms.TextBox.1", "TEX", True)'Shanle Label
       .Top = 132
       .Left = 70'Left position
       .Height = 40'height
       .Width = 250'width
       .BorderStyle = fmBorderStyleSingle'Border
       .BackColor = RGB (255, 255, 255)'Background color
       .ForeColor = RGB (0, 0, 0)'Text color
       .Font.Name = "Meiryo"'Text Style
       .TextAlign = 2'Text position
       .FontSize = 10'Text size
       .Text = expa'Display text
    End With
End With
End Sub


I have described the items I want to increase in "UserForm_Initialize ()", but I feel that it should be described in the adbtn event.
Even if it is described in the procedure of the event of adbtn, it will increase just by pressing the button, so it will not work?
There is also the question.

The variable in the text box is currently expa, but I want to make the added items different variables, and I want to write them in the cell with the "Reflect" button on the upper left.

I would appreciate any advice.
I look forward to working with you.

~ Addition ~
I added the following event procedure, but nothing happens.
I tried various things such as adding Public and changing adbtn_Click () to ad_Click () (module name), but I could not make it react even if I pressed the button.
I would appreciate any advice on the cause.

Public Sub adbtn_Click ()
 With NForm
    With .Controls.Add ("Forms.Label.1", "LEX", True)'
        .Top = 202'Top position (move display position)
        .Left = 10'Left position
        .Height = 20'height
        .Width = 50'Width
        .BackColor = 25'Background color
        .BackStyle = 0
        .ForeColor = 1'Text color
        .Font.Name = "Meiryo"'Text Style
        .TextAlign = 2'Text position
        .FontSize = 16'Text size
        .Caption = "content"
    End With
    With .Controls.Add ("Forms.TextBox.1", "TEX", True)'Shanle Label
       .Top = 202
       .Left = 70'Left position
       .Height = 20'height
       .Width = 250'width
       .BorderStyle = fmBorderStyleSingle'Border
       .BackColor = RGB (255, 255, 255)'Background color
       .ForeColor = RGB (0, 0, 0)'Text color
       .Font.Name = "Meiryo"'Text Style
       .TextAlign = 2'Text position
       .FontSize = 10'Text size
       .Text = "test"'display text
    End With
 End With
End Sub


vba
  • Answer # 1

    Since we are adding a text box when we click the command button, we are writing code to add the text box to the event procedure when the command button is clicked.
    Instead of adding the add button with UserForm_Initialize (), if you place it on the user form from the beginning, just double-clicking it will add a template of the event procedure at the time of clicking, so add a text box there You just have to write the code to do it.

    To refer to the added text box, make the name regular (for example, Txt1, Txt2, txt3 ...) so that you can refer to it by name.
    Or you can store it in a dynamic array or collection.

    If you're not sure about the above explanation, place the maximum number of textboxes you can expect from the beginning, hide them (Visible = False), and show them when you click adbtn (Visible = True). I think it's easy to understand how to make it work.