Sunday, October 12, 2014

The line and Shape controls



Graphics is a very important part of visual basic programming because an attractive interface will be appealing to the users. In the old BASIC, drawing and designing graphics are considered difficult jobs, as they have to be programmed line by line in a text-based environment. However, in Visual Basic 6, these jobs have been made easy. There are four basic controls in VB6 that you can use to draw graphics on your form: the line control, the shape control, the image box and the picture box

To draw a straight line, just click on the line control and then use your mouse to draw the line on the form. After drawing the line, you can then change its color, width and style using the BorderColor, BorderWidth and BorderStyle properties.Similarly, to draw a shape, just click on the shape control and draw the shape on the form. The default shape is a rectangle, with the default shape property set at 0. You can change the shape to square, oval, circle and rounded rectangle by changing the shape property’s value to 1, 2, 3 , 4, and 5 respectively. In addition, you can change its background color using the BackColor property, its border style using the BorderStyle property, its border color using the BorderColor pproperty as well its border width using the BorderWidth property.


The program in this example allows the user to change the shape by selecting a particular shape from a list of options from a list box, as well as changing its color through a common dialog box. 


The objects to be inserted in the form are a list box, a command button, a shape control and a common dialog box. The common dialog box can be inserted by clicking on ‘project’ on the menu and then select the Microsoft Common Dialog Control 6.0 by clicking the check box. After that, the Microsoft Common Dialog Control 6.0 will appear in the toolbox; and you can drag it into the form. The list of items can be added to the list box through the AddItem method. The procedure for the common dialog box to present the standard colors is as follows:


CommonDialog1.Flags = &H1&


CommonDialog1.ShowColor


Shape1.BackColor = CommonDialog1.Color


The last line will change the background color of the shape by clicking on a particular color on the common dialog box as shown in the Figure 18.1 below:




The Interface. 



Figure 18.1


The color dialog box




The Code






Private Sub Form_Load()


List1.AddItem "Rectangle"


List1.AddItem "Square"


List1.AddItem "Oval"


List1.AddItem "Circle"


List1.AddItem "Rounded Rectangle"


List1.AddItem "Rounded Square"


End Sub






Private Sub List1_Click()


Select Case List1.ListIndex


Case 0


Shape1.Shape = 0


Case 1


Shape1.Shape = 1


Case 2


Shape1.Shape = 2


Case 3


Shape1.Shape = 3


Case 4


Shape1.Shape = 4


Case 5


Shape1.Shape = 5


End Select


End Sub






Private Sub Command1_Click()


CommonDialog1.Flags = &H1&


CommonDialog1.ShowColor


Shape1.BackColor = CommonDialog1.Color


End Sub 


No comments:

Post a Comment