Udostępnij za pośrednictwem


Line Control for Visual Basic 6.0 Users

The Line control in Visual Basic 6.0 has no equivalent in Visual Basic 2008. However, you can use graphics methods to achieve the same results, or you can use the optional Visual Basic Power Packs LineShape control.

Conceptual Differences

In Visual Basic 6.0, the Line control provides an easy way to draw lines on a form at design time. The Line control is a "lightweight control"- that is, it does not have a Windows handle, also called an HWnd.

In Visual Basic 2008, there is no equivalent for the Line control, and lightweight controls are no longer supported. However, there are ways to draw lines on a form both at design time and at run time.

Note

The optional Visual Basic Power Packs controls include LineShape, OvalShape, and RectangleShape controls that can be used to replace the Line and Shape controls. In addition to duplicating the behavior of the Visual Basic 6.0 Line and Shape controls, these controls add new capabilities. These include gradient fills, run time selection, and run time events.

You can download the Visual Basic Power Packs from the Microsoft Visual Basic 2005 Power Packs page on the MSDN Web site.

At design time, you can draw a vertical or horizontal line on a form by adding a Label control and setting the Text property to an empty string, the BorderStyle property to None, and the Width or Height property to 1.

At run time, you can draw vertical, horizontal, or diagonal lines in a form's Paint event handler by creating a new Graphics object and calling its methods.

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

In Visual Basic 2008, you can achieve the same effect by calling the DrawLine method in the Paint event of the container control.

Code Changes for the Line Control

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

Drawing Horizontal or Vertical Lines

The following code demonstrates how to draw horizontal and vertical lines on a form at run time. In the Visual Basic 6.0 example, the Line control is used; assume that two Line controls were added at design time. The Visual Basic 2008 example demonstrates two methods - using a Label control and using Graphics methods.

Note

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

' Visual Basic 6.0
Private Sub Form_Load()
    ' Draw a horizontal line 200 twips from the top of the form.
    Line1.X1 = 0
    Line1.X2 = Me.Width
    Line1.Y1 = 200
    Line1.Y2 = 200
    Line1.BorderColor = vbRed
    Line1.BorderWidth = 1
    ' Draw a vertical line 200 twips from the left of the form.
    Line1.Y1 = 0
    Line1.Y2 = Me.Height
    Line1.X1 = 200
    Line1.X2 = 200
    Line1.BorderColor = vbBlue
    Line1.BorderWidth = 1
' Visual Basic' Using Label controls.PrivateSub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) HandlesMyBase.Load
    Dim Line1 AsNew System.Windows.Forms.Label
    Dim Line2 AsNew System.Windows.Forms.Label
    ' Draw a horizontal line 14 pixels from the top of the form.
    Line1.Location = New System.Drawing.Point(0, 14)
    Line1.Size = New System.Drawing.Size(Me.Width, 1)
    Line1.BorderStyle = BorderStyle.None
    Line1.BackColor = System.Drawing.Color.Red
    Line1.Text = ""
    Controls.Add(Line1)
    ' Draw a vertical line 14 pixels from the left of the form.
    Line2.Location = New System.Drawing.Point(14, 0)
    Line2.Size = New System.Drawing.Size(1, Me.Height)
    Line2.BorderStyle = BorderStyle.None
    Line2.BackColor = System.Drawing.Color.Blue
    Line2.Text = ""
    Controls.Add(Line2)
EndSub
' Visual Basic    ' Using Graphics methods.PrivateSub Form1Paint(ByVal sender AsObject, _
ByVal e As System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
    ' Draw a horizontal line 28 pixels from the top of the form.
    e.Graphics.DrawLine(Pens.Red, 0, 28, Me.Width, 28)
    ' Draw a vertical line 28 pixels from the left of the form.
    e.Graphics.DrawLine(Pens.Blue, 28, 0, 28, Me.Height)
EndSub

Drawing a Diagonal Line

The following code demonstrates how to draw a diagonal line on a form at run time. In the Visual Basic 6.0 example, the Line control is used; assume that a Line control was added at design time. The Visual Basic 2008 example uses Graphics methods.

Note

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

' Visual Basic 6.0
Private Sub Form_Load()
    ' Draw a diagonal line from the top left to the lower right.
    Line1.X1 = 0
    Line1.X2 = Me.ScaleWidth
    Line1.Y1 = 0
    Line1.Y2 = Me.ScaleHeight
    Line1.BorderColor = vbBlack
    Line1.BorderWidth = 1
End Sub
' Visual BasicPrivateSub FormPaint(ByVal sender AsObject, _
ByVal e As System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
    ' Draw a diagonal line from the top left to the lower right.
    e.Graphics.DrawLine(Pens.Black, 0, 0, Me.ClientSize.Width, _
    Me.ClientSize.Height)
EndSub

Upgrade Notes

When a Visual Basic 6.0 application is upgraded, code that references the Line control is upgraded to use the LineShape control that is included in the optional Visual Basic Power Packs library. If a reference to the Line control is found during upgrade, a warning is issued and you will have to install the library and reference it from your upgraded project.

You can download the Visual Basic Power Packs from the Microsoft Visual Basic 2005 Power Packs page on the MSDN Web site.

See Also

Concepts

Graphics for Visual Basic 6.0 Users