This is a collection of highly requested Visual Basic code that I put together back in 1998 and 1999. Almost all of these samples will work on fine on Visual Basic 5.0 and for Microsoft Excel and Microsoft Access Programming (when done with VBA), however, I've only made sure that they work in VB6. Back in the day I used to answer a lot of questions and I frequented a lot of VB message boards. I noticed that the same questions kept getting asked over and over again, so I started saving code that seemed to be important. 95% of the code you'll find on this page was written by me, the other samples were either edited or submitted. This guide does not claim to have the best answers, just answers that will get the job done. I hope you find it helpful.
If you aren't able to find what you need here, you can check out the main programming section for additional VB 6.0 code and tutorials.
Code Title | Code Description |
---|---|
Add a Menu to Another Program | An example of how to add a menu to another program. |
Beep like QBasic's Sound | This shows you how to set the frequency & duration of a beep sound (kinda like qbasic's sound function). |
Center a Form | Check this code out if you want to know how to put your forms in the center of the screen. |
Convert VB3 Forms to VB6 Forms | This will explain how to convert 16 bit Visual Basic forms to 32 bit Visual Basic forms. |
Count Lines | This code shows you how to count the number of lines that are in a textbox. |
Count the Times Loaded | This code shows you how to count the number of times the user has used your program. |
Count Words | This code shows you how to count the number of words that are in a textbox. |
Cut, Copy, Paste, & Undo | Ever wanted to use the cut, copy, paste, & undo commands in your program? Check this out. |
Disable/Enable Ctrl+Alt+Del | This shows you how to use the Windows API to disable/enable ctrl+alt+del. This only works for Windows 95 and Windows 98. |
Encrypt/Decrypt Text | This code will let you easily encrypt/decrypt strings of text. |
Flip Picture | This is an example that shows you a fast way to flip a picture in a picturebox horizontally or vertically. |
Font Lister | This code shows you one way on how you can add a list of all the fonts on your computer to a listbox. |
Form Mover | This code will show you how to move a form with a label. |
Get HTML Color | This example shows you how to get the HTML value of a color. |
Get Your Computer's Name | This is some code that shows you how to get your computer's name (yep, it has a name). |
Hex-Editor Related Code | VB probably isn't the best language to make a hex editor with, but here are some hex type examples anyway. |
HTML Color Fade | This is a short example on how to fade one color into another with HTML tags. |
HTML Color Fade Preview | This is an example on how to preview color faded text in a picturebox. |
HTML Waving Text | This is an example on how to generate the HTML code for waving text. |
Icons in System Tray | For some reason this is requested a lot, so I thought I'd put up an example on how to do it. This code only works in Windows 95 and Windows 98. |
Kill Duplicate Items in a Listbox | Ever wanta get rid of all the duplicates that are in a listbox? This code shows you how. |
Listbox Open | An example of how to open files to a listbox. |
Listbox Save | An example of how to save the list inside of a listbox. |
Macro Font Draw | This is a code example that shows you how to create an ASCII Art Font option for an ASCII Art Shop program. |
Open Default Browser | This example shows you how to open up to a webpage using your default browser. |
PictureBox Fade | This code shows you how to fade a picturebox from one color to another. |
Play Midi | Shows you one way you can play a midi (*.mid) file. |
Play Wav | Shows you one way you can play a wav file. |
Random Numbers | This code shows you how to generate random numbers within a given range. |
Replacing Text in a String | This code shows you one way to replace text within a string. |
Resize it (Form Stretch) | This code lets you easily resize the controls on your form when you resize it. It creates a cool stretch effect. |
Scramble | Shows you how to scramble words. This code is good for something like a Scrambler game. |
Screen Saver Creation | This explains how to create screen savers in Visual Basic. |
Score Keeper | This is a KeepScore function. It's for keeping score in games like Scrambler where points are usually kept in a listbox with people's names. |
Select All the List Items | This shows you the fastest way to select all the items in a listbox (the Windows API way). |
Set Windows Wallpaper | This code shows you how to set the wall paper for Windows. |
Spell Checker | This is a code example that shows you how to make a spell checker for your VB programs! It works by calling up MSWord's spell checker to spell check your documents. |
StayOnTop | This code will make your forms so they stay on top of all of the other windows on the screen. |
Tile a Pic in the Background | This shows you how to tile a picture in the background of a form. |
Timeout/Pause | This code will allow your program to use timeout statements, giving time for certain events to happen. |
VB3 Interface for VB5/6 | Just when you get used to VB3's interface, they change things up on you in VB5 and VB6. This explains how to set VB5/6's interface so it acts just like VB3. |
Total | 41 Code Examples |
Visual Basic 6.0. Form Events - VB code example. By Sergey Skudaev. Hover mouse over a thumbnail to see a large image. In the lesson 1 Start VB you have learned how to use Visual Studio to create a simple VB project. In the lesson 2 you will learn form events and properties. Following is an example of what our two programs will accomplish. Figure 1: The Sender and Receiver applications running concurrently. The Visual Basic Sender Application. This application will be used to send pieces of information via UDP. Add the following Imports statements to your UDP Sender application: Imports System.Net.Sockets Imports. This is a collection of highly requested Visual Basic code that I put together back in 1998 and 1999. Almost all of these samples will work on fine on Visual Basic 5.0 and for Microsoft Excel and Microsoft Access Programming (when done with VBA), however, I've only made sure that they work in VB6.
This code shows you how to add a menu to another program.The only thing is that nothing will happen when you click on the items. To make something happen when you click on an item you have to subclass the menu (I'd help with that but I don't have any subclassing controls, or at least not right now). Put this in your *.bas file:
Then put something like this in a button:
This shows you how to set the frequency & duration of a beep sound. Put this in your *.bas file1:53 AM 8/19/2007:
Put something like this in a button:
Or, if you want to do something a little more fun, try this:
Here's how to center a form so it will appear in the middle of the screen, put this in a button:
Here's how to convert old 16 bit VB froms into 32 bit VB forms: In a 16 bit version of VB (like VB3), select 'File' from the menu, then click on 'Save File As...' A dialog box will pop up. Look for a checkbox on that dialog box that says 'Save as Text.' Make sure that is checked. Once you do that, save the file. You should now be able to use that form in 32 bit versions of VB (like VB6).
This code will count how many lines of text there are in a string by counting the number of times the'carriage return' character shows up and adding one to that. This code will not count lines of text that wrap in a textbox. Put this code in a button:
This code shows you how to count the number of times your program is used. Place this function in your *.bas file:
Example on how to use this function, put something like this in the form load event:
Visual Basic Source Code Examples
This code lets you count the number of words that are in a textbox (note this is very different from the len function which counts the number of characters in a string). Put these two functions in your *.bas file:
Then put something like this in a button:
Ever wanted to use the cut, copy, paste, & undo commands in your program? Now you can, check this out:
Cut: (put this in a button)
Copy: (put this in a button)
Paste: (put this in a button)
Undo: (put this in your bas)
Put this in a button:
The following code only works in Windows 95 and Windows 98. Put this in your *.bas file:
Then put this in a button to disable c+a+d:
Then put this in a button to enable c+a+d:
Below is an example of a substitution cipher. Substitution ciphers can be broken by a cryptanalyst who knows what they're doing, so this method of encryption shouldn't beused if you want your data to be 99% secure. However, substitution ciphers will keep common users from being able to seeyour data. For high levels of security look into public key encryption andAES.To continue on, copy and paste the function below into your *.bas file:
How to use this function:
To encrypt a word just put something like this in a button:
And then to unencrypt the word just call the function again!
This code demonstrates a fast way to flip a picture in a picturebox horizontally or vertically. Put this in your *.bas file:
Then put something like this in a button:
This isn't the fastest way to add all the fonts on your computer to a listbox, but it's the easiest. Put something like this in a button:
Copy this code into your *.bas file:
How to Use:
Put this code in the label's MouseDown event:
Put this code in the label's MouseMove event:
This is an example that shows you how to get the html color value (ie, the hex value) of a color. You will need to add theMicrosoft Common Dialog control to your project for this to work. Put this code in a button:
Did you ever want to know what your computer's name was? Not me, but anyway, if you do want to know try this... Put this code in your *.bas file:
Then put something like this in a button:
Ok, this example code will show you how to open up an exe, copy it, replace strings in the copy with strings of your choice, and then make a new program with the new strings in it.
For this to work you need to have two textboxes on a form (named: txtProgInput and txtProgOutput) and two lists (named lstOldStrings and lstNewStrings). txtProgInput contains the name of program you're editing (like:'C:somefile.exe') and txtProgOutput contains the name of the program you're making from this program. lstOldStrings contains a list of strings your replacing and lstNewStrings contains the list of what you're replacing them with. IMPORTANT NOTE: The length of the string you're replacing must be the same as the length of the string you're replacing it with, otherwise you'll get an error. Put this code in a button:
HTML color values are stored in a hexadecimal format. Each HTML color value is divided into 3 parts: the color's amount of red, green, & blue - #FF0000. The amount of red a color can have is in the range of 0 to 255 (same goes for green & blue). So if you have 255 for red and20 for blue and green, your color will be sort of redish looking.
Anyway, the following is an example on how to fade one color into another. Put this function in your *.bas file:
Then put something like this in a button:
This example shows you how to create a fade preview of color faded text inside of a picturebox. You will need to add a picturebox control named 'Picture1' to your form for this to work.Put this code in a button:
Just like the title says. Put this code in a button:
This code did not work when tested in Windows XP. It was written for Windows 95/98.
This shows you how to put an icon in the system tray. It uses an icon stored in Picture1.picture as the icon.Put this in your *.bas file:
Then put something like this in a button to add the icon to the tray:
Put something like this in a button to remove the icon:
This code loops though a list (List1) checking each item with every other item, then when it finds a duplicate it removes it. Put this code in a button:
Here's another way to do it, this way is somewhat faster and uses some Windows API functions (sentin by Sopon). First put this in your *.bas file:
Then make this a function in your *.bas file:
Then put something like this in a button:
This code shows you how to open a file to a listbox. Make sure you have a CommonDialog control added to your form. Put something like this in a button:
This code shows you how to save the contents of a listbox. Make sure you have a CommonDialog control added to your form. Put something like this in a button:
AOL Macro Fonts allowed you to type in large ASCII art text, like you see below.
|¯¯¯-¯)::)¯¯,¯_':|¯¯¯¯¯¯¯||:|__|¯|°:/__/'__:|¯¯|__|¯¯|
::|__|¯::|__:|/|__|':¯¯|__|¯¯
This code example shows you how to create a macro font feature for a Macro Shop / ASCII Art Shop program. It makes it so you can select a (*.pmf) file (a sample pmf file can be downloaded here), load it into a 2D array, and thenhave the output display when the user types text in an input textbox. You will need the followingfor this example to work:
txtInput - An input textbox named 'txtInput'. This is where the user types their input.
txtOutput - An output textbox named 'txtOutput'. This is where the output is displayed. This textboxshould have it's multiline property set to true and it's font type set to 'Arial' and point size set to 10.
CommonDialog1 - A common dialog control should be added to the project.
Put something like this in your *.bas file:
Put something like this in the load a font button:
And then put something like this in the Change event of txtInput:
This shows you how to open your default browser. Put this function in your *.bas file:
Then put something like this in a button:
This code will fade one color into another color in a picturebox. It's a nice tid-bit to keep in mind if you want to make your own title bar for a form. Put this in a button:
Copy this function into your *.bas file:
The following code can go in buttons to play/stop/pause/etc:
This code will be the same as described in playing a midi file with one key difference. When opening afile, use 'waveaudio' as the type. Example:
This code lets you generate a random integer within a given range. Copy this function and paste it in your *.bas file:
Example on how to use:
'x' will be equal to a random integer in the range: [10, 20] (ie, between 10 and 20, including 10 and 20)
This small example on how to use the 'Replace' function in Visual Basic 6.0. Originally this code used the Mid and Instrstring functions, but since VB 6.0 you can simply use 'Replace'.
How to use (put in button):
This code lets you resize your forms with a neat stretch effect. Copy and paste the code as directed by the comments:
Here are a neat series of functions that will allow you to scramble each word in a sentence. This code would word well for a scrambler program.Put these functions your *.bas file:
Example on how to use these functions to scramble the words in a sentence:
Here's what you do to make a screen saver: Start a new project. Set the windowstate property of the form to '2 - Maximized', and the border style of the form to zero. In the 'KeyDown' event of the form put the 'End' statement. Then in the mouse move event of the form put something like this:
Now select 'Make some_project_name.exe' in the file menu. When it asks you what you want to name the exe, add a '*.scr' extension to the end of the name (example: Blah.src). Then select the directory 'c:windowssystem' as the place to make this program. And there you go, you've made a screensaver which you can use on your desktop (note: you'll probably want to jazz up the form a little bit first).
Additional Info: (Provided by Mike Clem)
Try adding 'SCRNSAVE: ' in front of the name of your screen saver when compiling if you cannot get Windows to recognize that your creation is a screen saver. Example: 'SCRNSAVE: Blah.SCR'
Problem of Windows not recognizing the Screen Saver occured in WFW 3.11 using VB 3.0.
This is a KeepScore function. It's for keeping score is games like Scramblerwhere points are usually kept in a listbox with people's names.Code written by deep arctic.
Here is an example on how you can try this function out:
This is an example that shows you how to select all of the list items in a listbox. This should work for any listbox window. Just make sure the listbox's multiselect property is setto '1 - Simple' in the properties window. Put something like this in your *.bas file:
Then put something like this in a button:
This code shows you how to set the wall paper for windows, put something like this in you *.bas file:
Then put something like this in a button or in the form_load:
Ever wanted a spell checker feature in one of your programs? Check this out, it calls up the MSWord's spell checkerso you are able to spell check your documents! You need MSWord95 or better for this code to work. This example spell checks the text in a textbox named 'Text1'.Put this in a button:
This makes a form stay on top of all other applications running in Windows. Put this code in your *.bas file:
Example on how to use (put this in the form's load event):
Example on how to remove a form from being the top most window:
This example shows you how to tile a picture in the background of a form. Put something like this in the form's load event:
This sub lets up create a pause in your programming code for whatever amount of seconds youtell it to pause for: (copy & paste is sub in your *.bas file)
Example on how to use (pause for one second):
Here's another way to do it, this way uses the Windows API (sentin by Sopon). First put this in your *.bas file:
Then make this a sub in your bas:
Visual Basic 6 Code Examples With Activex
Example on how to use (pause for one second):
Here's how to give VB5/6 the VB3 interface: Select 'Tools'>'Options' from the menu. Then when the 'Options' form pops up select the 'Advanced' tab. Make sure the 'SDI Development Environment' checkbox is checked, and click 'OK'. When you restart VB5/6, you should have the same kind of interface that you had with VB3.
Title |
---|
Tax Declaration Calci1.09 in Visual Basic |
'dancing mouse' -- randomly move the mouse pointer in visual basic |
'finicky': paint program in visual basic |
'lightweight' print preview dll in visual basic |
'smart' select case for decision-making in visual basic |
'xtreme player' -- mp3 player with skins in visual basic |
.ini file reader and writer in visual basic |
.net config file wrapper in visual basic |
.net library to help manipulate/process text files in visual basic |
.net scroll bar control in visual basic |
.net system information class demo in visual basic |
.net textdata provider demo in visual basic |
1-click customization of controls in visual basic |
1-click customization of controls v2 in visual basic |
10 seconds (simple game) in visual basic |
10 square tic-tac-toe in visual basic |
12 Image Transitions in Visual Basic |
2 Player Chess in Visual Basic |
216 color pallette in visual basic |
24-bit picture class w/ resize, crop, transregion and exposed bits in visual basic |
24-bit picture class w/ resize, crop, transregion and exposed bits part 2 in visual basic |
2d tileset creator (vb.net) |
3 Phase Short and Medium Transmission Lines in Visual Basic |
3-Layered Scrolling Background in Visual Basic |
30 classes for 40 instruments in visual basic |
3D Digital Reality Engine V1.00, PURE VB in Visual Basic |
3D Font (version 2) in Visual Basic |
3D Font in Visual Basic |
3d grid waves animation in visual basic |
3d label control in visual basic |
3d label control part 2 in visual basic |
3D Label in Visual Basic |
3d line control with 13 styles in visual basic |
3D Matrix Maths II (no openGL or DirectX required) in Visual Basic |
3d maze maker in visual basic |
3d mesh collision detection in visual basic |
3d mesh editor in visual basic |
3d sample applications in visual basic |
3d soccer field in visual basic |
3D Spinning Cube Demonstration in Visual Basic |
3d terrain without using any apis or external components in visual basic |
3D text in Visual Basic |
3D Tube Animation in Visual Basic |
3d world with a snowman in visual basic |
3dstudio files (3ds) viewer in visual basic |
52 card deck maker in visual basic |
6/49 lotto combination generator in visual basic |
6/49 Lotto Combination Generator Program in Visual Basic |
6/49 lotto game with dynamic html simulation in visual basic |
8Queens Game in Visual Basic |
a .net class to calculate circles in visual basic |
a .net drag-n-drop puzzle in visual basic |
a .net-based directx sound capture and recording application in visual basic |
a 3d demo w/o directx or opengl in visual basic |
A 3D Label control in Visual Basic |
A 8085 Simulator in Visual Basic |
a barebones 24bit wave audio reader in visual basic |
a base64 / mime encoding and decoding class. in visual basic |
a basic c# console application sample in visual basic |
a basic calculator application in visual basic |
a basic dao connection method in visual basic |
a basic database interaction sample in visual basic |
a basic drag and drop sample in visual basic |
a basic employee database system in visual basic |
a basic pythagoras implementation in vb6 |
a beautiful card game. in visual basic |
a better app.path in visual basic |
a better chr() function in visual basic |
a binary search algorithm in visual basic |
a c# overloaded method to calculate an md5 hash in visual basic |
a c# rss feed retriever sample in visual basic |
a calendar example similar to windows date and time properties in visual basic |
a class for searching datagrids in visual basic |
a class for system, logical drivers, memory, display drivers, etc. in visual basic |
a class to preserve message boundaries using tcp and the vb winsock control |
a class to resize the controls on the form as the form resizes in visual basic |
A Code Library for Programmers in Visual Basic |
a collection of bitmap routines in visual basic |
A College Library Management Software in Visual Basic |
a colorpicker that uses websafe colors. in visual basic |
A Complete Calculator in Visual Basic |
a complete mmorpg engine - vbgore v1.0.0 |
A complete mmorpg engine - vbGORE v1.0.0 in Visual Basic |
A Complete Simple Banking Application in Visual Basic |
a cool calculator in visual basic |
a custom color picker for easy integration in visual basic |
a custom command line interpreter in visual basic |
a daily alarm class in visual basic |
a database toolkit for ms access in visual basic |
a db-less personal expense management system in visual basic |
a dbase iv module with an export function in visual basic |
a demo on how to build a coolbar in visual basic |
a demonstration of the circle draw method in vb6 |
A Fast Fill FlexGrid by Recordset ADO in Visual Basic |
a fast method for appending strings in visual basic |
a fast syntax highlighting control in visual basic |
a feature rich custom combobox in visual basic |
a firebird 2.0 data access layer with vb.net |
a fully featured winxp-styled lcd alarm clock in visual basic |
a function that can calculate isbn checksums. in visual basic |