Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating visual objects at run time 2

Status
Not open for further replies.

PiniHadad

Technical User
Apr 23, 2001
4
US
Does anyone know how to make a new object such as a shape or label at run time? thanks.
 
You can "clone" an existing control that is in a control array (define one control of the type you want and set the Index to 0) on the form using Load. I define the controls I need with .Visible = false and Index = 0. Index must be asignbed at design time. I use constants to address the new controls.
Code:
'*****
'Module Declarations
Private Const idxGoback = 1
Private Const idxRefresh = 2
Private Const idxEport = 3
Private Const idxLookup = 4
Private Const idxStop = 5
Private Const idxExit = 6
'.............. later
    lngLeft = 60 ' Left of first new command
    With mform
        .Command1(0).Visible = False
    ' Create 5 New command buttons based on Command1(0)
        For I = 1 To 6
            Load .Command1(.Command1.Ubound + 1)
            With Command1((.Command1.Ubound )
                .Left = lngLeft
                lngLeft = .Left + .Width + 30
                .Visible = True
            End With
        Next
        
        .Command1(idxGoback).Caption = "Go Back"
        .Command1(idxRefresh).Caption = "Refresh"
        .Command1(idxEport).Caption = "EPort"
        .Command1(idxLookup).Caption = "Lookup"
        .Command1(idxStop).Caption = "Stop"
        .Command1(idxExit).Caption = "Exit"
    End With
'*** Delete Them backwards
        For I = Ubound(mform.command1 to 1 Step -1 ' Not 0
            On error Resume next ' Index may not Exist
                Unload mform.Command1(I)
            On Error GOTO 0
        Next
Don't ever Unload a control in one of its events.
 
Another option is to pass in the Control's ProgID and a control name into the Add fuction on the form's control collection's add function. That will create the control and return a reference to that new control. For example . . .

Code:
   Dim ctlTextBox As VB.TextBox
   Set ctlTextBox = Form1.Controls.Add("vb.TextBox", "txtSomeTextBox")
   
   With ctlTextBox
      .Top = 500
      .Left = 500
      .Height = 300
      .Width = 2000
      .Visible = True
   End With


If you want to capture events off of the control, you can create a class wrapper around a reference to your control type that was dimmed WithEvents . . . that gets a bit more involved, but it is not as bad as it sounds.
- Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top