Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 4

Adding and Removing a Control in the Controls Collection in Visual Basic 6

Once you've declared the intrinsic or ActiveX control object and programmed its events, you can add it to the Controls Collection with a line of the format:

Set objvariable = Controls.Add(strControlType, strControlName)

where objvariable is the name of the variable holding the control's instance, strControlType is a string containing the ProgID for this control's type, and strControlName is a string containing a unique name for the control (can be the same as objvariable).

Once you've added the control, you need to set its object variable's properties appropriately to the needs of the application and set the Visible property to True. The method for referring to a control object's members varies slightly, depending on whether the control is an intrinsic or an ActiveX control:

  • If the control is an intrinsic VB control, then simply refer to the control's members with the familiar objvariable.member syntax. Listing 4.12 gives an example that sets the properties of an intrinsic control that's just been added to the Controls Collection.

    LISTING 4.12
    REFERRING TO AN INTRINSIC CONTROL'S MEMBERS


    Set cmdbutton = Controls.Add( _
    "VB.CommandButton", _
    "CmdButton")
    cmdbutton.Caption = "XXXXX"
    cmdbutton.Left = 500
    cmdbutton.Top = 500
    cmdbutton.Visible = True
  • If the control is an ActiveX control, then you must refer to the control's custom members through the control's object property with syntax in the format objvariable.object.member. Listing 4.13 gives an example that sets the properties of an ActiveX control that's just been added to the Controls Collection. Note in the example that you do not use the Object property when referring to a standard property of the VB environment. You only use the Object property when referring to custom members. Referring to custom members without the Object property or to standard members with the Object property will cause a runtime error.

LISTING 4.13
REFERRING TO AN ACTIVEX CONTROL'S STANDARD AND CUSTOM MEMBERS

'Add the control
Set mscCalendar = Controls.Add( _
strControlType, _
"mscCalendar")
'Set a custom property
mscCalendar.object.Day = 15
'Set standard properties
mscCalendar.Top = 600
mscCalendar.Left = 500
mscCalendar.Visible = True

When you're finished with the control, you can take it out of the Controls Collection with a line of the format:

Controls.Remove objvariable

where objvariable is the name of the variable holding the control's instance.

Adding and Deleting Controls Dynamically Using the Controls Collection Topics

Related Topics

See Also


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved