Solved

Create controls in run time

Posted on 2001-06-26
2
145 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now