Microsoft & .NETVisual BasicBeginning Resource Files

Beginning Resource Files

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Have you ever wanted to use graphics, such as icons, bitmaps, cursors, and AVI files? How about sound or even message box text? This can be an enormous amount of overhead. These are all examples of more files to keep track of when you distribute an application, DLL, or OCX. Lets find out how resource files can help….

Now, lets get started. First thing we want to do is start Visual Basic 6.0. We will begin a new project. Now select the “Add-Ins” option on the menu bar, and then the “Add-In Manager…”. Scroll down the list and find “VB 6 Resource Editor”.


Figure 1.

Highlight and place a check mark in the “Loaded/Unloaded” box, if this is the only time you are going to access the editor. If you are going to access the editor on more than one occasion, then also place a check mark in the box “Load on Startup”. Click the “OK” button. Now every time you start Visual Basic, you will see a new item at the bottom of the drop down menu under “Projects” named “Add New Resource File”.

To begin with, we have to set up a place to save this new project. Select “File”, “Save Project”. Create a folder to save this project. At this time you may want to name your form or project.

Go to the menu bar and select “Project”, “Add New Resource File”. A window will open, prompting you for the name of a resource file. (See figure 2) Navigate to where you saved your new project and type on the filename line the name of your new resource file. (ex: MyFile.res) Select “Open”. You will prompted with a verification screen to create the file, select “Yes”.


Figure 2.

In the project window, usually in the upper right corner, there is now a new item named “Related Documents”. Click on the “+” and you will see a new entry named “MyFile.res”.

Double click this file (“MyFile.res”) and the resource editor window will open. If you drag your cursor slowly over the buttons, you will see a brief explanation of what each does. Before we go any further, you must understand that the resource file is just that, a resource (repository, storage location, or permanent holding area). Whatever is easier for you to remember.

First, we will add a couple of icons. Select the icon button, as shown in figure 3. Navigate to where you have some icons stored. (ex: C:Program FilesMicrosoft Visual Studio Common GraphicsIcons) Find one that represents STOP and another to represent START.


Figure 3.

One thing you have probably noticed is the names of these two icons have been changed. They are under the heading “Icons” and named 101 and 102. If you want to make sure as to what they look like, right mouse click one of them and select properties. These new names are known as their “Name ID”. This is important information to be used later. Close out of the resource editor window.

Add a command button to the form. Highlight the command button and go to the properties window. Find the “STYLE” property and change it to “1 Graphical”. Without this property being set, we cannot change the button color or place an icon on the button, both would be ignored. Set the height and width of this control to 1000. Figure 4 shows what you are looking at. Adjust accordingly.


Figure 4.

Open the code window for this form and paste the following code:

Private Sub Command1_Click()'Here is where we alternate the icons and the captions  If Command1.Caption = "Start" Then    Command1.Picture = LoadResPicture(101, vbResIcon)    Command1.Caption = "Stop"  Else    Command1.Picture = LoadResPicture(102, vbResIcon)    Command1.Caption = "Start"  End If  End SubPrivate Sub Form_Load()' paste the icon on the face of the command button' and set the caption.  Look in the online help for' additional information on the second parameter  Command1.Picture = LoadResPicture(102, vbResIcon)  Command1.Caption = "Start"  Show  End Sub

Press F5 and run it. Click on the button a few times.

I bet you feel that this is an awful lot of running around just to change an icon. Well, it isnt.

Consider this, you have written a wonderful application with multiple forms. Each of which have two or more command buttons, a picture control or two, image controls, etc. Let your imagination run with this. You decide that instead of a green button to represent START on all of your forms, you want to use the little car icon and you want to use a stop sign icon to represent STOP. You also found a better icon to represent exiting. You also want to change those message boxes to say something different.

How do you make these changes?

Do we open ALL the forms, highlight each command button and change the picture? Or do we change the icon in the resource file and recompile? Tough decision? I dont think so. I prefer the second alternative.

The commands to remember are:

LoadResData – For loading AVI and WAV files.

LoadResPicture For loading the icons, bitmaps, and cursors

LoadResString For loading those unique message box texts that are so meaningful.

How about a change of languages? After all, everything in the resource fileis indexed.

Another thing to remember is, you are not limited to one resource file per project. Read your online help for each of these commands. I have kept this as simple as possible. Only you and your imagination can limit your abilities.

Please write if you have any questions.

Kenneth Ives

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories