1. Technology

How To Use Resources in Visual Basic

Loading Resources in VB 6


VB6 supports

  • A string table editor
    ("Edit String Tables...")
  • Custom cursors - "CUR" files
    ("Add Cursor...")
  • Custom icons - "ICO" files
    ("Add Icon...")
  • Custom bitmaps - "BMP" files
    ("Add Bitmap...")
  • Programmer defined resources
    ("Add Custom Resource...")

VB 6 provides a simple editor for strings but you have to have a file created in another tool for all of the other choices. For example, you could create a BMP file using the simple Windows Paint program.

Each resource in the resource file is identified to VB 6 by an Id and a name in the Resource Editor. To make a resource available to your program, you add them in the Resource Editor and then use the Id and the resource "Type" to point to them in your program. Let's add four icons to the resource file and use them in the program.

When you add a resource, the actual file itself is copied into your project. Visual Studio 6 provides a whole collection of icons in the folder ...

C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons

To go with tradition, we'll select the Greek philosopher Aristotle's four "elements" - Earth, Water, Air, and Fire - from the Elements subdirectory. When you add them, the Id is assigned by Visual Studio (101, 102, 103, and 104) automatically.

To use the icons in a program, we use a VB 6 "Load Resource" function. There are several of these functions to choose from:

  • LoadResPicture(index, format) for bitmaps, icons, and cursors

Use the VB predefined constants vbResBitmap for bitmaps, vbResIcon for icons, and vbResCursor for cursors for the "format" parameter. This function returns a picture that you can use directly. LoadResData (explained below) returns a string containing the actual bits in the file. We'll see how to use that after we demonstrate icons.

  • LoadResString(index) for strings
  • LoadResData(index, format) for anything up to 64K

As noted earlier, this function returns a string with the actual bits in the resource. These are the values that can be used for format parameter here:

    1 Cursor resource
    2 Bitmap resource
    3 Icon resource
    4 Menu resource
    5 Dialog box
    6 String resource
    7 Font directory resource
    8 Font resource
    9 Accelerator table
    10 User-defined resource
    12 Group cursor
    14 Group icon

Since we have four icons in our AboutVB.RES resource file, let's use LoadResPicture(index, format) to assign these to the Picture property of a CommandButton in VB 6.

I created an application with four OptionButton components labeled Earth, Water, Air and Fire and four Click events - one for each option. Then I added a CommandButton and changed the Style property to "1 – Graphical". This is necessary to be able to add a custom icon to the CommandButton. The code for each OptionButton (and the Form Load event -- to initialize it) looks like this (with the Id and Caption changed accordingly for the other OptionButton Click events):

 Private Sub Option1_Click()
    Command1.Picture = _
       LoadResPicture(101, vbResIcon)
    Command1.Caption = _
 End Sub 
To download this complete project Click Here
  1. About.com
  2. Technology
  3. Visual Basic

©2014 About.com. All rights reserved.