Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create controls in run time

Posted on 2001-06-26
2
Medium Priority
?
152 Views
Last Modified: 2010-05-02
Hi,

Is there a way to create dynamically (in Run time) controls without putting them at design time first as control arrays?

Thanks.
0
Comment
Question by:amavr
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 400 total points
ID: 6227312
Only if you want individual controls, you cannot (AFAIK) create arrays of controls dynamically.

This example from MSDN shows how.

Add Method (Controls Collection) Examples
Private Sub Form_Load()
   Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
   With Form1!cmdObj1
      .Visible = True
      .Width = 2000
      .Caption = "Dynamic Button"
   End With
End Sub

Note   The code example above uses ! as a syntax element. You can also use standard collection syntax such as Form1.Controls("cmdObj1") to reference the control.

This second example declares an object variable of type CommandButton using the WithEvents keyword, allowing you to program the events of the control. The object variable is set to the reference returned by the Add method. To try the example, paste the code into the Declarations section and run the project.

Option Explicit
Private WithEvents btnObj As CommandButton

Private Sub btnObj_Click()
   MsgBox "This is a dynamically added button."
End Sub

Private Sub Form_Load()
   Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
   With btnObj
      .Visible = True
      .Width = 2000
      .Caption = "Hello"
      .Top = 1000
      .Left = 1000
   End With
End Sub

The third example adds an unreferenced control to the Controls collection. To program such a control's events, however, you must declare an object variable of type VBControlExtender, and set the reference returned by the method to the object. Then program the control's events using the ObjectEvent event.

Option Explicit
Dim ctlExtender As VBControlExtender

Private Sub Form_Load()
   Set ctlExtender = Controls.Add("Project1.UserControl1", "MyControl")
   With ctlExtender
      .Visible = True
      .Top = 1000
      .Left = 1000
   End With
End Sub

Private Sub extObj_ObjectEvent(Info As EventInfo)
   ' Program the events of the control using Select Case.
   Select Case Info.Name
   Case "UserName"
      ' Check User name value.
      MsgBox Info.EventParameters("UserName").Value
   ' Other cases now shown
   Case Else ' Unknown Event
      ' Handle unknown events here.
   End Select
End Sub

0
 

Expert Comment

by:mhowling
ID: 6230366
If you want an array of controls you will need an initial control to create from.  This is often referred to as SPAWNING or CLONING.

First create a new project and add a command button to the form.  Set the index property of this command button to 0 (Zero).  Then add the following code.

Private Sub Command1_Click(Index As Integer)
    Dim NewIndex As Long
   
    NewIndex = Command1.Count
    Load Command1(NewIndex)
   
    'The new control is created in the same position as the original
    'so we need to position it.  In this case underneath the original
    Command1(NewIndex).Top = Command1(NewIndex - 1).Top + Command1(NewIndex - 1).Height
    'New controls also start off invisible
    Command1(NewIndex).Visible = True
End Sub

Hope this is of some help and is not patronising.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Suggested Courses

916 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