next up previous
Next: 18. Functions Up: VisualBasic Previous: 16. Calculating the Sum


17. PictureBoxes

Start an entirely new project by going to the File menu item, and selecting the item New Project. Now insert a CommandButton (from the Tool Box onto the blank form, and then insert a PictureBox as well. This is the icon on the Tool Box which looks like a cactus plant!

You should adjust the size of your PictureBox to make it quite big (by dragging the corners). You may want to increase the whole form size itself as well. Note that the name of this PictureBox is Picture1 by default, although, you can obviously change it to anything you want! Also, you can set the background colour of the PictureBox via the BackColor property in the PropertyBox.

A PictureBox is quite a powerful object. You can plot points, or draw lines or other objects on it.

Now place a CommandButton on the form. Double click on this CommandButton and insert the following code:

Private Sub Command1_Click()

Picture1.Scale (-100, 100) - (100,-100)
Picture1.FillStyle = 0
Picture1.FillColor = RGB(255,0,0)
Picture1.Circle (10, 20), 30, RGB(255,0,0)

End Sub

In the above, the Picture1.Scale line defines the size of the PictureBox. Specifically, it defines the top-left and bottom-right of the PictureBox. In the case above, we have defined a PictureBox whose x and y coordinates both run from -100 to +100.

The Picture1.FillStyle line just ensures that any object (such as a circle, etc.) are "filled-in" rather than just an outline. To draw the outline only, use Picture1.FillStyle = 1

The Picture1.FillColor defines the colour of the object (note the American spelling!!). In the case above, we have defined it via the RGB function - which stands for Red, Green, Blue. The argument of the RGB function run from 0 to 255. So, in the case above, the colour chosen is going to be Red. (Note RGB(0,255,255) would be yellow, i.e. green + blue.)

Finally, the Picture1.Circle line plots a circle (which is filled in due to the two Picture1.Fill lines) at coordinate position (10,20), with a radius of 30, and a colour of red.

There are many, many more PictureBox commands (you would have seen them listed when you typed in the Picture1. commands in the above example.

I'll list just some more which might be useful:

You can also print out text etc. to a PictureBox:

Private Sub Command1_Click()

Dim b As Long
b=4

Picture1.Scale (-100, 100) - (100,-100)

Picture1.CurrentX = -30
Picture1.CurrentY = 50
Picture1.Print "I am writing this to a PictureBox, b = " & Str(b)

End Sub

The above code places the text "I am writing this to a PictureBox, b = 4" on the PictureBox starting at coordinates (-30,50). Note the use of the ampersand character & in the above to join two strings together. This is exactly as in the Message Box case (see [*]).

The PictureBox is a very powerful way of producing graphics, and plotting objects such as circles, lines etc etc.


next up previous
Next: 18. Functions Up: VisualBasic Previous: 16. Calculating the Sum
Chris Allton 2006-10-27