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.
- Simple Visual Basic Code
- Visual Basic Code For Simple Interest Examples
- Visual Basic Codes For Excel
- Visual Basic Codes List
- Visual Basic 2012 Code
Visual Basic Sample Codes Ebook. Visual Basic Sample Codes E-Book is written by our tutor, Dr.Liew. It comprises 258 pages of captivating contents and 48 fascinating Sample Codes.Perfect source of reference for your VB projects. Check it out by clicking the book picture. A very simple interest calculator Enter Principal, Interest Rate and Term (In years) after each year the interest earned is added to the principal and recalculated for every year. This is my very first C# program and I am still very new to this, if there is anyone who could help with shortening my decimal places to 2 I'd appreciate ir very much.
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 |
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:
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:
Simple Visual Basic Code
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:
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.
Visual Basic 6 is a third-generation event-driven programming language first released by Microsoft in 1991. In VB 6, there is no limit of what applications you could create, the sky is the limit. You can develop educational apps, financial apps, games, multimedia apps, animations, database applications and more.
Visual Basic Code For Simple Interest Examples
The team at vbtutor.net has created many sample codes, please browse them on the sidebar. You are welcome to use the sample codes as a reference for your assignments and projects. However, the usage of the sample codes for commercial purposes without prior consent from the webmaster is strictly prohibited.
Visual Basic Sample Codes E-Book is written by our webmaster, Dr.Liew. It comprises 258 pages of captivating contents and 48 fascinating Sample Codes.Perfect source of reference for your VB projects. Check it out.
About Us
The Tutor and webmaster of Vbtutor.net, Dr.Liew Voon Kiong , holds a Bachelor's Degree in Mathematics, a Master's Degree in Management and a Doctoral Degree in Business Administration. He obtained the DBA degree from the University of South Australia.
He has been involved in programming for more than years. He created the popular online Visual Basic Tutorial in 1996 and since then the web site has attracted millions of visitors .It is the top-ranked Visual Basic tutorial website in many search engines including Google. Besides that, he has also written a few Visual Basic related books. One of the books, Visual Basic 6 Made Easy was published by Creativespace.com, an Amazon.com publisher.
The Slot Machine created using VB6
Visual Basic Codes For Excel
Visual Basic Codes List
Visual Basic 2012 Code
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy