LESSON 7 - Text, Graphics and Multimedia

Manipulating text

Whenever you are entering data, creating files or databases, you are working with text strings. Text strings contain characters that can be copied, deleted, cut and reassembled but they also have important visual characteristics: size, color, weight, transparency, etc. In this lesson we will look at different ways of manipulating those text strings.

String functions

Here is a list of the basic functions that work with strings:

Formatting Numbers, Dates and Times

The Label control is still the easiest way of displaying the result of calculations. Whatever the answer is, just move it to Label5.Caption and it will appear on the form. Unfortunately, it does not always appear the way you want to see it. No problem if the result is a string but, what if it is a dollar amount or a date of some kind. That will require some formatting of the result before displaying it. We use the Format function:
Label5.Caption = Format(result, "formatting characters")


For example, given that:
        Dim result As Single

        result = 3456.7

        Label5.Caption = Format(result, "00000.00")          'displays: 03456.70

        Label5.Caption = Format(result, "#####.##")          'displays: 3456.7

        Label5.Caption = Format(result, "##,##0.00")          'displays: 3,456.70

        Label5.Caption = Format(result, "$##,##0.00")        'displays: $3,456.70

Here is a list of what the formatting characters mean:

represents a digit, with non-significant leading and trailing zeros 
represents a digit, without non-significant leading and trailing zeros 
decimal placeholder 
thousands separator 
$ + - ( ) space  literal character; displayed as typed 

When displaying dollar amounts, it is good practice to always use the 0 placeholder with the decimal so that 10 cents does not come out as $.1 or $0.1 Using the formatting string "$#0.00" ensures that the value follows standard rules and comes out as $0.10.

Dates and Times

When working with dates and times, you need to know that there is a function that will obtain the current date and time from the system clock. The function is: Now( ) and you can use it directly as in:
Label5.Caption = Now( )

The result is the current date and time formatted according to what you specified in the Windows Control Panel for your system. If you want to format the result, because you don't want to see the time, for example, there are formatting characters for date and time, as there are for numbers. The main characters are:

yy  year without the century - eg: 00 
yyyy  year with century - eg: 2000 
month number - eg: 12 
mmm  short name of month - eg: dec 
mmmm  long name of month - eg: december 
day of the month, without zero - eg: 8 
dd  day of the month, with zero - eg: 08 
dddd  name of the day of the week - eg: Monday 
hour, without zero - eg: 7 
hh  hour, with zero - eg: 07 
mm  minutes - eg: 45
ss  seconds - eg: 55

Thus, if Now( ) is July 8, 2000 ,
Label5.Caption = Format(Now( ), "dddd, yyyy mmmm dd")
returns: Saturday, 2000 July 08

Of course any other date can be formatted for display:
Label5.Caption = Format( DateOfBirth, "yyyy-mm-dd")

Named Formats

In addition to the formatting string, there are several named formats that can be used to determine the output format. These named formats are VB constants that you call when you need them:

General Number  Number with no thousands separator 
Currency  Thousands separator, two digits to the right of decimal 
Fixed  Displays at least one digit to the left and two digits to the right of decimal 
Standard  Thousands separator, at least one digit to the left and two digits to the right of decimal  
Percent  Multiplies by 100, add percent sign to the right 
General Date  Display determined by Control panel settings; displays date and time 
Long Date  Long date format specified for system 
Short Date  Short date format specified for system 
Long Time  Long time setting specified by system; includes hours, minutes, seconds 
Short Time  Shows hours and minutes 

Dim DateHired As Date
DateHired = "1995-10-25"
Label5.Caption = Format(DateHired, "Long Date")

returns: October 25, 1995

Manipulating blocks of text

The TextBox and the ComboBox controls contain several properties which will allow you to manipulate blocks of text, in addition to single characters.

If you have to input a large quantity of text in a TextBox, for example, you do not want to see it all in a single line. There are 2 properties that you set that will make the data easier to see:
  • MultiLine = True allows you to have several lines of input, all separated by <Enter>.

  • ScrollBars = 2 - Vertical will create scrollbars, useful to read text.

Then there are 3 properties to work with a block of selected text in the control:
  • SelStart an integer number identifying the start of selected text, the position of the first character in the block - starts at 0.

  • SelLength an integer number identifying the number of characters selected in the block.

  • SelText a string containing the selected text.

Note that this kind of manipulation is usually done with the mouse. However, you do not have to code for the mouse events. It is automatic - when you select text in a control, the appropriate events, MouseDown, MouseUp and MouseMove, are triggered by the control.

Useful objects: Screen and Clipboard

The Screen object represents the complete Windows environment. It allows access to all Forms and Controls. It has 2 important properties that we need:
  • ActiveForm returns the name of the Form currently active.

  • ActiveControl returns the name of the Control that currently has focus.

In the example that follows we will use these properties to avoid having to name the form and the control in code. This is a way of implementing re-usability of code, an important design principle - we can write code that can be re-used in many applications without having to be re-written.

The Clipboard object is the system clipboard that you know from all your other Windows applications. It is the object that temporarily stores text or graphics between applications. In the case of the Clipboard object, it has 3 important methods that we will use:
  • Clear empties the Clipboard.

  • SetText puts the selected text into the Clipboard.

  • GetText gets the contents of the Clipboard.


For the purposes of this example, we will use the Registration Form from Lesson 5.
We will add a Comment TextBox to the form. This textbox will be multiline, with a vertical scrollbar. Then, we will add items to the menu to allow us to edit the text entered in Comments. We want to be able to Cut, Copy, Paste and Delete blocks of text.

To change the Menu, we again call upon the Menu Editor. We add the new functions under the Edit item. To insert a separator bar, just put a single hyphen in the Caption and give it a Name, mnu_sep1, for example. The menu should look like this:

Then we code the menu events. Note that we use the Screen properties exclusively in this example. Even if we are working in a control which is called txt_comments, there is nothing in the code that refers specifically to that control. We can copy this whole section to any form in any application and it will work without a hitch.



When working with graphics (pictures, diagrams, images) the first thing to master in the environment is the coordinate system. That is the 2-dimensional grid that defines locations on the screen in terms of (x,y) coordinates. x is the horizontal position as measured from the left edge and y is the vertical position as measured from the top. (0,0) is the upper left-hand corner of the screen, form or other container.

By default, all VB measurements are in twips. A twip is 1/20 of a printer's point (of which there are 72 per inch). Thus, there are 1440 twips in an inch, 567 in a centimeter. The measurements refer to printed size; because there are great variations between monitors, sizes may vary.

You can change the units of measure yourself by setting the ScaleMode property for objects or controls.

ScaleMode  Meaning 
Twips - 1440 per inch. 
Points - 72 per inch. 
Pixels - number per inch depends on monitor. 
Characters - character = 1/6 inch high and 1/12 inch wide. 


Me.ScaleMode = 5      'sets scale to inches for this form
pic_EmployeePic.ScaleMode = 3      'sets scale to pixels for control

Adding Pictures

You can display pictures in 3 places in your application:
  • On a form
  • In a picture box
  • In an image control

The PictureBox control and the Image control are very similar in operation. However the PictureBox is more flexible in its methods and properties and is the one you should use most of the time. There is little use for the Image.
In all 3 cases you will display a picture contained in a graphics file (.BMP, .GIF, .JPEG, .WMF, etc). The name of the file containing the picture will be entered in the Picture property of the control or form.
In a form, the picture cannot be resized or moved, so it should be of the correct size before you use it. The picture will serve as background and other controls that you put on the form will be displayed over it.
The PictureBox's AutoSize property must be set to True, otherwise the control's size will not change to accomodate the picture, as in the example below.

In the above example the pictures were all added to the controls at design time.

You can also insert or remove a picture at run time. You use the LoadPicture function, as in:
pic_departmentlogo = LoadPicture("C:\Pictures\acctnglogo.bmp")

Removing the picture is done with the LoadPicture function without a file name:
pic_departmentlogo = LoadPicture ("" )

Drawing controls

There are 2 controls in the toolbox which allow you to draw directly on the form - the Line control and the Shape control.

Both are easy to use and fairly obvious. The main properties of each that have to be manipulated are: BorderColor for the color of the line or shape and BorderStyle to use a solid or dashed line.

In addition, the Shape control has: Shape for rectangle, circle, etc., FillColor and FillStyle to determine how the shape will be filled and BackStyle for transparent or opaque.



Multimedia refers to devices other than the screen or the printer to play sounds, watch videos or record music. This is done through the use of the Multimedia control. Don't look for it in the toolbox, it's not there. It is an additional control that you must load.

First, create anew form in Project Lesson7 and call it "multimed.frm". Then, in the menu, at Project --> Components, find the item "Microsoft Multimedia Control 6.0" and check the box next to it. Hit OK and that's it. The Multimedia control should now appear in your toolbox.

If you select the multimedia control and put it down on the form, you will have a button bar like all the ones you've seen on CD players, recorders, etc. In the DeviceType property you specify what type of device this control controls:

DeviceType  Device 
CDAudio  CD Audio player 
DAT  Digital audio tape player 
Overlay  Overlay 
Scanner  Scanner 
Vcr  Videotape player and recorder 
Videodisc  Videodisc player 
Other  Other devices not specified 

Example: a simple CD player

We create a new form in Lesson7 and call it multimed.frm. After adding the Multimedia control to the toolbox, we put a MM control on the form. Since we will only be using the one MM control, we'll leave its name as MMControl1. The only property we have to change at this time is the DeviceType, to tell it that we are using the CD player, so we write CDAudio in DeviceType. We add a few labels to complete the form and we get:

Now we have to write the code to operate the CD player.

Before we start to write the code there are a few things to know about the MM control. There is a Track property which contains the number of the current track. But its most important property is called the Command property and it can take on several values that, in fact, operate the device.

Command value Meaning
Open  Opens the device 
Close  Closes the device 
Eject  Ejects the CD 
Play  Plays the device 
Pause  Pauses the device 
Next  Goes to next track 
Prev  Goes to beginning of current track. If used within 3 seconds of most recent Prev, goes to beginning of previous track
Record  Initializes recording 
Save  Saves the open device file 
Seek   Step backward or forward a track 
Stop  Stops the device 
Step  Step forward through tracks 

Understand that both Track and Command are run-time properties because they are meaningless at design time. For example, to open the CD player:
MMControl1.Command = "Open"      'we assign the value "Open" to the Command property
To pause:
MMControl1.Command = "Pause"     'we assign the value "Pause" to the Command property

Now, as you have seen, the trick is to know with which events to associate the code that has to be written. The first one is fairly obvious: when we load the form, the Form_Load event, we will open the device. Now, one we haven't used before. When we unload the form, we will close the device. The reason is that, once launched, the device will keep on playing, even if the form is closed. So, just click on the Form_Unload event and write the code there. Finally, just to see that things are running smoothly, we will use the StatusUpdate event for the MM control to display the track being played every time the status of MMControl1 changes (change track, pause, play are all status changes).
As you will see, once the CD starts playing, you can use the buttons in the MM toolbar to control what it does.

You may notice that some of the buttons for the CD Player are not used during play. If you want you can hide these buttons from the control by using the (Custom) property. This will open an editor window that will allow you to customize the MMControl.

You might also want to visit this site for
Free Visual Basic 6 tutorials and sample source code examples .

If you haven't found the Visual Basic resource you're looking for,
use our Google Search box for more information.



Home | Tutorials | Contact | Sitemap | Add URL | Privacy policy