Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create controls in run time

Posted on 2001-06-26
2
Medium Priority
?
151 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
[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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

730 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