CSE/IMT 100: Assignment 5

 

Writing and Running Your First Visual Basic Program

 

The purpose of this lab is to introduce you to the VB Integrated Design Environment (IDE) and how to create a simple VB project. The IDE is the programming environment where you create applications. A VB project is the collection of files created when you are working on an application. Your TA will go through this environment with you in lab, but to have a better understanding of the terminology and windows in the IDE, you are strongly encouraged to read chapters 1 and 2 of your Visual Basic 6 book. There are a lot of concepts introduced in these chapters, but if you pay close attention to the blue boxes, which highlight practical exercises, you can get up to speed with the VB basics in no time.

 

You will have time to start working on this assignment in lab on Tuesday, October 19. In the unlikely event of VB not being available, your TA will walk you through this or a similar exercise and an addendum exercise will be posted on the course Web page.

 

  1. Open up VB6. On most computers in the library it can be found from the Windows “Start” button by selecting “Programs” and “Microsoft Visual Studio 6.0”.  When you start VB you will be presented with a “New Project” dialog box. You want the default project type, “Standard EXE”.
  2. You will be presented with the IDE, which typically contains the menu bar, the tool bar, the toolbox, and three types of windows: Form, Project, and Properties.
  3. Rename your project: each time you begin a project, the default name, Project1, appears at the top of the form. To change the name, choose Project, Project1 Properties, and then type in your name as the new project name.
  4. Rename the form: when you are working with objects in VB, it is important to realize that there are lots of properties associated with them. Even the form on which you design your application is an object (and has over 50 properties that can be set). To change the form name, click on the Properties window (or select F4) to bring it into focus. Name is the very first property. Select this property field and type in “First”.
  5. Notice that the title bar of the form still says “Form1”. To change this text, select the Caption property and type in “My First VB Program”.
  6. Before we proceed, save your project to a floppy disk. Notice that you are prompted to save not just the form, but the project as well.
  7. Let’s make this form do something. We could just set other properties of the form, but it will be more interactive if we add some controls. Let’s add a button. To add a button, select CommandButton from the toolbox, then place your cursor over the form, hold down the left mouse button, and draw a button. When you release the mouse button, your command button should appear.
  8. Notice that when your button appears, its properties are now in the Properties window. This is because it has focus. Select the caption property of the button and type “Blue” Select the name property and type “cmdBlue”.
  9. Repeat steps 7 and 8, creating another button with the caption “Red” and the name “cmdRed”.
  10. Although we have buttons on our form, we haven’t instructed them to do anything yet. Try running your program. While there are several ways to run a program from the IDE, for now just press F5 or press the Start button on the toolbar. Click one of your buttons. Does anything happen?
  11. To make the buttons do something we need to attach event procedures to them. These are instructions that controls execute when specific events are fired. To attach an event procedure to each of our buttons we need to open the Code Window and write some Visual Basic instructions. To open the Code Window, double click on the blue button. You will be inside of the Click() event procedure. Type in the following code so that your procedure looks like this:

 

Private Sub cmdBlue_Click()

First.BackColor = vbBlue

End Sub

 

  1.  While you are still in the Code Window notice that there are two drop down boxes at the top. The one on the left should say “cmdBlue”. This is the object list box. The one on the right should say “Click”. This is the procedure list box. Select “cmdRed” from the object list box and type in the following code so that your procedure looks like this:

 

Private Sub cmdRed_Click()

First.BackColor = vbRed

End Sub

 

 

  1. Close the Code Window and run your program. What happens? 
  2. Let’s add one more button to demonstrate some other properties you can manipulate. Add a button control and change the caption and name properties to “Name” and “cmdName”.
  3. Now add a textbox control. This is added to the form the same way as the button control. change the name property to “txtName” and the text property to your name. If you’d like, you can also change the font property to set the size, style, and face properties of the font.
  4. Open up the Code Window by double-clicking on the form or by selecting “View” and “Code” from the menu bar.
  5. Select “Form” in the object list box and “Load” in the procedure list box. Type in the following code:

 

Private Sub Form_Load()

txtName.Visible = False

End Sub

 

  1. Now select “cmdName” and “Click” and type in the following code:

 

Private Sub cmdName_Click()

txtName.Visible = True

End Sub

 

  1. Run your modified program. What happens when your form first loads? When you click the Name button?
  2. Save your work before closing VB and leaving the lab.

 

Additional reading and examples:

 

For more information and exercises on adding controls and event procedures, check out Chapter 3 of your VB book.