Introduction to Visual Basic Visual Basic offers a very comprehensive programming environment that can be a bit overwhelming at the start. The best rule is to ignore all that you do not need until you need it! Make sure that you get help from the lab tutors if you are unsure. When you startup Visual Basic, you are presented with a New Project and a default option to Open a Standard EXE application. Choose Open. The VB environment looks like this: Menus (some choices must be further specified through dialog boxes) Toolbar (quick access to some commands) Project Window (display a form in the form design window, or its code in the code window) Properties Window (change the appearance and/or behaviour of objects) Toolbox (create objects) Code Form design window (design the appearance of objects on your forms) Window (further define the behaviour of objects)
Terminology Project: Collection of forms, code modules. Project Window lists all the files in the application. Form: Window that displays controls and graphics useful to the overall project. Can be private (normal) or public. Code Window: To make application respond to events, code is written for the required events eg. click command button (click event) or enter text (keypress event) Properties Window: Allows programmer to set the properties of each object eg. name, caption etc.. Toolbox: Allows programmer to easily create the desired GUI by selecting the appropriate controls. A First Application: A VB application to convert temperatures between Fahrenheit and Celsius. The Temperature Conversion application consists of these elements: Recall the steps to creating an application: 1. Design the interface. 2. Plan properties. 3. Plan code. 4. Implement each of the above 1. Create the interface. To create an application, you first need to open a new project: choose the New Project command from the File menu. Every new project automatically contains one form. (You can add as many additional forms as your application needs.) The Temperature Conversion application requires only one form:
Next, you can select the tools you need from the Toolbox to draw the controls you want on the form. In this application, 2 text boxes will accept user input and display text. To create a control, select the tool from the Toolbox, and then hold down the left mouse button while dragging out an area on the form. For this application, you will require 2 text boxes and 2 labels. 2. Set properties. You set the properties of a control in the Properties window. When you select a form or control, its properties and their settings are displayed in the Properties window. You use the Name property to refer to a control in code. In this case, we'll make txtcels the name of this text box.
The Caption property specifies the text that is displayed in the control. We'll change the label's Caption property to "Celsius". We'll also change the Name property of the other text box to txtfahr and the Caption property of its label to "Fahrenheit". 3. Write code. We now need to add code to make the application respond to the user's actions. As with any programming language, you need to develop your algorithm at this stage before attempting to enter code. When you double-click a control, the focus shifts to the Code window. The name of the control appears in the Object list box. The Procedure list box contains a list of events for the control. Since we want to run the code in this application when a user presses a key, we'll select the KeyPress event from the Procedures list box.
We'll use the following formulae to convert the temperatures: Cels = (Fahr - 32) * 5/9 Fahr = (Cels * 9/5) + 32 We'll enter the code for the event procedure: (Recall ASCII codes) We must also attach a similar event procedure to the other text box, which will update the Celsius text when Fahrenheit is changed by the user (cf Lab Assignment) Saving Your Work Please ensure that you save your work correctly on the Network Drive (Not the local hard drive). Create a separate directory/folder for each of your assignments and use this directory/folder for your work. Use Save Project As..from the File menu to save the finished project It will ask you to save your forms first. Make sure you select the correct folder in which to save your work. It will then ask you to save the overall project. Choose an appropriate name. Running your application To run your application use Start from the Run menu.