Learn Visual Basic Version 6: Part 2: The Two Button Form in Detail
Learn Visual Basic Version 6: Part 2: The Two Button Form in Detail
1. Start Visual Basic. Usually, 'Start > Programs > Microsoft Visual Studio > Microsoft
Visual Basic 6.0'.
2. Click the default Standard EXE icon and then click the Open button.
3. Click the Command Button icon in the Toolbox. Draw two buttons on the form by
clicking and dragging.
4. Click the first Command Button to select it. Open the property window and change
the Name property to CommandA. Change the Caption to A.
5. Select the second Command Button from the drop down window at the top of the
property window and then change the Name property to CommandB. Change the
Caption to B.
6. Double click the first Command Button object in the form to open the code window
for that button.
7. Enter the code, Msgbox "You clicked Button A" after the automatically entered code
Private Sub CommandA_Click().
8. Double click the second Command Button object in the form to open the code
window for that button.
9. Enter the code, Msgbox "You clicked Button B" after the automatically entered code
Private Sub CommandB_Click().
10. Click the Run button in the toolbar at the top of the VB 6 development environment
window to execute the program.
11. Click either button A or button B and observe the fruits of your labor!
Start Visual Basic. Usually, 'Start > Programs > Microsoft Visual Studio > Microsoft Visual
Basic 6.0'.
Click the default "Standard EXE" icon and then click the "Open" button.
You should see the opening dialog window as explained in Learning Visual Basic. Check that
page out for more.
Step 3
Drag a 'Command Button' from the 'Toolbox' to the form and drop it. Drag another one to
the form and drop it.
If you can't see the toolbox, look for it in the 'View' menu. And notice that several other
components can be turned on and off there, too. Some that you will use are:
Project Explorer
Immediate Window
Locals Window
Watch Window
Another thing to know is that VB 6 uses the right-click 'context menu' a lot. Right click
'Form1' for example and notice the options there. This is the fastest way to switch between
viewing an object (like Form1) and the code in that object.
Learn Visual Basic Version 6 Part 3: Finishing the Two Button
Form
Click the first Command Button to select it. Open the property window and change the
Name property to CommandA. Change the Caption to A.
Select the second Command Button from the drop down window at the top of the property
window and then change the Name property to CommandB. Change the Caption to B.
The new thing in this step is 'Open the property window'. Remember the 'right click context
menu' from the previous page? Try right clicking the first command button and you will
see ...
Click 'Properties' at the bottom to open the window. Then click the 'Name' property in the
first column and change the value in the second. We do this because we want to use unique
names in our program code to refer to this object. Visual Basic will do it automatically (it
already has) but it's hard to keep track of a series of objects with names like 'Command1',
'Command2', and so forth. You might also notice that the Property window is dockable and
resizable. It's been undocked and resized below:
Steps 6, 7, 8, and 9
Double click the first 'Command Button' object in the form to open the code window for that
button.
Enter the code,
Double click the second 'Command Button' object in the form to open the code window for
that button.
Enter the code,
Notice that a subroutine has already been entered into the code window automatically. VB 6
helps out as much as it can! All you have to do is enter your code inside the subroutine. It's
worth knowing that all of the code is actually saved in the form object. VB has to organize
the files for your project and that just happens to be where it is. If you have independent
module code (code that might be used anywhere your program, rather than just for one
particular object and event), it's saved in a different object. Another thing to notice is the
'naming convention' used for the subroutine:
CommandA_Click
This is always the same. The first part is the name of the object. The second part is the
event. You can't change this name or VB won't know where to find your event code. Let VB
assign these names and leave them alone.
Steps 10 and 11
Click the 'Run' button in the toolbar at the top of the VB 6 development environment
window to execute the program.
Click either button A or button B and observe the fruits of your labor!
The only confusion factor here might be, "Where is this 'Run' button?" The answer is, "Right
Here ..." (You can also find this on the 'Run' menu.)
If you still think you need some more help getting up to speed, check out some of the other
web pages in the links on the next page.
Moving Up to Objects
At about this time, a few experienced programmers reading this will be shouting, "VB 6 is
not object oriented!!"
This has got to be one of the great religious struggles in programming. Although the wars
don't rage like they once did, some people have rigidly defined definitions of exactly what is,
and is not, object oriented and according to those definitions, VB 6 and predecessors do not
make the grade. Others, including me, take the view that "object oriented" is a concept,
and not a set of rules. VB 6 is clearly object oriented in concept.
The first thing you need to know about is databases and one of the best to start with is
Access, Microsoft's database for non-server environments. The About.com 'Databases' site
has an excellent article about Access
When you have created (or borrowed ... several great example databases are available
along with VB and Access from Microsoft) a database, the next thing to do is use the Data
Control object to read and write the data. An article about how to do that with Microsoft's
latest data technology.
Another more advanced technology that you can use with VB 6 is called Active Server
Pages, or ASP. This is the technology that allows you to run your VB 6 system on an IIS
web server and server HTML pages controlled by your programming over the web. You can
find an introduction to doing that in a VB 6 environment.
When VB.NET was introduced, Microsoft put millions into the advertising budget because
they were very worried about the competition. They really were not sure that .NET could
measure up. The competition is VB 6