Windows Forms Controls for ...


Visual Basic Concepts 
Line Control for Visual Basic 6.0 Users 

The Line 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 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 2005, there is no equivalent for the Line control and lightweight controls are no longer supported. There are, however, ways to draw lines on a form both at design time and at run time.

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 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 us 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 2005, 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 2005.

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; it assumes that two Line controls were added at design time. The Visual Basic 2005 example demonstrates two 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()
    ' 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
' Visual Basic 2005
' Using Label controls.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Line1 As New System.Windows.Forms.Label
    Dim Line2 As New 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)
End Sub
Visual Basic
' Visual Basic 2005
' Using Graphics methods
Private Sub Form1Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.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)
End Sub

Drawing a Diagonal Line

The following code demonstrates drawing a diagonal line on a form at run time. In the Visual Basic 6.0 example, the Line control is used; it assumes that a Line 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 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 Basic
' Visual Basic 2005
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.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)
End Sub

Upgrade Notes

When a Visual Basic 6.0 application is upgraded to Visual Basic 2005, vertical or horizontal Line controls are replaced with the Windows Forms Label control, with the Text property set to an empty string, the BorderStyle property set to None, and the BackColor, Width, and Height properties set to match the original control.

Line controls that were not vertical or horizontal are not upgraded. You can replace the Line control using the Graphics functions built into the .NET Framework.

See Also

Tags :


Page view tracker