Vous êtes sur la page 1sur 21

VISUAL BASIC PROGRAMMERS TIPS

1 AN INTRODUCTION
TO

PROGRAMMING

Programming is the art of writing instructions that a computer executes. Sets of instructions, saved in a file are known as a program. Programs can either be saved permanently on integrated circuits, and then known as firmware, or more commonly, on some type of media, such as a hard drive, and known as software. At the very basic level, the microprocessor, the brain of the computer, converts the instructions into electrical signals that perform operations such as adding two numbers or moving data around in the computers memory. A computers electronics are composed of millions of transistors that act like tiny electronic switches. They can either be on or off, represented by binary digits (1s and 0s). To program the earliest computers, in the 1940s and 50s, programmers had to understand how the computer interpreted different combinations of 1s and 0s. These programmers wrote all of their programs using binary digits. As programs became larger, programmers found it impractical to work in terms of the computers 1s and 0s. Instead, programming languages were created that let programmers express computer instructions in a form more meaningful to the programmer. This form is called a language because its instructions are comprised of words, numbers, and acronyms. After programmers place their programming language instructions in a file (called a source file), a second program, called a compiler, converts the programming language instructions into the 1s and 0s (called machine code) understood by the computer. The files on your disk with the EXE and COM extensions contain the machine code (1s and 0s) the computer will execute. Figure 1 illustrates the process of compiling a source code file into an executable program:

Figure 1 A compiler converts source code instructions into machine code. As discussed above, when you create programs, you specify the desired instructions using a programming language. Visual Basic is only one of many programming languages. Some programmers use programming languages such as C, C++, Pascal, and FORTRAN. Different programming languages provide unique features and have their own strengths.. Programming languages simplify the definition of the instructions you want the computer to execute.

Resolution

13

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


2 HOW VISUAL BASIC STORES SOURCE FILES

Visual Basic stores your source files in either an ASCII or a compressed ASCII file whose filename contains one of six extensions, as shown in Table 2:
File Extension File Type

.frm .bas .cls .ctl .pag .res

A form provides your programs visual means of representing data. A code module stores related pieces of code that do not require a form. A class module is a special type of code module which contains a single class and its associated objects. A user control allows you to add your own custom controls to your Visual Basic program. A property page enables you to add the ability to view an objects properties in your program.

A resource file holds the programs bitmaps, text strings, and other presentation-oriented information. Table 2 The file types used by Visual Basic to store information. When writing programs with a user interface, the heart of your program will be the Visual Basic form. Adding visual controls and attaching programming code to them, you can quickly build powerful programs. Because the number of files in larger software development projects can become cumbersome, Visual Basic maintains a Project File (extension: .vbp) to manage the forms, modules, and resource file used by your project. The Project File contains no instructions of its own, but merely advises Visual Basics built-in editor and compiler of the files that make up your project. For example, if you create a Visual Basic program that performs payroll operations, you might use three forms and a code module. You might name the code module Payroll.bas, and within it, place mathematical and accounting routines used throughout the program. You might name the form modules Parent.frm, Entry.frm, and Ledger.frm. You could design each form to provide the user with an interface to a separate type of financial data. You would save all of these files together in the Payroll.vbp project. Figure 2 shows the files and their interaction.

vb_025.p65

14

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

computations

Figure 2 The Payroll.vbp project and its component files.

CREATING

AN

EXECUTABLE PROGRAM

As you learned in Tip 1, a computer understands only binary code (series of 1s and 0s). To create an executable program, you must convert the Visual Basic instructions contained within your forms, code modules, class modules, and resource files into 1s and 0s. To do this, you run the Visual Basic compiler, which converts the human-readable code youve written inVisual Basic to binary, machine-readable code. There are several options for running the compiler program, including; compiling as you go, executing with full compilation, and creating a standalone executable. If you wish to create a standalone executable, use the following steps: 1. Save your project file by selecting Save Project from the File menu. 2. Select the File menu Make Project YourProjectName.exe option. Visual Basic will respond with the Make Project dialog box. 3. Click your mouse on the OK button in the dialog box. Visual Basic will display two blue bars, one after the other, at the right end of the toolbar, one captioned Compiling, the other captioned Writing Exe. 4. Select the Windows Start menu Run option and enter the path and name of the newly created executable file. Click your mouse on the OK button. The following Tips walk you through the steps required to create and compile a Visual Basic program. By Tip 44, you will create your first executable Visual Basic program.

GETTING STARTED WITH THE NEW PROJECT DIALOG BOX

Each time you start Visual Basic, the New Project dialog is displayed. The New Project dialog box provides you with an easy way to create the framework for whatever type of project you want to create. Throughout this book, you will create new projects using many of the templates found in the New Project dialog. At this point, it is only important that you know these templates exist, as

vb_025.p65

15

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


you will initially use either the VB Application Wizard or the Standard EXE blank project when designing your projects. Figure 4 shows the Visual Basic New Project dialog box. You will also notice that the New Project dialog also give you easy access to projects you may have already created. Clicking the Existing tab will allow you to explore your hard drive for saved projects, or you can also select the Recent tab, which lists your most recently accessed projects.

Figure 4 The New Project dialog box.

USING THE NEW APPLICATION WIZARD

One of the options found in the New Project dialog that appears each time you start Visual Basic is the VB Application Wizard. If you are familiar with the Windows platform, you know that wizards provide a way for you to expedite repetitive tasksthe VB Application Wizard makes creating a new project simple by displaying a series of options, assisting you in creating your programs framework. Although you can create an executable program entirely with the VB Application Wizard, the VB Application Wizard is intended only to speed your process of creating a project framework, not to build full programs. Figure 5 shows the New Project dialog box with the VB Application Wizard icon selected.

Figure 5 Selecting the VB Application Wizard icon.

vb_025.p65

16

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


6 UNDERSTANDING INTERFACE TYPES

When you create a Windows-based program, you must select one of the two basic types of Windows interfaces: Multiple Document Interface or Single Document Interface. Multiple Document Interface (MDI) enabled applications are the Windows standard. The majority of Windows applicationsfrom Microsoft Word to Intuit Quickenuse a Multiple Document Interface. The words Multiple Document refer to the ability of the program to maintain several different presentations or files within a single overriding window. Figure 6 shows an example of the Multiple Document Interface. Notice that three separate documents are open within the Microsoft Word window, and one is minimized in the bottom left corner.

Figure 6 The Multiple Document Interface in Microsoft Word. Single Document Interface (SDI) enabled applications are more like Web pages where moving from one to another means closing the preceding view. Single Document Interfaces are the most simple programs, and you will use them to discover more about Visual Basic.

USING THE VB APPLICATION WIZARD TO CREATE A PROJECT FRAMEWORK

To begin creating a new project framework, which you will use for the next 11 Tips, doubleclick your mouse on the VB Application Wizard icon that appears in the New Project dialog box, as previously shown in Figure 5. Visual Basic will display a dialog box explaining the purpose of the Application Wizard. Figure 7 shows the Application WizardIntroduction dialog box. Notice in Figure 7 that you can choose to load a saved profile. If this is your first time, there will not be any profiles to choose from. To move to the next dialog box, click the Next button. Visual Basic will display the Application WizardInterface Type dialog box.

vb_025.p65

17

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 7 The VB Application WizardIntroduction dialog box.

SELECTING AN INTERFACE TYPE IN VB APPLICATION WIZARD

There are three options within the Application WizardInterface Type dialog box: Multiple Document Interface (MDI), Single Document Interface (SDI), and Explorer Style. You can see a sample of each type of interface as you select each type. Watch as the graphic and description change on the left side of the Application Wizard Interface Type window. VB Application Wizard provides a third Interface Type not discussed in Tip 6, the Explorer style. The Explorer Style interface is an SDI-type interface, with the initial form divided into two frames, both of which are designed to handle what is known as a TreeView. You may be familiar with this type of tree-structured interface when you use the Windows Explorer to access files on your hard drive. The directories and files are represented in a tree, with branches that expand showing files and directories at different levels of the tree. You will work with the Explorer Style in later Tips. For the sample Project framework you are creating, you are designing an SDI application. Select the Single Document Interface radio button. Figure 8 shows the Application Wizard Interface Type dialog box with the SDI application radio button selected.

Figure 8 Selecting a Single Document Interface. Select an Interface and enter the name of your software application in the text box. Programs can also be called applications. To continue on with the Wizard, click the Next button at the

vb_025.p65

18

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


bottom of the Application WizardInterface Type dialog box. Visual Basic loads the Application WizardMenus dialog box.

UNDERSTANDING RADIO BUTTONS

Car radios often have buttons that the listener pushes to select pre-programmed stations. Each button is programmed so that the radio will tune to a specific radio station so that the listener can find the correct station without having to use the tuner dial. Radio buttons in a user interface are similar to this type of selection. In later Tips, you will use radio buttons when you want the user to select only one option from a list of several. In this particular example, your program can consist of only one type of interface. Figure 9 shows only the radio buttons on the interface page.

Figure 9 The radio buttons on the Application WizardInterface Type dialog box.

10

SELECTING MENUS IN APPLICATION WIZARD

The Application WizardMenus dialog box that Visual Basic displays after your completion of Tip 8 is another way the VB Application Wizard can simplify your coding by creating starter menus for you. The VB Application Wizards starter menus contain all the navigational elements of a menu, with the ability to cause menus to drop down and select items, but the starter menus do not have code associated with the selection, so they dont do anything until you add the instructions to tell them what to do when selected. In other words, the Wizard will provide the code to display the menus. You, in turn, must write the code the application performs when the user selects a menu option. Again, Visual Basic attempts to speed the initial stages of your development process by omitting some of the design issuesnamely, menus, resources files, and database connectivity. If you choose not to add menus within the VB Application Wizard, you can add them to your project at any time during development using the Menu Editor, as discussed in Tips 29-38. For this project, you will not create any menus. Instead, unselect each of the checked boxes next to the menu names shown checked in Figure 10. Clicking the Reset button will recheck the default selections. When youre ready, click the Next button of the Application WizardMenus dialog box. Visual Basic will display the Application WizardCustomize Toolbar

vb_025.p65

19

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 10 The Application WizardMenus dialog box with the checkboxes cleared.

11

USING THE APPLICATION WIZARD TO CREATE A TOOLBAR

Toolbars are the rows of icons, normally found located beneath the menus of many Windows applications, such as Microsoft Word. Each icon represents a task, such as save, copy and print. The Application Wizard Customize Toolbar makes adding these icons into your application very simple. Figure 11 shows the two selection lists of icons, one on each side of the window.

Figure 11 The Application WizardCustomize Toolbar dialog box. The list to the left of the window contains icons not currently included in the toolbar. To add an icon to the toolbar, click once on it with your mouse, highlighting it, then click the arrow pointing to the right (toward the window on the right). The icon will be moved to the window on the right, which includes the icons found in the toolbar, and the icon will no longer appear in the window on the left. You will also notice that the icon will appear in the toolbar above these lists. To remove an item in the toolbar, select the icon in the list to the right, and click the arrow pointing to the left. You will see that the selected icon is removed both from the list on the right as well as the toolbar. Clicking the Reset button will return the toolbar to a default setting. When you have finished customizing the toolbar click the Next button. Visual Basic will move to the Application Wizard Resources dialog.

vb_025.p65

20

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


12 USING THE APPLICATION WIZARD TO ADD RESOURCE FILES

The Application WizardResources dialog box lets you add a resource file to your Project framework. You learned in Tip 2 that Resource Files are used to store strings of text in a single, shared area. You will use Resource Files in Visual Basic primarily for distribution of programs in different languages. Resource files are also useful if you use a large number of strings of text within your project, or you use some the save text repetitively. You will use the Resource File in later tips. Select the No radio button on the Application WizardResources dialog box. Figure 11 shows the Application WizardResources dialog box.

Note: The Visual Basic resource file is radically different from the Visual C++ resource file. The
Visual Basic resource file is more similar to the Visual C++ string table than it is to anything else in Visual C++.

Figure 12 The Application WizardResources dialog box with no resource file selected. Click your mouse on the Next button on the Application WizardResources dialog box. Visual Basic will display the Application WizardInternet Connectivity dialog box.

13

USING THE WIZARD TO ADD AN INTERNET CONNECTION

The Application WizardInternet Connectivity dialog box lets you add a custom Web browser to your Project framework. Included in the new Visual Basic 6.0 set of Common Controls are a set of controls that provide the programmer with an easy means of enabling programmatic access to the Internet. These controls include a simple browser control. If you develop an application to distribute to your customers or other third party users, or you develop shareware and want people to register the program online, the browser inclusion feature is helpful. These are only two suggested uses for the browser featuresthere are an infinite number of ways to use a browser within a program. In later tips, you will learn how to create Internet-aware programs and controls. Select the No radio button on the Application WizardInternet Connectivity dialog box. Figure 13 shows the Application WizardInternet Connectivity dialog box.

vb_025.p65

21

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


Click your mouse on the Next button within the Application WizardInternet Connectivity dialog box. The VB Application Wizard responds with a dialog box entitled Application WizardStandard Forms.

Figure 13 The Application WizardInternet Connectivity dialog box with No selected.

14

UNDERSTANDING CONTROLS

In Tip 13, you learned that Visual Basic 6.0 includes a set of controls to assist the user in reaching the Internet. Controls are tools provided to programmers to assist in the creation of programs. Visual Basic comes with many standard controls: command buttons, labels, radio buttons, and more. Together with forms, controls are at the root of everything you will do in Visual Basic. Simply put, a control is the means by which your programs will communicate with users. Sometimes, controls work in the background, like the ImageList control, introduced in Tip 496. More often, controls will have a visual manifestation, like menus, toolbars, radio buttons, and command buttons. However, all controls have functionality that makes them important in program design. Initially, you will work with the more simple controls like labels, edit boxes, command buttons, and radio buttons. In Tip 363, you will learn about the Common Dialog control. Further, in Tips 804-806, you will learn about more complex ActiveX controls. Figure 14 shows the interaction of controls with your program and your users.

Figure 14 The interaction between programs, users, and controls.

vb_025.p65

22

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


15 INCLUDING STANDARD FORMS

The Application WizardStandard Forms dialog box asks if you would like to include any, some, or all of four forms: a splash screen form, a login dialog, an options dialog, and an about box. These forms are common to many commercial and enterprise products. Briefly, the splash screen is a screen that paints while the program is loading. Typically, a splash screen will contain some type of corporate graphic, a version number, and the programs registered user. You may include any information you desire on the splash screen, but remember it only loads briefly. You will learn more about the strategic use of splash screens in Tip 921. The login dialog can be used to protect access to your application or to data accessible through the application. You will learn more about login dialogs in the section on databases. The options dialog is another standard Windows item. Also known as a preferences dialog, it offers users options for customizing a program to fit their specific needs. The about box is similar to the splash screen. Most commonly a Help menu option, the about box typically states the product name, shows a graphic, and provides registration information. Lastly, the VB Application Wizard displays an option to include other form templates in the form of a command button. You will not add any form templates at this point. You will learn about form templates in Tips 38-39. You will not include any of these options within your project. If any of these options are selected, deselect them by clicking the left mouse button on each checkmark until it disappears. Figure 15 shows the Application WizardStandard Forms dialog box.

Figure 15 The Application WizardStandard Forms dialog box with no forms selected. Click your mouse on the Next button on the Application WizardStandard Forms dialog box. Visual Basic will respond by displaying the Application WizardData Access Forms dialog box.

vb_025.p65

23

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


16 CONNECTING TO DATABASES USING THE WIZARD

The Application WizardData Access Forms dialog box helps you generate basic forms from a database for use within your application. As your Visual Basic applications become more complex, your applications may need to interact with a database. In later Tips, you will learn how to associate your applications with most commonly used databases. You can create simple database access forms within the VB Application Wizard. While the VB Application Wizard is helpful for creating these simple forms, you will eventually discover that the majority of your database applications require custom form design. However, the Wizard-designed forms are useful both for understanding usage and structure of the database access and for creating the base form for such database access within your application. For example, if you create a simple phone book application, the Wizard-generated data access form would be sufficient to return the phone number data. If you create a program intended to manage clients, vendors, personal phone numbers, and order information, the Wizard-generated data access form would not be sufficient for your entire programbut it might provide a valuable template to use in creating that program. Do not select any Databases and deselect any Databases that are currently selected on the Application WizardData Access Forms dialog box. Figure 16 shows the Application WizardData Access Forms dialog box.

Figure 16 The Application WizardData Access Forms dialog box with no database forms selected. Click your mouse on Next in the Application WizardData Access Forms dialog box. Visual Basic then displays the Application WizardFinished! dialog box.

17

FINISHING THE CREATION OF YOUR NEW WIZARD GENERATED PROJECT

The Application WizardFinished! dialog box is the last dialog that the VB Application Wizard displays to you during creation of a new project by the Wizard. This dialog box gives you both information and options.

vb_025.p65

24

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


The Application WizardFinished! dialog box also asks if it should display a summary of what steps its has performed in designing your project framework. Click the View Report command button. The VB Application Wizard then displays a brief summary of what it has created (including any menus, data control objects, internet controls, standard forms, and more) and what steps it suggests you perform next. The Application WizardFinished! dialog box also allows you to save all the steps completed in Tips 7-15 as the profile that can be used for any future projects. Do not save your profile. Click your mouse on the Finish button within this dialog box. The Application WizardFinished! dialog box is displayed in Figure 17.

Figure 17 The Application WizardFinished dialog box with no profile name saved. The VB Application Wizard will create a project file, a module file, and a form file, and will alert you that it has created the application. Click your mouse on the OK button in the Application Created dialog box.

18

READING THE VB APPLICATION WIZARDS SUMMARY REPORT

The VB Application Wizard has used the selections you made in Tips 7-16 to create a project framework. The VB Application Wizard presents you with some brief notes on what actions it has performed while assisting in the design of your project. These notes may vary from use to use of the VB Application Wizard, depending upon what options you selected within the Wizard. The VB Application Wizard directs you to certain portions of the program to provide you with starting points to make your customized changes. Additionally, the summary report alerts you to the existence of: The Data Form Wizard The Class Builder The ActiveX Document Wizard You will learn about each of these tools in later Tips. Finally, you may save or close the Summary Report. To close the Summary Report, click your mouse on the Close button. Figure 18 shows the Application Wizard Summary Report.

vb_025.p65

25

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 18 The Application Wizard Summary Report window for the TipExam1 Project.

19

SAVING YOUR NEW WIZARD-GENERATED PROJECT

In Tip 18, you completed the creation of your first Visual Basic project. However, unless you save the information somewhere within that Project, the information and the project are lost when you exit the Visual Basic editor. Therefore, select the File menu Save Project As option. Visual Basic displays a series of Save As dialog boxes, one for each Project component. The Save As dialog box will display the current directory as the VB directory under the Microsoft Visual Studio directory, which is probably located in your C:\Program Files\ directory. Change the directory within the Save As dialog box, until you are looking at the Microsoft Visual Studio directory. You will find a directory named MyProjects displayed within this directory. MyProjects folder does not exists on my directory. Double-click your mouse on this directory to select it. Create a new sub-directory within the MyProjects directory named TipExam1. Double-click your mouse on this directory to select it. Save each of the files (one form, one module, and one project) within this newly created TipExam1 directory using the default names provided to you by Visual Basic. You can check your work within the Windows Explorer. Figure 19 shows the Windows Explorer view of the TipExam1 directory.

Note: TipExam1 directory contains four files, not the three you might expect, since you saved only
three. The frmMain.frx is a file Visual Basic creates to speed your projects compilation. Close or minimize Windows Explorer after you view the files you just saved.

vb_025.p65

26

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 19 The TipExam1 sub-directory of the MyProjects directory.

20

CREATING FULLY EXECUTABLE FILES WITHOUT ENTERING ANY CODE

The VB Application Wizard creates a file that is fully executable, without you entering a single line of code. However, just because the file is fully executable does not mean it is fully functional. The code you generate with the VB Application Wizard is generally insufficient to accomplish any tasks, with the exception of code you generate to access simple database information. The Wizard is not designed to do your work for you. Rather, the VB Application Wizard expedites some of the more dreary setup work so that you can focus on the important issueslike making your applications work!

21

UNDERSTANDING THE EDITING WINDOW

By following the steps described in Tips 5-18, you created and saved your first project. The Editing Window, as seen in Figure 21, is where you will perform the majority of your work during the design and implementation of any Visual Basic program. From within the Editing Window, you will design forms, edit code, and debug programs.

vb_025.p65

27

3/15/01, 5:08 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 21 The Visual Basic Editing Window. The Editing Window contains the following elements, as indicated on Figure 21, which you will learn about in the next five tips: The Toolbox The Properties Window The Project Explorer The Form Layout Window The Editing Display Window

Additionally, Visual Basic displays its standard toolbar and a standard menu bar. You will learn about the standard toolbar and the standard menu bar in Tips 27 and 28.

22

UNDERSTANDING THE TOOLBOX

The Toolbox is the first of the elements that comprise the Editing Window introduced in Tip 22. By default, Visual Basic places the Toolbox on the left side of the editing window. When you first start Visual Basic, the Toolbox will contain icons that represent the most commonly used controls. If the Toolbox is not visible, select the View menu Toolbox option, and the Toolbox will become visible. If the Toolbox is not on the left side of the Editing Window, click your right-mouse button once on the Toolbox. Visual Basic will respond with a popup menu. Within the popup menu, click your mouse on the Dockable option until it is checked. The Toolbox will then relocate to one of the edges of your screen, at which point you may drag-and-drop it until the Toolbox is positioned correctly on the left side of the Editing Window.

vb_025.p65

28

3/15/01, 5:09 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


As you design the visual component of each project, you will select icons from the Toolbox and place the associated controls upon the appropriate form. For example, the last icon in the first row of the Toolbox is a capital A. I dont have the A in the same place, perhaps the order of the tools is not the same in all computers. Click your mouse on the A icon. You will see that the A becomes sunken. Move the mouse pointer until it is over the blank form. Visual Basic will then change the mouse pointer from an arrow (or whatever your default pointer is) to crosshairs. Click your mouse somewhere in the upper left-hand corner of the form, below the toolbar, and hold the button down. Drag the mouse toward the bottom of your screen and to the right. Visual Basic will draw the outline of a box. After the box is about four grid spaces high and ten grid spaces wide, release the mouse button. Visual Basic will draw a blank rectangle containing the text Label1. Figure 22 shows the Toolbox and the Figure 22 shows the form frmMain with the new Label1 drawn upon it.

Figure 22 The Toolbox with the Label control selected.

Figure 22 frmMain with the Label1 control placed.

vb_025.p65

29

3/15/01, 5:09 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


23 UNDERSTANDING THE PROPERTIES WINDOW

In the previous Tip, you learned how to use the Toolbox to draw a label on a form. Labels are a type of control. The caption that appeared for the new label, Label1, is a property of that control. Properties are values used by every type of Visual Basic control, from forms to labels. Much as you would describe a house as being painted blue, a property describes the control with which it is associated. Most properties may be either read from or written to by the programmer when designing a form or when running a project. To understand this better, consider a house. Determining what color the house is painted now is equivalent to reading from its Color property; painting the house is equivalent to writing its Color property. Note that some properties of some controls will be read-only at runtime. Runtime is during execution, as opposed to design time, which occurs prior to attempting to run the code. If you attempt to set a property which is read-only at runtime, Visual Basic will alert you with a runtime error dialog box. You will learn more about the many runtime errors and their causes in later Tips. The Properties Window contains information about the object currently selected within the editing window. Click your mouse on the Label1 control that you created in the previous Tip. The Properties Window should appear as shown in Figure 23.1, which shows the Label1 controls properties. Note that property information appears, by default, in alphabetical order, by property. You may choose instead, to display property information by category.

Figure 23.1 The Label1 controls Properties Window. Properties common to most controls and forms include (Name), Height, Width, Scale Height, and Scale Width. You will set or retrieve Properties to control or provide information about the specifics of a form or control. For example, click twice with your mouse on the Caption property to select the entire block of text. Delete the current caption and change the label caption to Properties Example. Figure 23.2 and Figure 23.3 shows frmMain and the Properties Window reflecting the changed caption.

vb_025.p65

30

3/15/01, 5:09 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 23.2 The new Properties Windows values

Figure 23.3 The changed form. You will learn in Tip 43 how properties can also be changed at run time, through program code. Whenever you work with an object variable (Tip 91), you are setting its properties.

24

UNDERSTANDING THE PROJECT EXPLORER

The Properties Window is a dialog box which looks similar to the Windows Explorer. Like the Windows Explorer, this dialog box contains files and folders and lets you move through the currently open Project by traveling the trees, branches, and leaves that comprise the Project. Additionally, the Project Explorer contains three icons along its title bar: a View Code icon, a View Object icon, and a folder icon. The View Code and View Object icons let you control what you see of an objectits visual representation or the underlying code. If you want to work on an objects visual design, select the object name from within the tree and click your mouse on the View Object icon. If you want to see the code attached to a given object, select that object and click your mouse upon the View Code icon.

vb_025.p65

31

3/15/01, 5:09 PM

1001 VISUAL BASIC PROGRAMMERS TIPS


The folder icon is intended to help you better navigate the items within your project. If you click your mouse on the folder icon, it toggles the treeview from multiple levels to just one containing all objects within the project. Effective use of the Project Explorer will decrease both your code editing time and your information retrieval time when developing applications. Figure 24 shows the Project Explorer window.

Figure 24 The Project Explorer window with the folder icon selected and Module1 displayed.

25

UNDERSTANDING THE FORM LAYOUT WINDOW

With Visual Basic, you will use forms to design your programs visual interface. You will layout forms within the Editing Window. Sometimes, you will design a form that appears differently within the Editing Window than it does during program execution. The Form Layout window helps you position forms on the screen to your satisfaction. The Form Layout window also makes it easier for you to see the appearance of your forms in the event a user has their display set to different resolution levels than your display. The Form Layout window depicts three screen resolutions 640x480, 800x600 and 1024x728. The largest resolution fills the window; the other resolutions are indicated within the Form Layout window using dashed and labeled lines. As you design more complex projects, you will learn methods of determining the users system settings and adjusting your programs displays appropriately. If you already have your forms laid out, or after you are comfortable with how your forms translate between resolutions, you may want to close the Form Layout window to make more room for the Properties Window. Figure 25 shows the Form Layout window.

vb_025.p65

32

3/15/01, 5:09 PM

1001 VISUAL BASIC PROGRAMMERS TIPS

Figure 25 The Form Layout window with frmMain in the display area.

26

UNDERSTANDING THE EDITING DISPLAY WINDOW

The Editing Display window uses the remaining space within the Visual Basic Editing Window. Within the Editing Display window, Visual Basic will: Display visual representations of your forms Show code text within forms, code modules, and class modules Show the Debug window when debugging a program

The majority of your interaction with Visual Basic occurs within the Editing Display window. The Toolbox, Properties Window, Project Explorer, and Form Layout window are all supporting items, important as assistants. But the action really occurs within the Editing Display Window. Figure 26 shows the Editing Display window with an open form and a code module tiled horizontally.

Figure 26 The Editing Display window with two windows open and tiled horizontally.

vb_025.p65

33

3/15/01, 5:09 PM

Vous aimerez peut-être aussi