
How to create a text box in run time ?
This is essentially no different to using the VB form designer; the form
designer declares the object 'WithEvents', and performs the CreateObject()
call for you (Controls.Add simply calls CreateObject() then adds the control
to the collection). Doing it manually only takes implementation time, and
is probably slower.
In addition, your solution would allow only one instance of a textbox to be
created dynamically; the previous textbox is lost as soon as you attempt to
create an additional instance using the same method.
Surely, if you know how many textboxes you are going to need at design-time,
why wait until run-time to create them. Surely not so you can specify
location, size, style, etc. manually?
==============================
Nik Rivers
Software Development Engineer
Gladstone PLC [www.gladstoneplc.com]
Quote:
> To create a TextBox (or any control) at runtime use this:
> 'In the Declarations section of the Form:
> 'After this declaration is in place you can
> 'look in the "objects" dropdown list and see
> 'txtMyTextBox with all of its events in place.
> 'You can even add code in the events.
> Private WithEvents txtMyTextBox As TextBox
> 'In the Form_Load event or wherever you'd like:
> Private Sub Form_Load()
> Dim fScale As Single
> 'Create the Control (Syntax - Add("ControlType", "ControlName")
> Set txtMyTextBox = Me.Controls.Add("VB.TextBox", "txtMyTextBox")
> 'Get scale for Twips on this form
> fScale = Me.ScaleX(1, vbTwips, Me.ScaleMode)
> 'Move it into place, add some text and make it visible
> With txtMyTextBox
> .Move 90 * fScale, 90 * fScale, 1500 * fScale, 315 * fScale
> .Text = .Name
> .Visible = True
> End With
> End Sub
> Hope this helps'
> Rocky Clark (Kath-Rock Software)
> > Hi,
> > I am trying to create a textbox in run time (not drug & drop from the
tool
> > box, but coding it and createing it in run time). Any information about
> how
> > to do it ?
> > Thanks,
> > Zohar