The Visual Basic Programming Environment
The Programming Environment
The Visual Basic Programming environment is set-up to help you create applications quickly, with a minimum of programming.
The menu bar provides access to most of the commands that control the programming environment.
Below the menu bar is a toolbar containing a collection of icons that serve as shortcuts to many of the common commands you will use when creating applications.
Familiar Setup
Visual Basic is modeled after other Windows applications, so if you have used Word or Excel, you should feel right at home.
Other elements of the Visual Basic programming environment include the Project Container window, the Form window, the Project window, the Immediate window, the Properties window, and the Form Layout window.
Moving Windows Around
The position, shape and size of your programming elements depends on how you have set them up.
All of the elements in Visual Basic are customizable to help you create the most efficient programming environment for you.
Lets take a look at the default setup...
Moving Tools Around
With seven tools, the Visual Basic screen can get quite crowded. To give yourself more space you can move things around.
To move a window all you need do is drag it around the screen.
If you align it to the edge of another window it will "dock" itself to that window.
Dockable windows have the advantage of always being visible.
Moving Tools Around 2
If you want to see more of a window that is docked, simply drag its borders.
You can also undock the tools if you wish your windows to overlap and take up less of the programming space.
Try moving your tools around the screen and see how they behave as you move closer to the edges of another window.
Tools
The User Interface Form
In VB a form is a window you customize to create the user interface.
A form can contain menus, buttons, list boxes, scroll bars, and any other items you typically see in a Windows program
When you start a new program a form is placed on the screen in the default size.
You can adjust that size to fit your program by dragging on the sides.
The Toolbox
The Toolbox is by default located on the left of the programming environment and contains the tools, or controls that you will add to your applications (buttons, images, boxes, etc.).
The Toolbox also contains objects that are not visible to the user but which perform functions behind the scenes (I.e. time and database functions).
The Properties Window
The Properties window lets you change the characteristics, or property settings, of the user interface elements on a form.
For example, we can change the caption on a button or a window.
The Properties window contains a drop down box listing all the user interface elements (objects) contained in the form.
More about Properties
In VB all user interface elements in your applications, including the form itself, will have properties associated with it.
Properties can be a difficult concept to grasp. However, you can think to them as the characteristics or features that an object has. Some of those characteristics are defined when the object is first built, others are defined later as the object is used.
The Project Window
A VB program is made up of several files that are assembled together, or compiled, into a program.
To help you switch back and forth between these components the makers of VB created the Project window.
The Project window includes all the files used in the programming process.
Project Window Continued
The project file maintains that list of supporting files in a file with the extension .vbp.
You may have more than one project open at one time and switch back and forth between them.
The Project window displays the contents of a project in a tree structure, similar to the windows explorer structure.
A Program Example
Luck Daves Lotto Number Picker
In this program example we will build a program which picks lottery numbers.
This example will give us practice in placing controls, changing properties, and writing simple code.
Lets get started .
Create the Interface
Step one is to create the controls that will be associated with our program.
What we need are two command buttons:
Pick Numbers
End
A label with the words "Luck Daves Lotto Number Picker"
And six label boxes
Set the Properties
Next we want to set the properties so that our boxes look nice and have the right captions.
Next lets change the font in our number display boxes to an appropriate size for the box--something like TNR 24pt. After that change, delete the captions--dont need them here.
Colors
We can change the color of our text to something more attractive by changing the ForeColor property in the Properties window.
We can also play with some of the other color properties to get a nice looking application.
Write the Code
Double click on the Form, not any of the objects, to open the code box.
Use the drop down box to select the Command1.
By default VB displays all the event procedures for a form in one window, so that you can easily switch back and forth between them.
Write the Code 2
If you wish to see only one procedure at a time, click on the tiny Procedure View button in the bottom left corner.
Next add the code to generate your lotto numbers.
Label1.Caption = Int(Rnd * 36 + 1) ' pick numbers
Repeat the code for each label box
Explanation of the Command1 Procedure
The Command1 procedure is executed when the user clicks the "Pick Numbers" button.
The code we inserted create random numbers between 0 and 1 (a number with a decimal point) and the Int function multiples the numbers by 36 and rounds them to the nearest decimal point.
Finally, we add 1 to prevent zeros.
Command2 Procedure
For Command2, the End button, all we need to do is to insert a statement that causes the program to end.
The code we need to add is just the word "End"
Form Procedure
The last bit of code we need to write concerns generating true random numbers.
Our program will work without this statement, but you will notice that the first time we run it the numbers are always the same.
What we will add is a Randomize command in the Form Procedure. This creates random numbers based on the computers clock.
Thats it--Your lotto number picker is ready!