Lab 0 Introduction to Visual Basic Spring 2001

______

Pre-lab (to be completed before start of lab)

(none)

In Lab

The T. A. will introduce the laboratory environment and working policy and rules. Bring in a 3.5-inch diskette.

In this lab you will do the following tasks:

·  discuss basic lab rules

·  create a working directory on the networked drive g:

·  create a simple Visual Basic interface consisting of two buttons

·  create a more complicated Visual Basic interface that acts like an integer calculator

To receive credit for the lab, you must always “check out” with the T. A. to verify that you have completed these tasks. Pre-labs will be expected to be complete before lab starts, and the next pre-lab will be passed out during the current lab. There will be a few lab quizzes this semester at the start of each lab which cover (ONLY) the questions covered in the pre-lab. Obviously, the topics covered in each pre-lab are important and each student is expected to be familiar with them before entering the lab.

Lab Rules

The following rules are in place in this lab.

1.  Do NOT, for ANY REASON, download software onto any computers in this lab. These computers run a special version of NT, as well as contain various hardware boards, both of which will very likely conflict with even basic software (such as WinZip, etc.). If you want software, please let the T. A. know, and we will go through the procedure of installing the software for you. We will NOT install word processing software on these machines, simply because these machines are expressly dedicated to control. If software is installed improperly, and the machines crash, all data on that particular computer will be lost for all students using that computer. A crashed computer may be down for some time. Don’t download software, or load software from disk, and watch for other students that may attempt to do this.

2.  Back up your own data. The T. A. and Dan Block are NOT responsible for lost data. We do periodically back up the computers, but we will be very reluctant (or more likely unable) to replace lost data because a student did not back up their system.

3.  Do NOT copy other students’ software. Because the drive is networked, it is really easy for students to gain access to other students’ directories. If you worry that another student may copy your files, do NOT leave them on the g drive until the date they are due. In the past, one “cheating” student was looking at an “honest” student’s file while the “honest” student was saving a new version of that file to disk. The system automatically saved the old version over the new version, causing the “honest” student to loose all the work they had done up to that point. Students should try to learn the material on their own. This is not only for ethical reasons, but also because each group will be asked to write unique, fully functional GUI’s and c-programs. In the past, exams have included programming portions.

4.  Basic courtesy is a must because there are a limited number of machines. Particularly on the final projects, do not leave a computer with all the wires connected to your project. If you do, don’t expect to find them connected when you return. Do not take wires away from another bench, or keep tools you might be using. Respect the time blocks listed on the final-project sign up. If you oversleep (particularly for the 8:00 labs), be discreet when you come in and try not to disturb the lecture. Do not ask to make up a missed lab quiz unless you give the T. A. prior notice.

Creating a Working Directory

Please develop your software in your sub-directory under g:\Spring01\<yourname>. To create your sub-directory, you can use the Windows explorer, or type in a DOS window:

G: [enter]

cd Spring01 [enter]

md <yourname> [enter]

To get into your sub-directory type: cd g:\Spring99\<yourname>

When you are ready to leave the lab you should back up your work onto a diskette. For example if you are in a dos window in your directory type:

copy *.* a:\

Creating a Simple Visual Basic Interface (30 minutes)

1.  Open up Visual Basic by double clicking on the shortcut icon or by using the Start| Programs | Microsoft Visual Basic 6.0 | Microsoft Visual Basic 6.0.

2.  In the “New Project” Window, open a “Standard.EXE” formatted form.

3.  Rename the Form from “Form1” to “frmSimple” by changing the (Name) field in the Properties window. Note that we will strictly follow the STANDARD naming conventions for forms, controls, etc. listed in (Warner) page 57.

4.  Add a CommandButton control to your form by double clicking on the CommandButton key in the “General” Controls window.

5.  Rename the control to “Press Me” by changing the Caption field in the Properties window for that control.

6.  Copy this control you made by selecting it, and hitting control-c (or Edit | Copy from the menu). Paste a copy by hitting control-v (or Edit | Paste from the menu). Select “No” to the question of whether you want to create a control array (you can teach yourself more about this later, if you are interested).

7.  Save your work thus far in your working directory on the g drive. Note, Visual Basic will first ask you what to name the file as (just choose the default, frmSimple). It will then ask you to save the project. Choose prjSimple as the name.

8.  Align the two “ Press Me” keys side by side.

9.  Name the left CommandButton as cmdLeft by selecting the Left button and changing the (Name) field.

10.  Rename the right button in a similar manner as cmdRight.

11.  Double click on the left button. Note that VB opens up a Code window to create code that is called when the button is clicked. We can make the button now do something by writing code in this area. Write the following:

cmdLeft.Enabled = False

cmdLeft.Caption = "Can't Hit Me!"

cmdRight.Enabled = True

cmdRight.Caption = "Hit Me"

Note that VB checks the correctness of the code on the fly! To complete a field that VB has given a fixed option, simply hit tab (this will make more sense when you are sitting in front of a computer).

12.  Put the same code in the right button event by copying the code from the left button, double clicking on the right button, and pasting the code in the “cmdRight_Click” event handling area. Note, DO NOT paste the code in the “Form_Load” area, which is called when the program starts. Everywhere you read “left” , swap it with “right”, and vice versa.

13.  Run the code by hitting the run arrow. Play with your new GUI a little.

14.  Stop the code by hitting the X box or by double clicking in the upper left-hand corner of the program box. Note: DO NOT use the stop button, or get in the habit of doing so. This will PAUSE the VB code, but not run the stop code listed in your Visual Basic routine. Later, when we add this stop code, it will be very important that the stop code runs, as it tells the real time software running in C to also stop. Failure to do this may crash VB in the future!

15.  Add a stop button to the GUI by dragging a out a new command-button, naming it cmdQuit, and putting the code Unload Me in the code section. VB associates the word Me with the current project, and so the command Unload Me exits the current project.

16.  Add a few bugs to the code, to see what happens.

17.  You can create an executable of the GUI by going into File | Make prjSimple.exe. Note: this asks you for options, etc. and actually creates a stand-alone program that can run on any Windows platform. The executable will run significantly faster than inside VB, but obviously will be more of a pain to re-compile. I suggest only using this option if you are finished with a project, and you do NOT need to do it for this lab. This is just for your information.

Creating a Calculator GUI in Visual Basic (30 minutes)

1.  Copy the calculator files off of the f: drive, in Lab0, to your directory.

2.  Run the calculator program, and figure out how it works from looking at the code. This calculator supports integer math, but it has a few flaws a normal calculator would not have (see if you can find them).

3.  Change the integer support to floating point, to create a floating-point calculator. You will have to add a “point” key. Note that the “point” key event will not be the same as the other number button events.

4.  Rename the calculator to something else, and find a new icon for it.

5.  Check out with the lab T. A. that your new calculator works.