Partager via


Visual Basic Concepts

Using the Shape Control

The shape control is used to create the following predefined shapes on forms, frames, or picture boxes: rectangle, square, oval, circle, rounded rectangle, or rounded square.

Figure 7.42   The shape control

You can set the shape style, color, fill style, border color, and border style of any of the shapes you draw on a form.

For simple uses, the shape control allows you to create a variety of shapes without writing any code. For more advanced functionality you need to use the Line and Circle methods.

For More Information   See "Using Graphics Methods" in "Working with Text and Graphics" for more information on drawing lines, rectangles, and filled-in boxes at run time using the Line method or for more information on drawing circles, ellipses, and arcs at run time using the Circle method.

Predefined Shapes

The Shape property of the shape control provides you with six predefined shapes. The following table lists all the predefined shapes, their values and equivalent Visual Basic constants:

Shape Style Constant
Rectangle 0 vbShapeRectangle
Square 1 vbShapeSquare
Oval 2 vbShapeOval
Circle 3 vbShapeCircle
Rounded Rectangle 4 vbShapeRoundedRectangle
Rounded Square 5 vbShapeRoundedSquare

Figure 7.43   Predefined shapes

Fill and Line Styles

You can use the FillStyle and BorderStyle properties to set the fill style and border style of any of the shapes you draw on a form.

The FillStyle property, like the Style property, provides you with a number of predefined fill style patterns. These include: Solid, Transparent, Horizontal Line, Vertical Line, Upward Diagonal, Downward Diagonal, Cross, and Diagonal Cross.

The BorderStyle property provides you with a number of predefined border styles. These include: Transparent, Solid, Dash, Dot, Dash-Dot, Dash-Dot-Dot, and Inside Solid.

For More Information   Both the FillStyle and BorderStyle properties provide you with constants that represent the styles listed above. See "FillStyle Property" and "BorderStyle Property" for more information.

Setting Color Attributes

The BackColor and FillColor properties allow you to add color to the shape and its border.

At design time, you can set the fill or border colors by choosing either property from the Properties window of the shape control and then selecting from the available palette or system colors.

To set colors at run time, use the Visual Basic color constants (vbGreen, for example) or the system color constants (vbWindowBackground, for example) or the RGB function to specify fill colors.

Note   When the FillStyle or BackStyle properties are set to 1 (Transparent), the FillColor and BackColor properties are ignored.

For More Information   See "Color Constants" for a list of Visual Basic color constants. See "RGB Function" for information on specifying RGB colors. Also, refer to "Working with Text and Graphics" for detailed information on creating graphics in Visual Basic.

Drawing Shapes on a Form

You can use the shape control to draw rectangles (regular or rounded corners), squares (regular or rounded corners), ovals, and circles on a form.

To draw a shape on a form

  1. In the toolbox, select the Shape control.

    When the pointer moves onto the form, it changes to a cross hair.

  2. Click and drag the cross hair to make the item the size you want.

  3. From the Properties window, select the Shape property.

  4. In the Settings box, select the style you want.

Shapes can be sized like any other controls, by selecting and dragging the control to the desired size, or by setting the Height and Width properties.