- Account
- Join for Free
- Sign In
- Help & Info
- Privacy Notice
- DMCA
- Contact Us
- Terms Of Use
...Description...... more. less.
a rectangular region with its own boundaries. You are probably already aware of several different types of windows: an Explorer window in Windows 95, a document window within your word processing program, or a dialog box that pops up to remind you of an appointment. While these are the most common examples, there are actually many other types of windows.<br><br> A command button is a window. Icons, text boxes, option buttons and menu bars are all windows. The Microsoft Windows operating system manages all of these many windows by assigning each one a unique id number (window handle or hWnd).<br><br> The system continually monitors each of these windows for signs of activity or events. Events can occur through user actions such as a mouse click or a key press, through programmatic control, or even as a result of another window's actions. Each time an event occurs, it causes a message to be sent to the operating system.<br><br> The system processes the message and broadcasts it to the other windows. Each window can then take the appropriate action based on its own instructions for dealing with that particular message (for example, repainting itself when it has been uncovered by another window). As you might imagine, dealing with all of the possible combinations of windows, events and messages could be mind-boggling.<br><br> Fortunately, Visual Basic insulates you from having to deal with all of the low-level message handling. Many of the messages are handled automatically by Visual Basic; others are exposed as Event procedures for your convenience. This allows you to quickly create powerful applications without having to deal with unnecessary details.<br><br> Understanding the Event-Driven Model In traditional or "procedural" applications, the application itself controls which portions of code execute and in what sequence. Execution starts with the first line of code and follows a predefined path through the application, calling procedures as needed. In an event-driven application, the code doesn't follow a predetermined path 4 it executes different code sections in response to events.<br><br> Events can be triggered by the user's actions, by messages from the system or other applications, or even from the application itself. The sequence of these events determines the sequence in which the code executes, thus the path through the application's code differs each time the program runs. Because you can't predict the sequence of events, your code must make certain assumptions about the "state of the world" when it executes.<br><br> When you make assumptions (for example, that an entry field must contain a value before running a procedure to process that value), you should structure your application in such a way as to make sure that the assumption will always be valid (for example, disabling the command button that starts the procedure until the entry field contains a value). Your code can also trigger events during execution. For example, programmatically changing the text in a text box cause the text box's Change event to occur.<br><br> This would cause the code (if any) contained in the Change event to execute. If you assumed that this event would only be triggered by user interaction, you might see unexpected results. It is for this reason that it is important to understand the event-driven model and keep it in mind when designing your application.<br><br> 3 3 3 Interactive Development The traditional application development process can be broken into three distinct steps: writing, compiling, and testing code. Unlike traditional languages, Visual Basic uses an interactive approach to development, blurring the distinction between the three steps. With most languages, if you make a mistake in writing your code, the error is caught by the compiler when you start to compile your application.<br><br> You must then find and fix the error and begin the compile cycle again, repeating the process for each error found. Visual Basic interprets your code as you enter it, catching and highlighting most syntax or spelling errors on the fly. It's almost like having an expert watching over your shoulder as you enter your code.<br><br> In addition to catching errors on the fly, Visual Basic also partially compiles the code as it is entered. When you are ready to run and test your application, there is only a brief delay to finish compiling. If the compiler finds an error, it is highlighted in your code.<br><br> You can fix the error and continue compiling without having to start over. Because of the interactive nature of Visual Basic, you'll find yourself running your application frequently as you develop it. This way you can test the effects of your code as you work rather than waiting to compile later.<br><br> 3 4 3 2. Integrated Development Environment Elements The Visual Basic integrated development environment (IDE) consists of the following elements. Menu Bar Displays the commands you use to work with Visual Basic.<br><br> Besides the standard File, Edit, View, Window, and Help menus, menus are provided to access functions specific to programming such as Project, Format, or Debug. Context Menus Contain shortcuts to frequently performed actions. To open a context menu, click the right mouse button on the object you're using.<br><br> The specific list of shortcuts available from context menus depends on the part of the environment where you click the right mouse button. For example, the context menu displayed when you right click on the Toolbox lets you display the Components dialog box, hide the Toolbox, dock or undock the Toolbox, or add a custom tab to the Toolbox. Toolbars Provide quick access to commonly used commands in the programming environment.<br><br> You click a button on the toolbar once to carry out the action represented by that button. By default, the Standard toolbar is displayed when you start Visual Basic. Additional toolbars for editing, form design, and debugging can be toggled on or off from the Toolbars command on the View menu.<br><br> Toolbars can be docked beneath the menu bar or can "float" if you select the vertical bar on the left edge and drag it away from the menu bar. Toolbox Provides a set of tools that you use at design time to place controls on a form. In addition to the default toolbox layout, you can create your own custom layouts by selecting Add Tab from the context menu and adding controls to the resulting tab.<br><br> Project Explorer Window Lists the forms and modules in your current project. A project is the collection of files you use to build an application. Properties Window Lists the property settings for the selected form or control.<br><br> A property is a characteristic of an object, such as size, caption, or color. Object Browser Lists objects available for use in your project and gives you a quick way to navigate through your code. You can use the Object Browser to explore objects in Visual Basic and other applications, see what methods and properties are available for those objects, and paste code procedures into your application.<br><br> Form Designer Serves as a window that you customize to design the interface of your application. You add controls, graphics, and pictures to a form to create the look you want. Each form in your application has its own form designer window.<br><br> 3 5 3 Code Editor Window Serves as an editor for entering application code. A separate code editor window is created for each form or code module in your application. Form Layout Window The Form Layout window allows you to position the forms in your application using a small graphical representation of the screen.<br><br> Immediate, Locals, and Watch Windows These additional windows are provided for use in debugging your application. They are only available when you are running your application within the IDE. 3 6 3 3.<br><br> The Structure of a Visual Basic Project The following sections describe the different types of files and objects that you can include in a project. Form Modules Form modules (.frm file name extension) can contain textual descriptions of the form and its controls, including their property settings. They can also contain form-level declarations of constants, variables, and external procedures; event procedures; and general procedures.<br><br> Class Modules Class modules (.cls file name extension) are similar to form modules, except that they have no visible user interface. You can use class modules to create your own objects, including code for methods and properties. Standard Modules Standard modules (.bas file name extension) can contain public or module-level declarations of types, constants, variables, external procedures, and public procedures.<br><br> Resource Files Resource files (.res file name extension) contain bitmaps, text strings, and other data that you can change without having to re-edit your code. For example, if you plan to localize your application in a foreign language, you can keep all of the user-interface text strings and bitmaps in a resource file, which you can then localize instead of the entire application. A project can contain no more than one resource file.<br><br> ActiveX Documents ActiveX documents (.dob) are similar to forms, but are displayable in an Internet browser such as Internet Explorer. The Professional and Enterprise editions of Visual Basic are capable of creating ActiveX documents. User Control and Property Page Modules User Control (.ctl) and Property Page (.pag) modules are also similar to forms, but are used to create ActiveX controls and their associated property pages for displaying design-time properties.<br><br> The Professional and Enterprise editions of Visual Basic are capable of creating ActiveX controls. Components In addition to files and modules, several other types of components can be added to the project. ActiveX Controls ActiveX controls (.ocx file name extension) are optional controls which can be added to the toolbox and used on forms.<br><br> When you install Visual Basic, the files containing the controls included with Visual Basic are copied to a common directory (the \Windows\System subdirectory under Windows 95). Additional ActiveX controls are available from a wide variety of sources. You can also create your own controls using the Professional or Enterprise editions of Visual Basic.<br><br> 3 7 3 Insertable Objects Insertable objects , such as a Microsoft Excel Worksheet object, are components you can use as building blocks to build integrated solutions. An integrated solution can contain data in different formats, such as spreadsheets, bitmaps, and text, which were all created by different applications. References You can also add references to external ActiveX components that may be used by your application.<br><br> You assign references by using the References dialog, accessed from the References menu item on the Project menu. ActiveX Designers ActiveX designers are tools for designing classes from which objects can be created. The design interface for forms is the default designer.<br><br> Additional designers may be available from other sources. Standard Controls Standard controls are supplied by Visual Basic. Standard controls, such as the command button or frame control, are always included in the toolbox, unlike ActiveX controls and insertable objects, which can be removed from or added to the toolbox.<br><br> 3 8 3 4. Creating an Application in Visual Basic There are three main steps to creating an application in Visual Basic: 1. Create the interface.<br><br> 2. Set properties. 3.<br><br> Write code. Creating the Interface Forms are the foundation for creating the interface of an application. You can use forms to add windows and dialog boxes to your application.<br><br> You can also use them as containers for items that are not a visible part of the application's interface. For example, you might have a form in your application that serves as a container for graphics that you plan to display in other forms. The first step in building a Visual Basic application is to create the forms that will be the basis for your application's interface.<br><br> Then you draw the objects that make up the interface on the forms you create. Resizing, Moving, and Locking Controls Notice that small rectangular boxes called sizing handles appear at the corners of the control; you'll use these sizing handles in the next step as you resize the control. You can also use the mouse, keyboard, and menu commands to move controls, lock and unlock control positions, and adjust their positions.<br><br> Setting Properties The next step is to set properties for the objects you've created. The Properties window provides an easy way to set properties for all objects on a form. To open the Properties window, choose the Properties Window command from the View menu, click the Properties Window button on the toolbar, or use the context menu for the control.<br><br> Setting the Icon Property All forms in Visual Basic have a generic, default icon that appears when you minimize that form. However, you will probably change this icon to one that illustrates the use of the form or your application. To assign an icon to a form, set the Icon property for that form.<br><br> You can use 32 x 32 pixel icons that were standard in 16-bit versions of Microsoft Windows and are also used in Windows 95 and Windows NT, as well as the 16 x 16 pixel icons used in Windows 95. Writing Code The Code Editor window is where you write Visual Basic code for your application. Code consists of language statements, constants, and declarations.<br><br> Using the Code Editor window, you can quickly view and edit any of the code in your application. Creating Event Procedures Code in a Visual Basic application is divided into smaller blocks called procedures . An event procedure , contains code that is executed when an event occurs (such as when a user clicks a button).<br><br> An event procedure for a control combines the control's actual name (specified in the Name property), an underscore (_), and the event name. For example, if you want a command button named Command1 to invoke an event procedure when it is clicked, use the procedure Command1_Click. 3 9 3 For example, Private Sub Command1_Click () Text1.Text = "Hello, world!" End Sub You'll note here that the code is simply changing the Text property of the control named Text1 to read "Hello, world!" The syntax for this example takes the form of object.property, where Text1 is the object and Text is the property.<br><br> You can use this syntax to change property settings for any form or control in response to events that occur while your application is running. Running the Application To run the application, choose Start from the Run menu, or click the Start button on the toolbar, or press F5 . Click the command button you've created on the form, and you'll see "Hello, world!" displayed in the text box.<br><br> 3 10 3 5. Forms, Controls, and Menus The first step to creating an application with Visual Basic is to create the interface, the visual part of the application with which the user will interact. Forms and controls are the basic building blocks used to create the interface; they are the objects that you will work with to build your application.<br><br> Form objects are the basic building blocks of a Visual Basic application, the actual windows with which a user interacts when they run the application. Forms have their own properties, events, and methods with which you can control their appearance and behavior. By setting the properties of the form and writing Visual Basic code to respond to its events, you customize the object to meet the requirements of your application.<br><br> Controls are objects that are contained within form objects. Each type of control has its own set of properties, methods and events that make it suitable for a particular purpose. Some of the controls you can use in your applications are best suited for entering or displaying text.<br><br> Other controls let you access other applications and process data as if the remote application was part of your code. Setting Form Properties Many of a form's properties affect its physical appearance. The Caption property determines the text that is shown in the form's title bar; the Icon property sets the icon that is displayed when a form is minimized.<br><br> The MaxButton and MinButton properties determine whether the form can be maximized or minimized. By changing the BorderStyle property, you can control the resizing behavior of the form. Height and Width properties determine the initial size of a form; Left and Top properties determine the form's location in relation to the upper left-hand corner of the screen.<br><br> The WindowState property can be set to start the form in a maximized, minimized, or normal state. The Name property sets the name by which you will refer to the form in code. By default, when a form is first added to a project, its name is set to Form1, Form2, and so forth.<br><br> It's a good idea to set the Name property to something more meaningful, such as "frmEntry" for an order entry form. The best way to familiarize yourself with the many form properties is to experiment. Form Events and Methods As objects, forms can perform methods and respond to events.<br><br> The Resize event of a form is triggered whenever a form is resized, either by user interaction or through code. This allows you to perform actions such as moving or resizing controls on a form when its dimensions have changed. The Activate event occurs whenever a form becomes the active form; the Deactivate event occurs when another form or application becomes active.<br><br> These events are convenient for initializing or finalizing the form's behavior. For example, in the Activate event you might write code to highlight the text in a particular text box; in the Deactivate event you might save changes to a file or database. To make a form visible, you would invoke the Show method.<br><br> Invoking the Show method has the same effect as setting a form's Visible property to True. Many of a form's methods involve text or graphics. The Print, Line, Circle, and Refresh methods are useful for printing or drawing directly onto a form's surface.<br><br> These methods and more are discussed in "Working with Text and Graphics." Using Command Buttons Most Visual Basic applications have command buttons that allow the user to simply click them to perform actions. When the user chooses the button, it not only carries out the appropriate action, it 3 11 3 also looks as if it's being pushed in and released. Whenever the user clicks a button, the Click event procedure is invoked.<br><br> You place code in the Click event procedure to perform any action you choose. There are many ways to choose a command button at run time: " Use a mouse to click the button. " Move the focus to the button by pressing the TAB key, and then choose the button by pressing the SPACEBAR or ENTER .<br><br> (See "Understanding Focus" later in this chapter.) " Press an access key ( ALT + the underlined letter) for a command button. " Set the command button's Value property to True in code: cmdClose.Value = True " Invoke the command button's Click event in code: cmdClose_Click " If the command button is the default command button for the form, pressing ENTER chooses the button, even if you change the focus to a different control other than a command button. At design time, you specify a default command button by setting that button's Default property to True.<br><br> " If the command button is the default Cancel button for the form, then pressing ESC chooses the button, even if you change the focus to another control. At design time, you specify a default Cancel button by setting that button's Cancel property to True. All these actions cause Visual Basic to invoke the Click event procedure.<br><br> 3 12 3 6. UI Controls Label and text box controls are used to display or enter text. Use labels when you want your application to display text on a form, and text boxes when you want to allow the user to enter text.<br><br> Labels contain text that can only be read, while text boxes contain text that can be edited. " Text box: Text that can be edited by the user, for example an order entry field or a password box " Label: Text that is displayed only, for example to identify a field on a form or display instructions to the user Most applications need to present choices to their users, ranging from a simple yes/no option to selecting from a list containing hundreds of possibilities. Visual Basic includes several standard controls that are useful for presenting choices.<br><br> The following table summarizes these controls and their appropriate uses. " Check boxes: A small set of choices from which a user can choose one or more options. " Option buttons (use frames if additional groups are needed): A small set of options from which a user can choose just one.<br><br> " List box: A scrollable list of choices from which the user can choose. " Combo box: A scrollable list of choices along with a text edit field. The user can either choose from the list or type a choice in the edit field.<br><br> Because Windows is a graphical user interface, it's important to have a way to display graphical images in your application's interface. Visual Basic includes four controls that make it easy to work with graphics: the picture box control, the image control, the shape control, and the line control. The image, shape and line controls are sometimes referred to as "lightweight" graphical controls.<br><br> They require less system resources and consequently display somewhat faster than the picture box control; they contain a subset of the properties, methods and events available in the picture box. Each is best suited for a particular purpose. " Picture box: A container for other controls, for displaying a picture, for graphics methods.<br><br> " Image control: Displaying a picture " Shape control: Displaying a simple graphical element " Line control: Displaying a simple graphical element 3 13 3 7. Menu Basics If you want your application to provide a set of commands to users, menus offer a convenient and consistent way to group commands and an easy way for users to access them. The menu bar appears immediately below the title bar on the form and contains one or more menu titles .<br><br> When you click a menu title (such as File), a menu containing a list of menu items drops down. Menu items can include commands (such as New and Exit), separator bars, and submenu titles. Each menu item the user sees corresponds to a menu control you define in the Menu Editor (described later in this chapter).<br><br> Some menu items perform an action directly; for example, the Exit menu item on the File menu closes the application. Other menu items display a dialog box 4 a window that requires the user to supply information needed by the application to perform the action. These menu items should be followed by an ellipsis (&).<br><br> For example, when you choose Save As& from the File menu, the Save File As dialog box appears. A menu control is an object; like other objects it has properties that can be used to define its appearance and behavior. You can set the Caption property, the Enabled and Visible properties, the Checked property, and others at design time or at run time.<br><br> Menu controls contain only one event, the Click event, which is invoked when the menu control is selected with the mouse or using the keyboard. Pop-up Menus A pop-up menu is a floating menu that is displayed over a form, independent of the menu bar. The items displayed on the pop-up menu depend on the location of the pointer when the right mouse button is pressed; therefore, pop-up menus are also called context menus .<br><br> (In Windows 95, you activate context menus by clicking the right mouse button.) You should use pop-up menus to provide an efficient method for accessing common, contextual commands. Using the Menu Editor With the Menu Editor, you can add new commands to existing menus, replace existing menu commands with your own commands, create new menus and menu bars, and change and delete existing menus and menu bars. The main advantage of the Menu Editor is its ease of use.<br><br> You can customize menus in a completely interactive manner that involves very little programming. To display the Menu Editor " From the Tools menu, choose Menu Editor . 3 14 3 8.<br><br> Dialog Boxes In Windows-based applications, dialog boxes are used to prompt the user for data needed by the application to continue or to display information to the user. Dialog boxes are a specialized type of form object that can be created in one of three ways: " Predefined dialog boxes can be created from code using the MsgBox or InputBox functions. " Customized dialog boxes can be created using a standard form or by customizing an existing dialog box.<br><br> " Standard dialog boxes, such as Print and File Open, can be created using the common dialog control. A dialog is displayed when you invoke the MsgBox function in code. You supply three pieces of information, or arguments, to the MsgBox function: the message text, a constant (numeric value) to determine the style of the dialog box, and a title.<br><br> Styles are available with various combinations of buttons and icons to make creating dialog boxes easy. Because most dialog boxes require user interaction, they are usually displayed as modal dialog boxes. A modal dialog box must be closed (hidden or unloaded) before you can continue working with the rest of the application.<br><br> For example, a dialog box is modal if it requires you to click OK or Cancel before you can switch to another form or dialog box. Modeless dialog boxes let you shift the focus between the dialog box and another form without having to close the dialog box. You can continue to work elsewhere in the current application while the dialog box is displayed.<br><br> Modeless dialog boxes are rare; you will usually display a dialog because a response is needed before the application can continue. From the Edit menu, the Find dialog box in Visual Basic is an example of a modeless dialog box. Use modeless dialog boxes to display frequently used commands or information.<br><br> 3 15 3 9. Online Help No matter how great your user interface, there will be times that a user needs assistance. The user assistance model for your application includes such things as online Help and printed documentation; it may also contain user assistance devices such as ToolTips, Status Bars, What's This help, and Wizards.<br><br> For online Help use Help Workshop. This is a program supplied with Visual C++ that you use to create Help (.hlp) files, edit project and contents files, and test and report on help files. Help Workshop takes the information in the project (.hpj) file to combine the topic (.rtf) files, bitmaps, and other sources into one Help file that can be viewed using the Microsoft Windows Help program.<br><br> Help and Documentation Online Help is an important part of any application 4 it's usually the first place a user will look when they have a question. Even a simple application should provide Help; failing to provide it is like assuming that your users will never have questions. In designing your Help system, keep in mind that its primary purpose is to answer questions.<br><br> Try to think in terms of the user when creating topic names and index entries; for example, "How do I format a page?" rather than "Edit, Page Format menu" will make your topic easier to locate. Don't forget about context sensitivity; it's frustrating to most users if they press the F1 key for help on a specific field and find themselves at the Contents topic. Conceptual documentation, whether printed and/or provided on compact disc, is helpful for all but the simplest applications.<br><br> It can provide information that may be difficult to convey in the shorter Help topic. At the very least, you should provide documentation in the form of a ReadMe file that the user can print if desired. User Assistance Devices Within the user interface, there are several techniques for providing assistance to the user.<br><br> Visual Basic makes it easy to add ToolTips, What's This help, Status displays, and Wizards to your applications. It's up to you to decide which of these devices are appropriate for your application. ToolTips ToolTips are a great way to display information to the user as they navigate the user interface.<br><br> A ToolTip is a small label that is displayed when the mouse pointer is held over a control for a set length of time, usually containing a description of the control's function. Normally used in conjunction with toolbars, ToolTips also work well in most any part of the interface. Most Visual Basic controls contain a single property for displaying ToolTips: ToolTipText.<br><br> The following code would implement a ToolTip for a command button named cmdPrint: cmdPrint.ToolTipText = "Prints the current document" As with other parts of the interface, make sure that the text clearly conveys the intended message to the user. What's This Help What's this Help provides a link to a pop-up Help topic when the user selects What's This Help and clicks the What's This cursor on a control. What's This Help can be initiated from a toolbar button, a menu item, or a button on the title bar of a dialog box.<br><br> 3 16 3 Status Displays A status display can also be used to provide user assistance in much the same way as a ToolTip. Status displays are a good way to provide instructions or messages that may not fit easily into a ToolTip. The status bar control included in the Professional and Enterprise editions of Visual Basic works well for displaying messages; a label control can also be used as a status display.<br><br> The text displayed in a status display can be updated in one of two ways: in the GotFocus event of a control or form, or in the MouseMove event. If you want to use the display as a learning device, add an item to the Help menu to toggle its Visible property on and off. Wizards A wizard is a user assistance device that takes the user step by step through a procedure, working with the user's actual data.<br><br> Wizards are usually used to provide task-specific assistance. They help a user accomplish a task that would otherwise require a considerable (and undesirable) learning curve; they provide expert information to a user that has not yet become an expert. The Professional and Enterprise editions of Visual Basic include the Wizard Manager, a tool for creating wizards.<br><br> 3 17 3 10. Distributing Your Applications Once you 9ve created an application that you would like to distribute to others, you need to create a setup program to properly install your application. Visual Basic 9s Setup Wizard does most of this work for you.<br><br> It can determine which files you 9ll need to distribute, determine where they 9ll need to be installed on the user 9s machine, enter them in the system registry, and create a Program Group and Start Menu links in Windows. You need to decide which media you want to use to distribute your application. For example, you can create disks or install your application to a network server or on the Internet.<br><br> In most cases, the Setup Wizard can handle everything you 9ll need for your setup program. If, however, you want to customize your setup program to meet some special need or functionality, you can use the Setup Toolkit to create a modified or enhanced setup program. The Setup Wizard vs.<br><br> The Setup Toolkit The Setup Wizard is, essentially, a helper program that walks you through the steps necessary to create a professional setup program for your Visual Basic application. The setup program that the wizard creates for you is compiled from the Visual Basic Setup Toolkit project, which resides in the \Setupkit\Setup1 directory of your Visual Basic installation. Like any other Visual Basic project, the forms, code, and functionality of the setup project can be modified or enhanced.<br><br> In most cases, the Setup Wizard is the best way to create a setup for your application. If, however, you want to write a setup program that has features and functionality not provided by the Setup Wizard, you can do so by modifying the Setup Toolkit project. Caution If you determine that you need to modify the Setup Toolkit project to add some functionality not already provided, make sure that you copy the Setup Toolkit project into a new directory to serve as a backup before changing the source code in the Setup1 directory.<br><br> Any modifications you make to the Setup Toolkit project will affect any subsequent setup programs created with the Setup Wizard.