Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding textbox at runtime

Status
Not open for further replies.

joelxez

Programmer
Apr 18, 2002
67
0
0
PH
Hi,

Any one can give some codes on how to add textbox at runtime? Let say i have one textbox in a form then when you run it in a load event it will add another textbox?
any suggestions are accepted...


jef,
 
If you are trying to create a control array,

Then the code is pretty simple,

Just set the Index = 0 of the textbox in design mode.

then add the following code:

Option Explicit

Private Sub Form_Load()
Dim txtbox As TextBox

Set txtbox = Text1(1) ==> 1 = index of the new textbox.

Load txtbox

txtbox.Left = 0
txtbox.Top = 0
txtbox.Visible = True

End Sub

Now you have a control array.
Tou can reuse thsi code, just be sure to alter the index
 
nickske80,

I try to use For Loop but its having an error...

For i = 1 To 3

Set Text1 = Me.Controls.Add("VB.TEXTBOX", "MyTextBox")
With Text1
.Visible = True
.Top = 10
.Left = 2000
.Height = 100

End With

Next i


jef,
 
I tried another one but "Object Already Loaded" Error is displayed..
pls help...


Dim i
For i = 1 To 3
Dim txtbox As TextBox

Set txtbox = Text1(i) textbox.

Load txtbox

txtbox.Left = 0
txtbox.Top = 0
txtbox.Visible = True
Next i

Jef,
 

Start a new project and add a text box to Form1. In the properties window set it's index to 0 (zero). Then place the following code in the form.
[tt]
Option Explicit

Private Sub Form_Load()
Dim I As Integer

Text1(0).Left = 30
Text1(0).Top = 30


For I = 1 To 3
Load Text1(I)
Text1(I).Left = Text1(0).Left
Text1(I).Top = Text1(I - 1).Top + Text1(I - 1).Height + 30
Text1(I).Visible = True
Next I

End Sub
[/tt]

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top