adding a components in Visual Basic


How is it possible, in visual basic, to add a component (like a rectangle) in code ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
Here is an example to do just that, for more info see help: Add Method (Controls Collection)

    Dim shpOne As Shape
    Set shpOne = Me.Controls.Add("VB.Shape", "shpOne")
    With shpOne
        .Left = 100
        .Top = 100
        .Visible = True
        .FillColor = vbGreen
        .FillStyle = 7
        .Shape = 1
    End With

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nope.  Not possible.

Your EXE must contain the references to your registry where to find components.  Adding references to your compiled code is not possible.

You must specify all components if you want to reference them from code later (to dynamically create a control or something)
TimCotteeHead of Software ServicesCommented:
vindevogel, if you look at the help text you will see that it is possible to add an unreference control, it is a little bit of extra code but IS possible. Here is an extract from MSDN.

Adding Unreferenced Controls at Run Time
You can also use the Add method to dynamically add a control that is not referenced in the project. (An "unreferenced" control is a control that is not present in the Toolbox.) To do so, you must also add the control's License key to the Licenses collection as well. The example below adds a control's license key before adding the control itself:

Option Explicit
Private WithEvents extCtl As VBControlExtender

Private Sub Form_Load()
   Licenses.Add "prjWeeks.WeeksCtl", "xydsfasfjewfe"
   Set extCtl = Form1.Controls.Add("prjWeeks.WeeksCtl", "ctl1")
   extCtl.Visible = True ' The control is invisible by default.
End Sub

Note   See Add Method (Licenses Collection) in the See Also list for more information about retrieving a control's license key.
Hmm, never too old to learn ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.