Visual Basic Tips

  • Upload
    benoyc

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 7/31/2019 Visual Basic Tips

    1/21

    VISUAL BASICPROGRAMMERSTIPS

    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 integratedcircuits, and then known as firmware, or more commonly, on some type of media, such as ahard drive, and known as software. At the very basic level, the microprocessor, the brain of thecomputer, converts the instructions into electrical signals that perform operations such as add-ing two numbers or moving data around in the computers memory. A computers electronicsare composed of millions of transistors that act like tiny electronic switches. They can either beon or off, represented by binary digits (1s and 0s). To program the earliest computers, in the1940s and 50s, programmers had to understand how the computer interpreted differentcombinations of 1s and 0s. These programmers wrote all of their programs using binarydigits. As programs became larger, programmers found it impractical to work in terms of thecomputers 1s and 0s. Instead, programming languageswere created that let programmersexpress computer instructions in a form more meaningful to the programmer. This form iscalled 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), asecond program, called a compiler, converts the programming language instructions into the 1sand 0s (called machine code) understood by the computer. The files on your disk with the EXEand COM extensions contain the machine code (1s and 0s) the computer will execute. Figure1 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 aprogramming language. Visual Basic is only one of many programming languages. Some pro-grammers use programming languages such as C, C++, Pascal, and FORTRAN. Differentprogramming languages provide unique features and have their own strengths.. Programminglanguages simplify the definition of the instructions you want the computer to execute.

    1 AN INTRODUCTIONTOPROGRAMMING

    Re s o lu tio n 3/113

  • 7/31/2019 Visual Basic Tips

    2/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

    File Extension File Type

    .frm Aform provides your programs visual means of represent-ing data.

    .bas A code module stores related pieces of code that do notrequire a form.

    .cls Aclass moduleis a special type of code module which con-tains a single class and its associated objects.

    .ctl A user controlallows you to add your own custom controlsto your Visual Basic program.

    .pag A property page enables you to add the ability to view anobjects properties in your program.

    .res A resource file holds the programs bitmaps, text strings,and other presentation-oriented information.

    Table 2The file types used by Visual Basic to store information.

    When writing programs with a user interface, the heart of your program will be the VisualBasic form. Adding visual controls and attaching programming code to them, you can quicklybuild 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 theforms, modules, and resource file used by your project. The Project File contains no instruc-tions of its own, but merely advises Visual Basics built-in editor and compiler of the files thatmake up your project.

    For example, if you create a Visual Basic program that performs payroll operations, you mightuse three forms and a code module. You might name the code module Payroll.bas, and withinit, place mathematical and accounting routines used throughout the program. You mightname the form modules Parent.frm, Entry.frm, and Ledger.frm. You could design each form toprovide the user with an interface to a separate type of financial data. You would save all ofthese files together in the Payroll.vbp project. Figure 2 shows the files and their interaction.

    2 HOWVISUAL BASICSTORESSOURCEFILES

    vb_025.p65 3/15/01, 5:08 PM14

  • 7/31/2019 Visual Basic Tips

    3/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    computations

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

    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 withinyour forms, code modules, class modules, and resource files into 1s and 0s. To do this, you runthe Visual Basic compiler, which converts the human-readable code youve written inVisualBasic to binary, machine-readable code. There are several options for running the compilerprogram, including; compiling as you go, executing with full compilation, and creating astandalone 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.exeoption. VisualBasic 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 nameof 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 Basicprogram. By Tip 44, you will create your first executable Visual Basic program.

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

    3 CREATINGANEXECUTABLE PROGRAM

    4 GETTINGSTARTEDWITHTHENEWPROJECTDIALOGBOX

    vb_025.p65 3/15/01, 5:08 PM15

  • 7/31/2019 Visual Basic Tips

    4/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

    Figure 4The New Project dialog box.

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

    Figure 5Selecting the VB Application Wizard icon.

    5 USINGTHENEWAPPLICATIONWIZARD

    vb_025.p65 3/15/01, 5:08 PM16

  • 7/31/2019 Visual Basic Tips

    5/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

    Figure 6The Multiple Document Interface in Microsoft Word.

    Single Document Interface (SDI) enabled applications are more like Web pages where movingfrom one to another means closing the preceding view. Single Document Interfaces are themost simple programs, and you will use them to discover more about Visual Basic.

    To begin creating a new project framework, which you will use for the next 11 Tips, double-click 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 thepurpose of the Application Wizard. Figure 7 shows the Application WizardIntroductiondialog box.

    Notice in Figure 7 that you can choose to load a saved profile. If this is your first time, therewill 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.

    6 UNDERSTANDING INTERFACETYPES

    USINGTHEVB APPLICATIONWIZARDTO

    CREATEA PROJECT FRAMEWORK7

    vb_025.p65 3/15/01, 5:08 PM17

  • 7/31/2019 Visual Basic Tips

    6/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    Figure 7The VB Application WizardIntroduction dialog box.

    There are three options within the Application WizardInterface Type dialog box: MultipleDocument Interface (MDI), Single Document Interface (SDI), and Explorer Style. You cansee a sample of each type of interface as you select each type. Watch as the graphic and descrip-tion 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 thistype of tree-structured interface when you use the Windows Explorer to access files on your harddrive. The directories and files are represented in a tree, with branches that expand showing filesand 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 WizardInterface Type dialog box with the SDI application radio button selected.

    Figure 8Selecting a Single Document Interface.

    Select an Interface and enter the name of your software application in the text box. Programscan also be called applications. To continue on with the Wizard, click the Next button at the

    8 SELECTINGANINTERFACETYPEINVB APPLICATIONWIZARD

    vb_025.p65 3/15/01, 5:08 PM18

  • 7/31/2019 Visual Basic Tips

    7/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

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

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

    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 foryou. 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 havecode associated with the selection, so they dont do anything until you add the instructions to tellthem what to do when selected. In other words, the Wizard will provide the code to display themenus. You, in turn, must write the code the application performs when the user selects a menuoption. Again, Visual Basic attempts to speed the initial stages of your development process byomitting 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 yourproject 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 boxesnext to the menu names shown checked in Figure 10. Clicking the Reset button will recheckthe default selections.

    When youre ready, click the Next button of the Application WizardMenus dialog box. Vi-sual Basic will display the Application WizardCustomize Toolbar

    9 UNDERSTANDING RADIO BUTTONS

    10 SELECTING MENUSINAPPLICATIONWIZARD

    vb_025.p65 3/15/01, 5:08 PM19

  • 7/31/2019 Visual Basic Tips

    8/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

    Toolbars are the rows of icons, normally found located beneath the menus of many Windowsapplications, 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 applicationvery 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 anicon to the toolbar, click once on it with your mouse, highlighting it, then click the arrow point-ing to the right (toward the window on the right). The icon will be moved to the window on theright, which includes the icons found in the toolbar, and the icon will no longer appear in thewindow 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 arrowpointing to the left. You will see that the selected icon is removed both from the list on theright as well as the toolbar. Clicking the Reset button will return the toolbar to a defaultsetting. When you have finished customizing the toolbar click the Next button. Visual Basicwill move to the Application Wizard Resources dialog.

    11 USINGTHEAPPLICATIONWIZARDTOCREATEA TOOLBAR

    vb_025.p65 3/15/01, 5:08 PM20

  • 7/31/2019 Visual Basic Tips

    9/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    The Application WizardResources dialog box lets you add a resource file to your Projectframework. 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 programsin different languages. Resource files are also useful if you use a large number of strings of textwithin your project, or you use some the save text repetitively. You will use the Resource File inlater tips. Select the No radio button on the Application WizardResources dialog box. Figure11 shows the Application WizardResources dialog box.

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

    Figure 12The Application WizardResources dialog box with no resource file selected.

    Click your mouse on the Next button on the Application WizardResources dialog box. VisualBasic will display the Application WizardInternet Connectivity dialog box.

    The Application WizardInternet Connectivity dialog box lets you add a custom Web browser toyour Project framework. Included in the new Visual Basic 6.0 set of Common Controls are a set ofcontrolsthat provide the programmer with an easy means of enabling programmatic access to theInternet. These controls include a simple browser control. If you develop an application to distrib-

    ute to your customers or other third party users, or you develop shareware and want people toregister the program online, the browser inclusion feature is helpful. These are only two suggesteduses for the browser featuresthere are an infinite number of ways to use a browser within aprogram. 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. Fig-ure 13 shows the Application WizardInternet Connectivity dialog box.

    12 USINGTHEAPPLICATIONWIZARDTOADDRESOURCEFILES

    13 USINGTHEWIZARDTOADDANINTERNETCONNECTION

    vb_025.p65 3/15/01, 5:08 PM21

  • 7/31/2019 Visual Basic Tips

    10/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    Click your mouse on the Next button within the Application WizardInternet Connectivitydialog box. The VB Application Wizard responds with a dialog box entitled Application Wiz-ardStandard Forms.

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

    In Tip 13, you learned that Visual Basic 6.0 includes a set of controls to assist the user inreaching the Internet. Controls are tools provided to programmers to assist in the creation ofprograms. Visual Basic comes with many standard controls: command buttons, labels, radiobuttons, and more.

    Together with forms, controls are at the root of everything you will do in Visual Basic. Simplyput, a control is the means by which your programs will communicate with users. Sometimes,

    controls work in the background, like the ImageListcontrol, introduced in Tip 496. Moreoften, controls will have a visual manifestation, like menus, toolbars, radio buttons, and com-mand buttons. However, all controls have functionality that makes them important in pro-gram 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 Dialogcontrol. Further, in Tips 804-806, you will learn about more complex ActiveX controls. Figure14 shows the interaction of controls with your program and your users.

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

    14 UNDERSTANDING CONTROLS

    vb_025.p65 3/15/01, 5:08 PM22

  • 7/31/2019 Visual Basic Tips

    11/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    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 aboutbox. 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 splashscreen will contain some type of corporate graphic, a version number, and the programs regis-tered user. You may include any information you desire on the splash screen, but remember itonly loads briefly. You will learn more about the strategic use of splash screens in Tip 921.

    The login dialogcan be used to protect access to your application or to data accessible throughthe 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, itoffers 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 aboutbox 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 theform of a command button. You will not add any form templates at this point. You will learnabout form templates in Tips 38-39.

    You will not include any of these options within your project. If any of these options areselected, deselect them by clicking the left mouse button on each checkmark until it disap-pears. Figure 15 shows the Application WizardStandard Forms dialog box.

    Figure 15The 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.

    15 INCLUDING STANDARD FORMS

    vb_025.p65 3/15/01, 5:08 PM23

  • 7/31/2019 Visual Basic Tips

    12/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    The Application WizardData Access Forms dialog box helps you generate basic forms froma database for use within your application. As your Visual Basic applications become morecomplex, your applications may need to interact with a database. In later Tips, you will learnhow to associate your applications with most commonly used databases. You can create simpledatabase access forms within the VB Application Wizard.

    While the VB Application Wizard is helpful for creating these simple forms, you will eventu-ally discover that the majority of your database applications require custom form design. How-ever, the Wizard-designed forms are useful both for understanding usage and structure of thedatabase 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 accessform 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-gener-ated data access form would not be sufficient for your entire programbut it might provide avaluable template to use in creating that program.

    Do not select any Databases and deselect any Databases that are currently selected on theApplication WizardData Access Forms dialog box. Figure 16 shows the Application Wiz-ardData Access Forms dialog box.

    Figure 16The Application WizardData Access Forms dialog box with no database forms selected.

    Click your mouse on Next in the Application WizardData Access Forms dialog box. VisualBasic then displays the Application WizardFinished! dialog box.

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

    16 CONNECTINGTODATABASESUSINGTHEWIZARD

    FINISHINGTHECREATIONOFYOURNEWWIZARD

    GENERATED PROJECT17

    vb_025.p65 3/15/01, 5:08 PM24

  • 7/31/2019 Visual Basic Tips

    13/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    The Application WizardFinished! dialog box also asks if it should display a summary of whatsteps its has performed in designing your project framework. Click the View Reportcommandbutton. The VB Application Wizard then displays a brief summary of what it has created (in-cluding any menus, data control objects, internet controls, standard forms, and more) and whatsteps 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 futureprojects. 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 17The 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 willalert you that it has created the application. Click your mouse on the OK button in theApplication Created dialog box.

    The VB Application Wizard has used the selections you made in Tips 7-16 to create a projectframework. The VB Application Wizard presents you with some brief notes on what actions it hasperformed while assisting in the design of your project. These notes may vary from use to use ofthe 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 withstarting points to make your customized changes. Additionally, the summary report alerts youto 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 Sum-mary Report. To close the Summary Report, click your mouse on the Close button. Figure 18shows the Application Wizard Summary Report.

    18 READINGTHEVB APPLICATIONWIZARDSSUMMARY REPORT

    vb_025.p65 3/15/01, 5:08 PM25

  • 7/31/2019 Visual Basic Tips

    14/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

    In Tip 18, you completed the creation of your first Visual Basic project. However, unless yousave the information somewhere within that Project, the information and the project are lostwhen 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 SaveAs dialog box will display the current directory as the VBdirectory under the Microsoft VisualStudio 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 Studiodirectory. You will find a directory named MyProjectsdisplayed within this directory. MyProjectsfolder 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-clickyour mouse on this directory to select it. Save each of the files (one form, one module, and oneproject) within this newly created TipExam1 directory using the default names provided toyou by Visual Basic. You can check your work within the Windows Explorer. Figure 19 showsthe Windows Explorer view of the TipExam1 directory.

    Note:TipExam1 directory contains four files, not the three you might expect, since you saved onlythree. 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.

    19 SAVINGYOURNEWWIZARD-GENERATED PROJECT

    vb_025.p65 3/15/01, 5:08 PM26

  • 7/31/2019 Visual Basic Tips

    15/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    Figure 19TheTipExam1 sub-directory of theMyProjectsdirectory.

    The VB Application Wizard creates a file that is fully executable, without you entering a singleline of code. However, just because the file isfully executabledoes not mean it is fully functional.The code you generate with the VB Application Wizard is generally insufficient to accomplishany tasks, with the exception of code you generate to access simple database information. TheWizard is not designed to do your work for you. Rather, the VB Application Wizard expeditessome of the more dreary setup work so that you can focus on the important issueslike mak-ing your applications work!

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

    CREATING FULLY EXECUTABLE FILESWITHOUT

    ENTERING ANYCODE20

    21 UNDERSTANDINGTHEEDITINGWINDOW

    vb_025.p65 3/15/01, 5:08 PM27

  • 7/31/2019 Visual Basic Tips

    16/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    Figure 21 The Visual Basic Editing Window.

    The Editing Window contains the following elements, as indicated on Figure 21, which youwill 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.

    The Toolbox is the first of the elements that comprise the Editing Window introduced in Tip22. By default, Visual Basic places the Toolbox on the left side of the editing window. Whenyou first start Visual Basic, the Toolbox will contain icons that represent the most commonlyused controls.

    If the Toolbox is not visible, select the View menu Toolbox option, and the Toolbox will becomevisible. If the Toolbox is not on the left side of the Editing Window, click your right-mouse buttononce 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 oneof the edges of your screen, at which point you may drag-and-drop it until the Toolbox is posi-tioned correctly on the left side of the Editing Window.

    22 UNDERSTANDINGTHETOOLBOX

    vb_025.p65 3/15/01, 5:09 PM28

  • 7/31/2019 Visual Basic Tips

    17/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    As you design the visual component of each project, you will select icons from the Toolbox andplace the associated controls upon the appropriate form. For example, the last icon in the first rowof the Toolbox is a capital A. I dont have the A in the same place, perhaps the order of the tools isnot the same in all computers. Click your mouse on the A icon. You will see that the A becomessunken. 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 mousesomewhere in the upper left-hand corner of the form, below the toolbar, and hold the buttondown. Drag the mouse toward the bottom of your screen and to the right. Visual Basic will drawthe outline of a box. After the box is about four grid spaces high and ten grid spaces wide, release themouse button. Visual Basic will draw a blank rectangle containing the text Label1. Figure 22 showsthe Toolbox and the Figure 22 shows the form frmMain with the newLabel1 drawn upon it.

    Figure 22TheToolboxwith theLabel control selected.

    Figure 22frmMain with theLabel1 control placed.

    vb_025.p65 3/15/01, 5:09 PM29

  • 7/31/2019 Visual Basic Tips

    18/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    In the previous Tip, you learned how to use the Toolbox to draw a label on a form. Labels are atype ofcontrol. The caption that appeared for the new label, Label1, is apropertyof that control.Properties are values used by every type of Visual Basic control, from forms to labels. Much asyou would describe a house as being painted blue, a property describes the control with whichit is associated. Most properties may be either read from or written to by the programmerwhen 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 Colorprop-erty; 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 execu-tion, as opposed to design time, which occurs prior to attempting to run the code. If you attemptto 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 theediting 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 Label1controls 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 TheLabel1 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 thespecifics of a form or control. For example, click twice with your mouse on the Caption prop-erty to select the entire block of text. Delete the current caption and change the label captionto Properties Example. Figure 23.2 and Figure 23.3 shows frmMain and the Properties Win-dow reflecting the changed caption.

    23 UNDERSTANDINGTHEPROPERTIESWINDOW

    vb_025.p65 3/15/01, 5:09 PM30

  • 7/31/2019 Visual Basic Tips

    19/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    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 programcode. Whenever you work with an object variable (Tip 91), you are setting its properties.

    The Properties Window is a dialog box which looks similar to the Windows Explorer. Like theWindows Explorer, this dialog box contains files and folders and lets you move through thecurrently 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 Codeicon, a View Object icon, and a folder icon. The View Code and View Object icons let youcontrol what you see of an objectits visual representation or the underlying code. If you wantto work on an objects visual design, select the object name from within the tree and click yourmouse on the View Object icon. If you want to see the code attached to a given object, selectthat object and click your mouse upon the View Code icon.

    24 UNDERSTANDINGTHE PROJECT EXPLORER

    vb_025.p65 3/15/01, 5:09 PM31

  • 7/31/2019 Visual Basic Tips

    20/21

    1001 VISUAL BASICPROGRAMMERSTIPS

    The folder icon is intended to help you better navigate the items within your project. If youclick your mouse on the folder icon, it toggles the treeview from multiple levels to just onecontaining all objects within the project.

    Effective use of the Project Explorer will decrease both your code editing time and your informa-

    tion retrieval time when developing applications. Figure 24 shows the Project Explorer window.

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

    With Visual Basic, you will use forms to design your programs visual interface. You will layoutforms within the Editing Window. Sometimes, you will design a form that appears differentlywithin the Editing Window than it does during program execution. The Form Layoutwindowhelps 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 displayset to different resolution levels than your display. The Form Layout window depicts threescreen resolutions 640x480, 800x600 and 1024x728. The largest resolution fills the window;the other resolutions are indicated within the Form Layout window using dashed and labeledlines. As you design more complex projects, you will learn methods of determining the userssystem settings and adjusting your programs displays appropriately.

    If you already have your forms laid out, or after you are comfortable with how your formstranslate between resolutions, you may want to close the Form Layout window to make moreroom for the Properties Window. Figure 25 shows the Form Layout window.

    25 UNDERSTANDINGTHEFORMLAYOUTWINDOW

    vb_025.p65 3/15/01, 5:09 PM32

  • 7/31/2019 Visual Basic Tips

    21/21

    1001 VISUAL BASICPROGRAMMERSTIPS

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

    The Editing Display window uses the remaining space within the Visual Basic Editing Win-dow. 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. TheToolbox, 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 26shows the Editing Display window with an open form and a code module tiled horizontally.

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

    26 UNDERSTANDINGTHEEDITING DISPLAYWINDOW