This is basic of Visual Basic
Your Free IT Training Links
PHP
ASP
Visual Basic
Oracle
C-Language
Micro Soft Office 
Home
Courses
Faculty/Facilities
Students Notes
Admission
Discussion
Learning Visual Basic

Learning VB 6

In the 'Essentials' topic Learning Visual Basic, a program that uses one form with two command buttons was introduced.

We're going to go through the complete steps to create this.
First ... in rapid fire ... the steps.
Then ... a more complete explanation.

  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!
Part 2: The Two Button Form In Detail
 Join the Discussion
Need a component that isn't in the toolbox? Ask about it in the discussion forum.
Discussion Forum
 
 

.Step 1 and Step 2

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 as explained in . 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.

The VB 6 development environment is designed to let you constantly change the components that are visible so you can work with only those windows that apply to the problem at hand. The 'ToolBox' is one of those components and may, or may not, be visible in your system. The 'Toolbox' looks like this: (The tools that are in the toolbox can change too!)

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.

 Join the Discussion
If your two button form doesn't work, let's find out why in the discussion forum.
Discussion Forum
 
 

In the previous page, we actually completed the design of the form for our project ... which only consisted of dragging two command buttons to the form. In this page, we change their names, add the event code for both buttons and actually run the program.

Steps 4 and Step 5
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,

Msgbox "You clicked Button A"

after the automatically entered code

Private Sub CommandA_Click()

Double click the second 'Command Button' object in the form to open the code window for that button.
Enter the code,

Msgbox "You clicked Button B"

after the automatically entered code

Private Sub CommandA_Click()

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.

 

Copyright © College of Information Technology Dera Ismail Khan (CIT)