Creating controls at run time

Posted on 1998-10-09
Last Modified: 2010-04-30
Is it possible to create a label control at run time as opposed to design time.  If so what's the way to code it?
Question by:Taranadilla

Expert Comment

ID: 1439024
Yes, but you must have alredy a label  on the form.

You need to create an array of labels.
label1 with index equal to 0.

At run time:
dim Varlbl as label

set Varlb = New  label1 =0
Varlb.left =0
Varlb.visible = true

Hope this helps

Author Comment

ID: 1439025
Can you give me some  more information.  Are you talking about a control array of labels?
Otherwise where do I set up this array.

At the moment I am getting the message "Invalid use of New keyword" using your code

Expert Comment

ID: 1439026
Yes, you need a control array of labels.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1439027
Sorry I have found out how to do it from another source.  You use the "Load" command
load label1(ix)

The new keyword could not be used in this context

LVL 14

Accepted Solution

waty earned 100 total points
ID: 1439028
There are three ways to create a control array at design time:
·      Assign the same name to more than one control.
·      Copy an existing control and then paste it onto the form.
·      Set the control's Index property to a value that is not Null.
Note   You must create menu control arrays in the Menu Editor. For details on how to do this, see "Creating and Modifying Menus at Run Time" in "Creating a User Interface."
      To add a control array element by changing its name
1      Draw the controls you want to be in the control array. (The controls must all be of the same type.) Decide which control will become the first element in the array.
2      Select one of the controls and change its Name setting to the Name setting for the first element in the array.
3      When you type an existing name for a control in the array, Visual Basic displays a dialog box asking you to confirm that you want to create a control array. Choose Yes to confirm the action.
For example, if the name of the first element in a control array is cmdCtlArr, you would choose a command button to add to the array and then set its name to cmdCtlArr. The message "You already have a control named 'cmdCtlArr.' Do you want to create a control array?" is displayed. Choose Yes to confirm the operation.
Controls added this way share only their Name property and control type; all other properties remain the same as when the control was originally drawn.
      To add a control array element by copying an existing control
1      Draw a control in the control array.
2      While the control has the focus, choose Copy from the Edit menu.
3      From the Edit menu, choose Paste. Visual Basic displays a dialog box asking you to confirm that you want to create a control array. Choose Yes to confirm the action.
This control is assigned an index value of 1. The first control you drew has a value of 0.
The index value of each new array element corresponds to the order in which the element was added to the control array. When controls are added this way, most of the visual properties, such as height, width, and color, are copied from the first control in the control array to the new controls.

LVL 14

Expert Comment

ID: 1439029
The control array example demonstrates how controls — in this case, option buttons — are added and deleted at run time. The example allows the user to add option buttons that change the background color of a picture box.
Start with a form, and then draw a picture box, a label, two option buttons, and three command buttons, as shown in Figure 7.4.
Figure 7.4   Adding controls at run time

The following table lists the property settings for the objects in the application.
Object Property Setting

Form Caption Control Array Example
Picture box Name picDisplay
Label Caption Select an option button to display a new color
Option1 NameIndex optButton0
Option2 NameIndex optButton1
First command button NameCaption cmdAdd&Add
Second command button NameCaption cmdDelete&Delete
Third command button NameCaption cmdClose&Close

Events in the Control Array Application
Next, you need to add the event procedures for the option buttons and command buttons. Start by adding the form declaration:
Dim MaxId As Integer

The Click event procedure is shared by all the option buttons:
Private Sub optButton_Click (Index As Integer)
picDisplay.BackColor = QBColor(Index + 1)
End Sub

New option buttons are added by the Click event procedure for the Add command button. In this example, the code checks that no more than ten option buttons are loaded before the Load statement is executed. Once a control is loaded, its Visible property must be set to True.
Private Sub cmdAdd_Click ()
If MaxId = 0 Then MaxId = 1 ' Set total option
' buttons.
If MaxId > 8 Then Exit Sub ' Only ten buttons
' allowed.
MaxId = MaxId + 1 ' Increment button count.
Load optButton(MaxId) ' Create new button.
optButton(0).SetFocus ' Reset button selection.
' Set new button under previous button.
optButton(MaxId).Top = optButton(MaxId - 1)._
Top + 400
optButton(MaxId).Visible = True ' Display new
' button.
optButton(MaxId).Caption = "Option" & MaxId + 1
End Sub

Option buttons are removed by the Click event procedure for the Delete command button:
Private Sub cmdDelete_Click ()
If MaxId <= 1 Then Exit Sub ' Keep first two
' buttons.
Unload optButton(MaxId) ' Delete last button.
MaxId = MaxId - 1 ' Decrement button count.
optButton(0).SetFocus ' Reset button selection.
End Sub

The Close button Click event ends the application:
Private Sub cmdClose_Click ()
Unload Me
End Sub

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

679 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