• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Creating controls at run time

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?
1 Solution
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

Varlb.top =0
Varlb.left =0
Varlb.visible = true

Hope this helps
TaranadillaAuthor Commented:
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
Yes, you need a control array of labels.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

TaranadillaAuthor Commented:
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

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.

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now