Export (0) Print
Expand All
Expand Minimize

Shape Control for Visual Basic 6.0 Users 

The Shape control in Visual Basic 6.0 has no equivalent in Visual Basic 2005. However, you can use Graphics methods to achieve the same results.

Conceptual Differences

In Visual Basic 6.0, the Shape control provides an easy way to draw rectangles, circles, and other shapes on a form at design time. The Shape control is a "lightweight" control - meaning that it does not have a Windows handle, also known as an HWnd.

In Visual Basic 2005, there is no equivalent for the Shape control and lightweight controls are no longer supported. There are, however, ways to draw shapes on a form both at design time and at run time.

At design time, you can draw a square or rectangular shape on a form by adding a Label control and setting the Text property to an empty string, the BorderStyle property to FixedSingle, and the BackColor, Width, and Height to the desired color and dimensions.

At run time, you can draw rectangles, ellipses, and complex shapes in a form's Paint event handler by creating a new object from the Graphics class and calling its methods.

In Visual Basic 6.0, you can use a Shape control to draw a shape on top of a container control such as a PictureBox or Frame control by adding a Shape control to the container.

In Visual Basic 2005, you can achieve the same effect by calling the Graphics methods in the Paint event of the container control.

Code Changes for the Shape Control

The following code examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2005.

Code Changes for Drawing Rectangular Shapes

The following code demonstrates drawing a filled rectangle on a form at run time. In the Visual Basic 6.0 example, the Shape control is used; it assumes that a Line control was added at design time. The Visual Basic 2005 example demonstrates two different methods - using a Label control, and using Graphics methods.

NoteNote

In Visual Basic 6.0 the default unit of measurement was twips; in Visual Basic 2005 it is pixels.

' Visual Basic 6.0
Private Sub Form_Load()
    ' Show a solid red rectangle 200 twips from the top left.
    Shape1.Top = 200
    Shape1.Left = 200
    Shape1.FillColor = vbRed
    Shape1.FillColor= vbFSSolid
    Shape1.BorderColor = vbRed
End Sub
' Visual Basic 2005
' Using a Label control
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
    Dim Shape1 As New System.Windows.Forms.Label
    ' Show a solid red rectangle 14 pixels from the top left.
    Shape1.Location = New System.Drawing.Point(14, 14)
    Shape1.Size = New System.Drawing.Size(200, 100)
    Shape1.BorderStyle = BorderStyle.None
    Shape1.BackColor = System.Drawing.Color.Red
    Shape1.Text = ""
    Controls.Add(Shape1)
End Sub

' Visual Basic 2005
' Using Graphics methods
Private Sub Form2_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    ' Draw a solid blue rectangle below the red rectangle.
    e.Graphics.FillRectangle(Brushes.Blue, 14, 120, 200, 100)
End Sub

Code Changes for Drawing a Circle

The following code demonstrates drawing a circle on a form at run time. In the Visual Basic 6.0 example, the Shape control is used; it assumes that a Shape control was added at design time. The Visual Basic 2005 example uses Graphics methods.

NoteNote

In Visual Basic 6.0 the default unit of measurement was twips; in Visual Basic 2005 it is pixels.

' Visual Basic 6.0
Private Sub Form_Load()
    Draw a 1000 twip diameter red circle
    Shape1.Top = 0
    Shape1.Left = 0
    Shape1.Height = 1000
    Shape1.Width = 1000
    Shape1.Shape = vbShapeCircle
    Shape1.BorderColor = vbRed
End Sub
' Visual Basic 2005
Private Sub Form3_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    ' Draw a 70 pixel diameter red circle.
    e.Graphics.DrawEllipse(Pens.Red, 0, 0, 70, 70)
End Sub

Upgrade Notes

When a Visual Basic 6.0 application is upgraded to Visual Basic 2005, any Shape controls are replaced with Windows Forms Label controls, with the BorderStyle property set to FixedSingle, and the BackColor, Width, and Height properties set to match the original control.

NoteNote

The Label controls may be added to the form in a different order than the original Shape controls. For overlapping shapes, it may be necessary to call the BringToFront or SendToBack function to achieve the same visual layout.

Shape controls with the Shape property set to 2-Oval or 3-Circle cannot be upgraded and are replaced with a Label control as a placeholder. You can replace the Shape control using the graphics functions built into the .NET Framework. For more information, see Graphics for Visual Basic 6.0 Users.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft