D
davidm
I have trouble getting controls created on the fly at run time to work.
For example the following code creates a TextBox with necessary
attributes.
Set x =Me.Controls.Add("Forms.TextBox.1")
With x
..Top=10
..Height=50
..Left=6
..Width=20
..Name = "TextBox1"
End With
In addition, suppose I manually add a CommandButton (at design time)
and append an event handler to it in the userform codemodule, as in:
Private Sub CommandButton1_Click()
TextBox1.Text =""
TextBox1.Text = "Enter Surname"
End Sub
I would expect that upon clicking the CommandButton, the TextBox1
should respond accordingly by getting "Enter Surname" printed. Indeed,
nothing happens!
However, the problem is averted when the TextBox is created at design
time (even programatically) as in the code below: Then the manually
crafted CommandButton and its event handler work as expected.
Set x =
ThisWorkBook.VBProjects.VBComponents.Designer.Controls.Add("Forms.TextBox1.1")
With x
..Top=10
..Height=50
..Left=6
..Width=20
..Name = "TextBox1"
End With
Why do controls created on the fly exhibit this problem?
I do know that if we programmatically use code to write event handlers
about the same time as the controls are created, the event handlers so
created *do work*. Which translates, in this example, to the need to
create the Commandbutton and its event programatically in order for
them to be functional. There seems to be no room for manual "dig" to
work on "fly controls" (although they do get along with Designer-time
controls).
Have I gotten myself in a mental tangle? I would love to have an easier
way of manipulating controls created on the fly. Any light will be
welcome.
David
For example the following code creates a TextBox with necessary
attributes.
Set x =Me.Controls.Add("Forms.TextBox.1")
With x
..Top=10
..Height=50
..Left=6
..Width=20
..Name = "TextBox1"
End With
In addition, suppose I manually add a CommandButton (at design time)
and append an event handler to it in the userform codemodule, as in:
Private Sub CommandButton1_Click()
TextBox1.Text =""
TextBox1.Text = "Enter Surname"
End Sub
I would expect that upon clicking the CommandButton, the TextBox1
should respond accordingly by getting "Enter Surname" printed. Indeed,
nothing happens!
However, the problem is averted when the TextBox is created at design
time (even programatically) as in the code below: Then the manually
crafted CommandButton and its event handler work as expected.
Set x =
ThisWorkBook.VBProjects.VBComponents.Designer.Controls.Add("Forms.TextBox1.1")
With x
..Top=10
..Height=50
..Left=6
..Width=20
..Name = "TextBox1"
End With
Why do controls created on the fly exhibit this problem?
I do know that if we programmatically use code to write event handlers
about the same time as the controls are created, the event handlers so
created *do work*. Which translates, in this example, to the need to
create the Commandbutton and its event programatically in order for
them to be functional. There seems to be no room for manual "dig" to
work on "fly controls" (although they do get along with Designer-time
controls).
Have I gotten myself in a mental tangle? I would love to have an easier
way of manipulating controls created on the fly. Any light will be
welcome.
David