Solved

Duplicate text box

Posted on 2000-03-27
17
214 Views
Last Modified: 2010-05-02
I have a text box object on my VB form.
How can I duplicate it in a run-time mode?
0
Comment
Question by:Elad22
  • 5
  • 3
  • 2
  • +6
17 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2661039
Pardon? You mean like a control array?

Give it an Index property of 0

(say it's called Text1)

then just load as you need

Load Text1(1)
Text1(1).Left = some value
Text1(1).top = some value
etcetera....

and finally, most important:
Text1(1).Visible = True

And repeat for 2, 3, 4.... etc

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2661043
I'm going to assume that you did indeed mean a control array. The above answers your question. If not, just reject the answer...

Also check out:

Working with Control Arrays


A control array is a group of controls that share the same name and type. They also share the same event procedures. A control array has at least one element and can grow to as many elements as your system resources and memory permit; its size also depends on how much memory and Windows resources each control requires. The maximum index you can use in a control array is 32767. Elements of the same control array have their own property settings. Common uses for control arrays include menu controls and option button groupings.

Note   Visual Basic includes the ability to dynamically add unreferenced controls to the Controls collection at run time. This topic refers only to referenced controls added at design time by cutting and pasting a control onto the form. For more information about adding controls at run time, see the reference topic "Add Method (Controls Collection)" and "Add Method (Licenses Collection)."

Why Use Control Arrays?
Adding controls with control arrays uses fewer resources than simply adding multiple controls of the same type to a form at design time. Control arrays are also useful if you want several controls to share code. For example, if three option buttons are created as a control array, the same code is executed regardless of which button was clicked.

If you want to create a new instance of a control at run time, that control must be a member of a control array. With a control array, each new element inherits the common event procedures of the array.

Using the control array mechanism, each new control inherits the common event procedures already written for the array. For example, if your form has several text boxes that each receive a date value, a control array can be set up so that all of the text boxes share the same validation code


And:

Adding to a Control Array at Run Time


You can add and remove controls in a control array at run time using the Load and Unload statements. However, the control to be added must be an element of an existing control array. You must have created a control at design time with the Index property set, in most cases, to 0. Then, at run time, use this syntax:

Load object(index%)

Unload object(index%)

Argument Description
object Name of the control to add to or delete from the control array.
index% The control's index value in the array.


When you load a new element of a control array, most of the property settings are copied from the lowest existing element in the array — in this example, the element with the 0 index value. The Visible, Index, and TabIndex property settings are not automatically copied to new elements of a control array, so to make the newly added control visible, you must set its Visible property to True.

Note   Visual Basic generates an error if you attempt to use the Load statement with an index number already in use in the array.

Important   You can use the Unload statement to remove any control created with Load. However, you cannot use Unload to remove controls created at design time, regardless of whether or not they are part of a control array.

0
 
LVL 1

Author Comment

by:Elad22
ID: 2661144
I mean instance.
You answer is good for design environment and not for a run-time environment.
0
 
LVL 18

Expert Comment

by:deighton
ID: 2661153
at run time with an existing control array of one element

Load Text1(1)
Text1(1).Visible = True
Text1(1).Left = 1 'position it here



...

then for 2,3,4 etc if you want
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 total points
ID: 2661215
caraf_g gave the solution...

Private Sub Command1_Click()
    Dim ndx As Integer
    ndx = txtArray.UBound + 1
    Load txtArray(ndx)
    txtArray(ndx).Top = txtArray(ndx - 1).Top + txtArray(ndx - 1).Height
    txtArray(ndx).Visible = True
End Sub
0
 
LVL 4

Expert Comment

by:pcumberbatch
ID: 2661225
Is this what you are looking for ?

Try typing some text in the box before you click the button

Dim NewTextBox As Object
Private Sub Command1_Click()
   Set NewTextBox = Text1
   NewTextBox.Text = "HELLO"
'remove the line above and the
'text in Text1 will remain

End Sub


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2661438
"You answer is good for design environment and not for a run-time environment"

Wrong.
0
 

Expert Comment

by:R_N_WARD
ID: 2661467
Do you want another text box to be opened at runtime? if so, create a new texbox and set the VISIBLE option to FALSE.  When you want this object to apper, such as when you press command1, place this line of code:

Text2.Visible = True

I am sorry if this does not help. This is only a Guess.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Expert Comment

by:R_N_WARD
ID: 2661472
Do you want another text box to be opened at runtime? if so, create a new texbox and set the VISIBLE option to FALSE.  When you want this object to apper, such as when you press command1, place this line of code:

Text2.Visible = True

I am sorry if this does not help. This is only a Guess.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2661480
elad22 writes "I mean instance.
You answer is good for design environment and not for a run-time environment"

Please note that in Caraf_g's comment.. no where is there a mention of doing anything in the IDE other than setting the Text1.Index property to 0. This  will add an Index parameter on each Text1 event procedure.. telling you which Text1 fired.. so you can program accordingly. <you will have to change any existing Text1 event procedures that do not have an Index parameter to have one>.

By the way.. ALL of the above Load Text1 code snippets work (except pcumberbatch's)!!! BUT.. only in a late post by Erick37.. did anyone happen to  mention to change the new Textbox's dimensions so that it doesn't appear right on the top of the last textbox.. <lol> and a <wink>.
0
 
LVL 1

Expert Comment

by:jbathija
ID: 2661536
Option Explicit
Dim i As Integer

Private Sub Command1_Click()
i = i + 1
Load Text1(i)
Text1(i).Left = i * 50
Text1(i).Top = i * 50
Text1(i).Visible = True
End Sub


put this out in a form witha textbox named text1 and a command button named command1 and try it out
0
 

Expert Comment

by:JimmieToo
ID: 2661546
Elad22:

You are way off base!  The answers given are correct.  To create controls at runtime, you must have one invisible on the form with the index set at zero.  Load as many new duplicates with appropriate indeces as required.  Set their visible property to true.

This approach is the one I know and works in both IDE and RT.  Assign the points!

Jim
0
 
LVL 1

Author Comment

by:Elad22
ID: 2661616
Erick37 answered right. I like his answer.
0
 
LVL 1

Author Comment

by:Elad22
ID: 2661622
Thank you all, guys!!!
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2661928
My comment: "Paid For By The Comittee To Reelect Erick37 Answer President".. Oh what a thankless job it is.. LOL.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2663172
Hng.. hngggggggg gngngngngng......

That's just flippin' great that is lads..

"Accepted Answer  
From: Erick37
 Date: Monday, March 27 2000 - 05:36PM IST  
caraf_g gave the solution..."

etceterahr....

mutter mutter mutter mutter, growl, mutter mutter mutter...
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2663205
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now