Add manually COM Object in a Form

Add  automatically a COM object in a form is very simple click on the object and put in the form using visual studio. But how to do the same thing manually? for example I want to add the COM object in a form only when an event occur. I tried to copy the same code that I found in the Form1.Designer.vb inside the Form1_Load event but I have errors and I do not know how to do.
Here the code in the form designer:
Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.AxCamImage1 = New AxATLCAMIMAGELib.AxCamImage
        CType(Me.AxCamImage1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.AxCamImage1.Enabled = True
        Me.AxCamImage1.Location = New System.Drawing.Point(45, 28)
        Me.AxCamImage1.Name = "AxCamImage1"
        Me.AxCamImage1.OcxState = CType(resources.GetObject("AxCamImage1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxCamImage1.Size = New System.Drawing.Size(192, 192)
        Me.AxCamImage1.TabIndex = 0
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.AxCamImage1, System.ComponentModel.ISupportInitialize).EndInit()
    End Sub  

How can i do the same thing in the event Load
I hope is clear my question thank You very much
Who is Participating?
sognoctConnect With a Mentor Commented:
exactly in the same way of the designer

but ovviously you must delete the object from the designer first ...

private AxCamImage1 as  AxATLCAMIMAGELib.AxCamImage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxCamImage1 = New AxATLCAMIMAGELib.AxCamImage
  AxCamImage1.Enabled = True
        AxCamImage1.Location = New System.Drawing.Point(45, 28)
        AxCamImage1.Name = "AxCamImage1"
        AxCamImage1.OcxState = CType(resources.GetObject("AxCamImage1.OcxState"), System.Windows.Forms.AxHost.State)
        AxCamImage1.Size = New System.Drawing.Size(192, 192)
        AxCamImage1.TabIndex = 0
end sub 

Open in new window

probably the OcxState must be setted manually with the values inside the resx file of the designer
mpbejoAuthor Commented:
Hi sognoct thank You for help,
this solution is perfect if You use .net framework but if You use .net compact framework You receive this message:
The targeted version of the .NET Compact Framework does not support latebinding
Do You have idea to solve this problem
Thank You
I always use ctype of objects, i prefer programming with

option strict on
option explicit on

Visual Studio itself resolves latebinding suggesting how to do the correct ctype
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.