codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » Visual Basic » Article

Beginning Resource Files in Visual Basic

Article by: Kenneth Ives (3/30/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: 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....
Viewed: 119454 times Rating (43 votes): 
 4.6 out of 5
 Rate this Article  Read Comments  Post Comments

Beginning Resource Files in Visual Basic



Introduction

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....

Getting Started

Now, let’s 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".


Figure1

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.

Adding some resources.

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 Files\Microsoft Visual Studio\ Common\ Graphics\Icons) 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

Get coding! 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 Sub Private 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 isn’t.

Conclusion

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 don’t think so. I prefer the second alternative.

The commands to remember are:

LoadResData - For loading AVI and WAV files. These are stored as binary arrays.
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 file is indexed.

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 kenaso@attbi.com




CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on ' Beginning Resource Files in Visual Basic'
RELATED ARTICLES
Generate License Keys (such as CD keys on Microsoft software)
by Brian Gillham
This class allows you to generate license keys, such as the CD keys on the case of most Microsoft software
Communicating with the Database (Using ADO)
by Chaudhary Pradeep K. Roy
Using ADO to get connected with the database.
Visual Basic Read and Modify the Registry
by Kenneth Ives
Perform the four basic functions to the Windows registry. Add, change, delete, and query. Allows you to to read registry values, and modify both keys and values.
Generate an Array of Unique Random Numbers
by Brian Gillham
This function will generate an ARRAY of TRULY random numbers.
Beginning Resource Files in Visual Basic
by Kenneth Ives
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....
Visual Basic Compact/Repair Access Database Utility
by Kenneth Ives
This utility will compact and repair the access database you select. It is actually more convenient to use this utility than do the same task with Access, because you don't have to save the database with a different name and then delete the old database when you are done: here, a temporary copy of the database is automatically created and deleted.
Creating a watermark in Excel with VBA
by Kenneth Ives
Adding a watermark to an Excel spreadsheet using VBA.
Change Cursor to Hour Glass
by Brian Gillham
A simple script to change the cursor to an hour glass.
Generate your own Random Numbers
by Thaha Hussain
This simple program generates random numbers using the basic mathematical methods.
Analog Clock
by Thaha Hussain
This program demonstrates Thaha Hussain's Clock Work Formula to paint an Analogue Clock.








Recent Forum Threads
• Game:Colonization based with HTML5 Canvas and JavaScript
• Pointwise.GridGen.v15.18
• Global.Mapper.v15.2.3.b060614
• Geometric_Glovius_Pro_v3.6.1
• VERO.SurfCAM.v2014
• Schlumberger.Petrel.V2013.2
• Petrel.V2013.2
• Altair.HyperWorks.v12
• VoluMill.v6.1


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2014