Solved

Creating controls at run time

Posted on 1998-10-09
6
148 Views
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?
0
Comment
Question by:Taranadilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:fguerreiro_inix
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

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


Hope this helps
Regards
0
 

Author Comment

by:Taranadilla
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
0
 

Expert Comment

by:sjrl
ID: 1439026
Yes, you need a control array of labels.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:Taranadilla
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

regards
0
 
LVL 14

Accepted Solution

by:
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.

0
 
LVL 14

Expert Comment

by:waty
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month6 days, 8 hours left to enroll

634 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