Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Drawing Thicker, Dotted, And Dashed Lines

Using the line control, you can select a line style with the BorderStyle property. Here are the possible values for the line control’s BorderStyle property:

  vbTransparent—0; transparent
  vbBSSolid—1 (the default); solid
  vbBSDash—2; dash
  vbBSDot—3; dot
  vbBSDashDot—4; dash-dot
  vbBSDashDotDot—5; dash-dot-dot
  vbBSInsideSolid—6; inside solid

To set a line’s width, you use the BorderWidth property (the default value is 1). It seems a little odd to call the line’s style BorderStyle and its width BorderWidth—after all, what is the line a border to? However, those properties are named that way to be consistent with the shape control.


TIP:  We might also note that the effect of setting the BorderStyle property depends on the setting of the BorderWidth property; if BorderWidth isn’t 1 and BorderStyle isn’t 0 or 6, Visual Basic sets BorderStyle to 1.

Drawing A Line Without The IDE Grid

When you draw lines in the Visual Basic Integrated Development Environment (IDE), those lines fall along the dotted grid you can see in forms. That grid can help in aligning controls and lines, but there are times when you want finer control.

To turn off the automatic alignment of controls to the grid as you draw them, follow these steps:

1.  Select the Tools|Options menu item.
2.  Click the General tab in the Options dialog box.
3.  Deselect the box marked Align Controls To Grid.
4.  Click on OK to close the Options dialog box.

That’s it. Now you’re free to draw controls as you want them and where you want them, without having your controls’ boundaries fall on a grid line.


TIP:  You can hide the grid by deselecting the Show Grid box in the Options dialog box, as well as reset its dimensions (the default size of each cell in the grid is 120x120 twips).

Changing A Line Control At Runtime

You can move Visual Basic controls at runtime—why not line controls? You can’t use the Move method to move a line control at runtime, but you can move or resize it by altering its X1, X2, Y1, and Y2 properties.

Let’s see an example. In this case, we’ve added four random line controls to a form in a control array, LineControl(0) to LineControl(3). When the user clicks a command button, Command1, we loop over all four lines and arrange them horizontally.

Here’s what the code looks like (the measurements are in the Visual Basic default, twips, or 1/1440s of an inch):

Private Sub Command1_Click()
    Dim intLoopIndex As Integer

    For intLoopIndex = 0 To 3
        LineControl(intLoopIndex).X1 = 1000
        LineControl(intLoopIndex).X2 = 3500
        LineControl(intLoopIndex).Y1 = 1000 + 100 * intLoopIndex
        LineControl(intLoopIndex).Y2 = LineControl(intLoopIndex).Y1
    Next intLoopIndex

End Sub

The result of this code appears in Figure 14.14. Now we’re moving lines around at runtime.


Figure 14.14  Changing line controls at runtime.

Using Form Methods To Draw Lines

We’ve seen how to draw lines with the line control—but you can use a form method, the Line method, to draw lines directly. The Line method can be an important part of graphic design (especially if you want to draw lines in a loop and don’t want to create a dozen or more line controls), and because we’re covering that topic in this chapter, we’ll look at the line control here.

Here’s how you use the Line method:

[Form.]Line [(x1, y1)]&45;(x2, y2)[, color]

Let’s see an example. Here, we’ll just draw four lines with the Line method when a form first loads. As with other graphic methods, to use this method in the Form_Load() handler, you must set the form’s AutoRedraw property to True.

Here’s the code we add to the Load event, making use of the Line method:

Private Sub Form_Load()
    Dim intLoopIndex As Integer

    For intLoopIndex = 0 To 3
        Line (1000, 1000 + 400 * intLoopIndex)-(3500, 1000 + 400 _
        * intLoopIndex)
    Next intLoopIndex
End Sub

The result of the preceding code appears in Figure 14.15—you can see the four lines we’ve drawn there.


Figure 14.15  Drawing lines with the Line method.


TIP:  The Line method is often a better choice than line controls if you have a large number of evenly spaced lines to draw, such as when you need to draw a grid or rules. Note, however, that if the user resizes the containing form, you might have to redraw those lines.

Using Form Methods To Draw Circles

We’ve seen that you can use the shape control to draw circles, but there is also a form method to do the same thing: the Circle method.

Here’s how you use the Circle method:

[Form.]Circle (x, y), radius[, color]

For example, here’s how we draw a few circles in a form using the Circle method (note that as with all graphics methods used in the Form_Load() event handler, you must set the form’s AutoRedraw property to True here):

Private Sub Form_Load()

    Dim intLoopIndex As Integer

    For intLoopIndex = 1 To 4
        Circle (2300, 500 + 400 * intLoopIndex), 400 * intLoopIndex
    Next intLoopIndex

End Sub

Running this code yields the result you see in Figure 14.16. Now we’re drawing circles using the form’s Circle method.


Figure 14.16  Drawing circles with the Circle method in a form.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.