Export (0) Print
Expand All
Expand Minimize

Object was upgraded to a panel, and cannot be coerced to a PictureBox

Visual Studio .NET 2003

When an application is upgraded from Visual Basic 6.0, if a PictureBox control is used as a container (that is, it contains child controls), it is upgraded to Panel control; otherwise, it is upgraded to a PictureBox control.

If a PictureBox control that has been upgraded to a Panel control is assigned to a variable of type PictureBox, the assignment will fail.

If a PictureBox control that was upgraded to a Panel control is passed as a parameter to a function, a run-time error will occur because the parameter is expecting a PictureBox control.

The following example shows code that would cause these errors:

' Visual Basic 6.0
Function PictureOrPanel()
   Dim p As PictureBox
   ' Not used as a container.
   Set p = Me.PictureBoxWithoutControls
   MyFunction Me.PictureBoxWithoutControls
   ' Used as a container.
   Set p = Me.PictureBoxWithControls
   MyFunction Me.PictureBoxWithControls
End Function

Function MyFunction( p As PictureBox )
   ' Do something here.
End Function

' After upgrade to Visual Basic .NET
Function PictureOrPanel() As Object
   Dim p As System.Windows.Forms.PictureBox
   ' Not used as a container.
   Set p = Me.PictureBoxWithoutControls
   MyFunction Me.PictureBoxWithoutControls
   ' Used as a container.
   ' UPGRADE_ISSUE: Form1.PictureBoxWithControls was upgraded to a Panel and cannot be coerced to a PictureBox.
   Set p = Me.PictureBoxWithControls
   ' UPGRADE_ISSUE: Form1.PictureBoxWithControls was upgraded to a Panel and cannot be coerced to a PictureBox.
   MyFunction Me.PictureBoxWithControls
End Function

Function MyFunction( p As PictureBox )
   ' Do something here.
End Function

What to do next

  • If the Panel control is being assigned to a variable of type PictureBox, create a new variable of type Panel and assign to that. If the Panel control is being passed as a parameter to a function, create a second version of the function with a parameter of type Panel.
    Function PictureOrPanel() As Object
       Dim p As System.Windows.Forms.PictureBox
       Dim q As System.Windows.Forms.Panel
       ' Not used as a container.
       Set p = Me.PictureBoxWithoutControls
       MyFunction Me.PictureBoxWithoutControls
       ' Used as a container.
       Set q = Me.PictureBoxWithControls
       MyFunction2 Me.PictureBoxWithControls
    End Function
    
    Function MyFunction( p As PictureBox )
       ' Do something here.
    End Function
    
    Function MyFunction2( p As Panel )
       ' Do something here.
    End Function
    

See Also

PictureBox Control Changes in Visual Basic .NET

Show:
© 2014 Microsoft