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

Comment Utility
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

Comment Utility
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

Comment Utility
Yes, you need a control array of labels.
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Author Comment

Comment Utility
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
Comment Utility
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

Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 62
Excel - Save a copy of work book 13 79
How does CurrentUser work? 10 25
Add a task in Outlook from access 11 32
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 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

8 Experts available now in Live!

Get 1:1 Help Now