How to replace control instance in a form

conradoplg

New member
Joined
Sep 12, 2007
Messages
2
Programming Experience
3-5
Is there a way to replace a instance of a ActiveX control in a form with a new instance of the same control?

Thanks!
 
Because I'm having problems with the Flash ActiveX control. Whenever I load a ActionScript 3 flash file, and after that, I load a AS2 flash file, it behaves strangely. I think there must be a bug in the control that fails to cleanup the flash engine state so I would like to create a new instance of the control.

So... how can I do it? The control raises events, so I need the event handling to work too.

Thanks
 
First you would destroy the existing instance and remove it from the form. Then you would create a new instance, set its properties, assign it to the member variable that currently refers to the old instance, then add it to the form. To add or remove controls from a container you use its Controls collection.
 
VB.NET:
dim withevents a as new flashobject ' flashobject is an hypothetical class

sub ManInstance()
a=nothing 'if you want to delete the instance of the heap
a=new flashobject 'here you'll have a complete new instance ready to use
'me.controls.remove(a) 'use this if you want to remove the control of the 
container
dim b as new flashobject
me.controls.add(b) ' set the position and size using values of a.
end sub
 
Check the designer generated code to see how your object is currently set up.

For example in InitializeComponent method you will find something like this for the Adobe PDF Reader ActiveX:
VB.NET:
Me.AxAcroPDF1 = New AxAcroPDFLib.AxAcroPDF
If the component support interface ISupportInitialize you will also find this line:
VB.NET:
CType(Me.AxAcroPDF1, System.ComponentModel.ISupportInitialize).BeginInit()
Then there is the sections that configure each object, browse to find similar (notice the commented name is same as the variable name for the object):
VB.NET:
[COLOR="Green"]'
'AxAcroPDF1
'[/COLOR]
Me.AxAcroPDF1.Enabled = True
Me.AxAcroPDF1.Location = New System.Drawing.Point(631, 128)
Me.AxAcroPDF1.Name = "AxAcroPDF1"
Me.AxAcroPDF1.OcxState = CType(resources.GetObject("AxAcroPDF1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxAcroPDF1.Size = New System.Drawing.Size(59, 48)
Me.AxAcroPDF1.TabIndex = 13
When form itself is configured the controls are added:
VB.NET:
Me.Controls.Add(Me.AxAcroPDF1)
And finally (in this case) EndInitialize is signaled to the component:
VB.NET:
CType(Me.AxAcroPDF1, System.ComponentModel.ISupportInitialize).EndInit()
To replace this ActiveX component I would replicate this code (during this process you find there is a unresolved reference to "resources" when OcxState is set, look again into the generated code to see how this is declared, "frmDiv" it the form name here). Here is the replace method:
VB.NET:
Sub replaceAcroAX()
    Dim resources As System.ComponentModel.ComponentResourceManager = _
        New System.ComponentModel.ComponentResourceManager(GetType(frmDiv))
    [COLOR="Green"]'remove and dispose[/COLOR]
    Me.Controls.Remove(AxAcroPDF1)
    AxAcroPDF1.Dispose()
    [COLOR="Green"]'create new instance, the AxAcroPDF1 variable can be reused (*)[/COLOR]
    Me.AxAcroPDF1 = New AxAcroPDFLib.AxAcroPDF
    [COLOR="green"]'BeginInit[/COLOR]
    CType(Me.AxAcroPDF1, System.ComponentModel.ISupportInitialize).BeginInit()
    [COLOR="green"]'configure it[/COLOR]
    Me.AxAcroPDF1.Enabled = True
    Me.AxAcroPDF1.Location = New System.Drawing.Point(631, 128)
    Me.AxAcroPDF1.Name = "AxAcroPDF1"
    Me.AxAcroPDF1.OcxState = CType(resources.GetObject("AxAcroPDF1.OcxState"), System.Windows.Forms.AxHost.State)
    Me.AxAcroPDF1.Size = New System.Drawing.Size(59, 48)
    Me.AxAcroPDF1.TabIndex = 13
    [COLOR="green"]'add to form[/COLOR]
    Me.Controls.Add(Me.AxAcroPDF1)
    [COLOR="green"]'EndInit[/COLOR]
    CType(Me.AxAcroPDF1, System.ComponentModel.ISupportInitialize).EndInit()
End Sub
(*) the AxAcroPDF1 variable is also declared in the designer generated code, it is declared Friend WithEvents, so when you assign a new instance to it the event handlers still work.
 
Back
Top