VB6迁移至VB.NET:添加ShockwaveFlash控件报错咨询
The error you’re hitting boils down to a critical difference between VB6 and VB.NET’s Controls.Add method: VB6 let you pass a ProgID string to dynamically spawn a control, but VB.NET requires an actual Control object instead. That’s why your old VB6 syntax breaks here.
Here’s how to fix this properly:
Step 1: Confirm Required References
First, make sure your VB.NET project has the correct interop assemblies for the ShockwaveFlash control:
AxShockwaveFlashObjects(the Windows Forms wrapper for the control)ShockwaveFlashObjects(the underlying COM interop assembly)
You can add these via the Toolbox (right-click → Choose Items → COM Components → select "Shockwave Flash Object") or through your project’s References dialog.
Step 2: Rewrite the Control Creation Code
Replace your existing code with this VB.NET-compatible version:
Public WithEvents ShockwaveFlash1 As AxShockwaveFlashObjects.AxShockwaveFlash ' Put this in your form's initialization method (like Form_Load or the form's constructor): ShockwaveFlash1 = New AxShockwaveFlashObjects.AxShockwaveFlash() With ShockwaveFlash1 .Name = "ShockwaveFlash1" .Parent = Me ' Set essential display properties (adjust values as needed): .Location = New System.Drawing.Point(10, 10) .Size = New System.Drawing.Size(400, 300) .Visible = True End With Me.Controls.Add(ShockwaveFlash1)
Why This Works
- Instead of passing a ProgID string to
Controls.Add, we directly instantiate theAxShockwaveFlashcontrol withNew. - We explicitly set key properties (name, parent, position, size) before adding it to the form’s controls collection.
- The
WithEventsdeclaration still functions exactly like it did in VB6, so you can handle the control’s events without extra changes.
Quick Troubleshooting Note
If the control fails to render, try calling ShockwaveFlash1.CreateControl() right after adding it to the controls collection. In most cases, the Parent assignment and Controls.Add will handle this automatically, but it’s a handy fallback.
内容的提问来源于stack exchange,提问作者Himanshu

