Contents 1. Introduction of Language and Environment Preview . I-I Objectives . I-I What is Visual Basic? . 1-2 . Visual Basic 6.0 versus Other Versions of Visual Basic . 1-3 16 Bits versus 32 Bits . 1-3 Structure of a Visual Basic Application . 1-4 Steps in Developing Application . 1-4 Drawing the User Interface and Setting Properties . 1-5 Example 1-1: Stopwatch Application - Drawing Controls . 1-9 Setting Properties of Objects at Design Time . 1-10 Setting Properties at Run Time . 1-11 How Names Are Used in Object Events . 1-11 Example 1-2: Stopwatch Application - Setting Properties . 1-12 Variables . 1-14 Visual Basic Data Types . 1-14 V mabie Declaration . 1-14 Example 1-3: Stopwatch Application -Attaching Code . 1-18 Quick Primer on Saving Visual Basic Applications . 1-20 Exercise 1: Calendar/Time Display . 1-21 .
1-1 1 Language and Environment Preview • In this section, we will do a quick overview of how to build an application in Visual Basic. You'll learn a new vocabulary, a new approach to programming, and ways to move around in the Visual Basic environment. You will leave having written your first Visual Basic program. Objectives => Understand the benefits of using Microsoft Visual Basic 6.0 for Windows as an application tool => Understand the Visual Basic event-driven programming concepts, terminology, and available tools => Learn the fundamentals of designing, implementing, and distributing a Visual Basic application => Learn to use the Visual Basic toolbox => Learn to modify object properties => Learn object methods => Use the menu design window => Understand proper debugging and error-handling procedures => Gain a basic understanding of database access and management using databound controls => Obtain an introduction to ActiveX controls and the Windows Application Programming Interface (API) .
2-1 2 Visual Basic 6.0 Language Review and Preview • Last chapter, we found there were three primary steps involved in developing an application using Visual Basic: 1. Draw the user interface 2. Assign properties to controls 3. Attach code to events This week, we are primarily concerned with Step 3, attaching code. We will become more familiar with moving around in the Code window and learn some of the elements of the Basic language. A Brief History of Basic • Language developed in early 1960's at Dartmouth College: B (eginner's) A (All-Purpose) S (Symbolic) I (Instruction) C (Code) • Answer to complicated programming languages (FORTRAN, Algol, Cobol . ). First timeshare language. .
3-1 3 Using Tool Box Review and Preview • In this chapter, we begin a journey where we look at each tool in the Visual Basic toolbox. We will revisit some tools we already know and learn a lot of new tools. First, though, we look at an important Visual Basic functions. The Message Box • One of the best functions in Visual Basic is the message box. The message box displays a message, optional icon, and selected set of command buttons. The user responds by clicking a button. • The statement form of the message box returns no value (it simply displays the box): MsgBox Message, Type, Title where Message Text message to be displayed Type Type of message box (discussed in a bit) Title T ext in title bar of message box You have no control over where the message box appears on the screen. .
4-1 4 More Exploration of Toolbox Review and Preview • In this chapter, we continue looking at tools in the Visual Basic toolbox. We will look at some drawing tools, scroll bars, and tools that allow direct interaction with drives, directories, and files. In the examples, try to do as much of the building and programming of the applications you can with minimal reference to the notes. This will help you build your programming skills. Display Layers • In this class, we will look at our first graphic type controls: line tools, shape tools, picture boxes, and image boxes. And, with this introduction, we need to discuss the idea of display layers. • Items shown on a form are not necessarily all on the same layer of display. A form's display is actually made up of three layers as sketched below. All information displayed directly on the form (by printing or drawing with graphics methods) appears on the bottom-layer. Information from label boxes, image boxes, line tools, and shape tools, appears on the middle-layer. And, all other objects are displayed on the top-layer. r------------.--- r--_______. . Bottom-layer: form I . r~M iddle-layE'!r: label, image, shape, line r--------~---. / Top-layer: other controls and objects .
Advance Use of Visual BASIC 5-1 5 Advance use of VB Avoiding Naming Conflicts A naming conflict occurs when you try to create or use an identifier that was previously defmed. In some cases, naming conflicts generate errors such as "Ambiguous name detected" or "Duplicate declaration in current scope". Naming conflicts that go undetected can result in bugs in your code that produce erroneous results, especially if you do not explicitly declare all variables before first use. You can avoid most naming conflicts by understanding the scoping characteristics of identifiers for data, objects, and procedures. Visual Basic has three scoping levels: procedure-level, private module-level, and public module-level. A naming conflict can occur when an identifier: • Is visible at more than one scoping level. • Has two different meanings at the same level. For example, procedures in separate modules can have the same name. Therefore; you can define a procedure named MySub in modules named ModI and Mod2. No conflicts occur if each procedure is called only from other procedures in its own module. However, an error can occur ifMySub is called from a third module, and no qualification is provided to distinguish between the two MySub procedures. Most naming conflicts can be resolved by preceding each identifier with a qualifier that consists of the module name and, if necessary, a project name. For example: YourProject.YourModule.YourSub MyProject.MyModule.MyVar The preceding code calls the Sub procedure YourSub and passes MyVar as an argument. You can use any combination ofq ualifiers to differentiate identical identifiers. Visual Basic matches each reference to an identifier with the "closest" declaration of a matching identifier . For example, ifMyID is declared Public in two modules in a project (ModI and Mod2), you can specify the MyID declared in Mod2 without qualification from within Mod2, but you must qualify it as Mod2.MyID to specify it in Mod 1. This is also true if Mod2 is in a different but .
1-1 Appendix-I: Symbolic Constants Contents Alignment Constants . 1-4 Align Property . 1-4 Alignment Property . 1-4 Border Property Constants . 1-4 BorderStyle Property (Form) . 1-4 . BorderStyle Property (Shape and Line) . 1-4 Clipboard Object Constants . 1-5 Color Constants . 1-5 Colors . 1-5 System Colors . 1-5 Control Constants . 1-6 ComboBox Control . 1-6 ListBox Control . 1-6 ScrollBar Control . 1-6 Shape Control . 1-7 Data Control Constants . 1-7 Error Event Constants . ~ . 1-7 EditMode Property Constants . 1-7 Options Property Constants . 1-7 Validate Event Action Constants . 1-8 Beginning-ot-File Constants . 1-8 End-ot-File Constants . :. . 1-8 Record set-Type Constants . 1-8 Date Constants . 1-9 firstdayofweek Argument Values . 1-9 firstweekofyear Argument Values . 1-9 Return Values . :. . 1-9 .
II-I Appendix-II. Common Dialog Box Constants CommonDialog Control Constants File Open/Save Dialog Box Flags Constant Value Description cdlOFNReadOnly Ox1 Checks Read-Only check box for Open and Save As dialog boxes. cdlOFNOverwritePrompt 0x2 Causes the Save As dialog box to generate a message box if the selected file already exists. cdlOFNHideReadOnly Ox4 Hides the Read-Only check box. cdlOFNNoChangeDir Ox8 Sets the current directory to what it was when the dialog box was invoked. cdlOFNHelpButton Ox10 Causes the dialog box to display the Help button. cdlOFNNoValidate Ox100 Allows invalid characters in the returned filename. cdlOFNAllowMultiselect Ox200 Allows the File Name list box to have multiple selections. cdlOFNExtensionDifferent Ox400 The extension of the returned filename is different from the extension set by the DefaultExt property. cdlOFNPathMustExist Ox800 User can enter only valid path names. cdlOFNFileMustExist Ox1000 User can enter only names of existing files. cdlOFNCreatePrompt Ox2000 Sets the dialog box to ask if the user wants to create a file that doesn't currently exist. .
Glossary G-l Glossary 16 Bit Operating System This refers to an operating system which is capable of processing 16 bits (or 2 bytes) at anyone time. Windows 3.1 is an example. File names can be no more than 8 characters in 16 bit operating systems. 32 Bit Operating System This refers to an operating system, which is capable of processing 32 bits (or 4 bytes) at anyone time. Windows 95 is a 32 Bit Operating system. One of the features that this allows is long file names AdiveWindow In Windows, the front-most window on the desktop. The window which accepts any keyboard input. Archive A backup copy of data designed to be kept long term -often for security or audit reasons. The verb for doing this is also archive. BAT File Extension of a DOS Batch file - An ASCII text file of Dos Commands, which are executed in sequence. AUTOEXEC.BAT is a BAT file. Boot Quite simply to start up or restart (re-boot) the PC. When the PC is switched on the files which run the operating system are executed. Client Server Client/Server distributes the processing ofa Computer Application between 2 computers the Client & the Server -the principal being to exploit the power of each. The Client is normally a Pc. The Application Program will access Data & perform processing on the Server & using the data obtained via the server more processing tasks will be performed on the Client. The Application can be used by more than one user. Command Interpreter The command interpreter is a DOS program that executes commands entered at the DOS prompt. CPUIMemory Bus The CPU/Memory bus, also referred to as the System Bus, transmits data between the CPU, Cache & RAM. The CPUlMemory Bus runs at the same speed as the Processor. Database A collection of Data organised & designed for easy access. A collection of customer names & addresses may form the content of a database. Dialog Box A box displayed on your PC screen by a program including a message normally indicating that something is about to happen or has just happened. The dialog box requires the user to respond to the message before continuing with what it is about to do -normally the response is in the form of a Yes or No & based on the answer the program will carry out the next step or stop what it is doing. An example of this could be withm an Email .