Essential Visual Basic 5.0 Fast Includes ActiveX Control Development Springer London Berlin Heidelberg New York Barcelona Budapest Hong Kong Milan Paris Santa Clara Singapore Tokyo John Cowell Essential Visual s.o Basic Fast Includes ActiveX Control Development With 211 Figures , Springer John Cowell, BSc (Hons), MPhil, PhD Department of Computer and Information Sciences De Montfort University Kents Hill Campus, Hammerwood Gate, Kents Hill Milton Keynes, MK7 6HP, UK British Library Cataloguing in Publication Data Cowell, John, 1957- EssentialVisual Basic 5.0 fast : includes ActiveX Control Development 1.Visual Basic (Computer program language) 2.Visual programming (Computer science) I.Title 005.1'33 Library of Congress Cataloging-in-Publication Data Cowell, John, 1957- Essential Visual Basic 5.0 fast : includes ActiveX Control Development 1 John Cowell. p. cm. Includes index. ISBN-I3: 978-3-540-76148-8 e-ISBN-I3: 978-1-4471-0983-9 DOl: 10.1007/978-1-4471-0983-9 1. Application software--development. 2. Microsoft Visual Basic. I. Title. QA76.76.A65C69 1997 97-17622 005.26'8--DC21 CIP Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of repro graphic reproduction in accordance with the terms oflicences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. © Springer-Verlag London Limited 1997 The use of registered names, trademarks etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Typesetting: Camera ready by author 34/3830-543210 Printed on acid-free paper Contents 1 WHY USE VISUAL BASIC 57 ...................................................................... 1 Introduction .............................................................................................. 1 Why change to version 5? ........................................................................ 2 Is this book for you? ................................................................................. 2 How this book is organised ...................................................................... 3 New features in version 5 ......................................................................... 4 What computer do you need to run Visual Basic 5? ................................ 5 Versions of Visual Basic 5 ....................................................................... 5 Conventions .............................................................................................. 6 2 THE VISUAL BASIC ENVIRONMENT ......................................................... 7 Introduction .............................................................................................. 7 Starting Visual Basic 5 ............................................................................. 7 The Visual Basic design screen .............................................................. 10 The form ................................................................................................. 11 The Form Layout Window ..................................................................... 12 The Project Explorer window ................................................................. l3 The Properties window ........................................................................... 13 The Visual Basic standard menu bar and toolbar ................................... 14 The toolbox ............................................................................................. 15 3 SHRINKING AND GROWING .................................................................... 17 Introduction ............................................................................................ 17 Outline of the Shrinking and Growing program ..................................... 17 Creating a new project.. .......................................................................... 18 Defining the user interface ..................................................................... 19 Moving and scaling ................................................................................ 20 The Properties window ........................................................................... 20 Changing captions .................................................................................. 21 Displaying a picture ................................................................................ 22 Supported file types ................................................................................ 24 Common problems ................................................................................. 24 Running the program .............................................................................. 25 Event driven software ............................................................................. 26 Making the buttons work ........................................................................ 26 Saving the project ................................................................................... 29 v vi Contents 4 MANAGING PROJECTS AND FILES ....................................................... 30 Introduction ............................................................................................ 30 Opening and closing projects ................................................................. 30 Application files ..................................................................................... 32 FRM files ............................................................................................... 33 BAS files ................................................................................................ 33 OCX files ............................................................................................... 34 FRX files ................................................................................................ 34 CTL files ................................................................................................ 35 CLS files ................................................................................................. 35 Adding elements to a project.. ................................................................ 35 A common problem. ............................................................................... 36 5 STANDARD CONTROLS .......................................................................... 37 Introduction ............................................................................................ 37 The toolbox appearance ......................................................................... 37 Adding controls to forms ....................................................................... 38 Selecting controls ................................................................................... 38 Cutting and Pasting ................................................................................ 39 Common toolbox controls ...................................................................... 39 The pointer icon ..................................................................................... 41 Common properties ................................................................................ 41 The PictureBox control .......................................................................... 42 The Label control ................................................................................... 43 The TextBox Control ............................................................................. 43 Passwords ............................................................................................... 44 Captions and hot keys ............................................................................ 45 The Frame control .................................................................................. 45 The CommandButton control ................................................................. 45 The CheckBox control ........................................................................... 46 The OptionButtons control ..................................................................... 47 The ComboBox control .......................................................................... 47 The ListBox control ............................................................................... 48 The AddItem method ............................................................................. 49 The Sorted property ................................................................................ 49 The HScrollBar and VScrollBar controls ............................................... 49 The Timer control .................................................................................. 50 The Shape, Line and Image Controls ..................................................... 50 6 BUYING A NEW PC ................................................................................... 52 Introduction ............................................................................................ 52 Design of the form ................................................................................. 52 Adding the TextBox controls ................................................................. 53 Contents vii Adding the CheckBox controls ............................................................... 53 Adding the OptionButton Controls ......................................................... 54 Adding the ListBox and ComboBox controls ........................................ 54 Running the program .............................................................................. 54 Initialising the ListBox and ComboBox ................................................. 55 TabIndex and TabStop ........................................................................... 56 Forms ...................................................................................................... 56 Controlling form properties .................................................................... 56 Using properties ...................................................................................... 57 Hiding and showing forms ..................................................................... 58 Loading and unloading forms ................................................................. 58 The Show method ................................................................................... 59 The Hide method .................................................................................... 59 MDI forms .............................................................................................. 59 Arranging forms ..................................................................................... 60 7 DISPLAYING TEXT .................................................................................... 62 Introduction ............................................................................................ 62 The MsgBox function ............................................................................. 62 Message box text .................................................................................... 63 MsgBox buttons parameter. .................................................................... 64 Getting button input. ............................................................................... 66 Getting text input from a dialog box ...................................................... 67 Missing optional parameters ................................................................... 67 Controlling fonts and font characteristics ............................................... 68 Using the Print method ........................................................................... 70 Formatting numbers dates and times ...................................................... 70 Number format ....................................................................................... 70 Custom formats ...................................................................................... 72 Date and time format.. ............................................................................ 72 Writing to a printer ................................................................................. 74 The Printer object ................................................................................... 74 The PrintForm method ........................................................................... 74 8 DECLARING AND USING VARIABLES .................................................... 76 Introduction ............................................................................................ 76 Declaring variables ................................................................................. 76 Naming conventions ............................................................................... 77 Static variables ........................................................................................ 77 Variables scope ....................................................................................... 78 Variable types ......................................................................................... 78 The Boolean data type ............................................................................ 79 The Byte data type .................................................................................. 79 viii Contents The Currency data type .......................................................................... 79 The Date data type ................................................................................. 79 The Decimal data type ............................................................................ 79 The Integer data type .............................................................................. 80 The Long data type ................................................................................. 80 The Object data type .............................................................................. 80 The Single data type ............................................................................... 80 The Double data type ............................................................................. 80 The String data type ............................................................................... 80 The Variant data type ............................................................................. 81 Range of data types ................................................................................ 81 Converting between data types .............................................................. 81 Int and Fix .............................................................................................. 82 Arithmetic operations ............................................................................. 82 The comparison operators ...................................................................... 83 Arrays ..................................................................................................... 83 Declaring arrays ..................................................................................... 83 Changing array dimensions .................................................................... 84 User-defined data types .......................................................................... 84 The Erase statement ............................................................................... 85 9 CONTROLLING PROGRAM FLOW .......................................................... 86 Introduction ............................................................................................ 86 Types of program contro1.. ..................................................................... 86 The ILThen. .. Else statement. ................................................................ 87 Nested Ifs ............................................................................................... 87 The Else If clause ................................................................................... 88 The Select Case structure ....................................................................... 88 The GoTo statement ............................................................................... 89 Looping .................................................................................................. 89 The Do ... While clause ............................................................................ 90 The Do ... Until clause .............................................................................. 90 Exit Do statement ................................................................................... 91 The While ... Wend clause ...................................................................... 91 The For. .. Next construct. ........................................................................ 92 Terminating For loops ............................................................................ 92 Exit For .................................................................................................. 93 10 PROCEDURES AND FUNCTIONS .......................................................... 94 Introduction ............................................................................................ 94 Event procedures .................................................................................... 94 General procedures ................................................................................. 95 The scope of event and general procedures ........................................... 95 Contents IX Passing arguments .................................................................................. 96 Function procedures ............................................................................... 96 11 DEBUGGING ............................................................................................ 98 Introduction ............................................................................................ 98 What is debugging? ................................................................................ 98 Syntax errors and Quick Info ................................................................. 99 Logic errors .......................................................................................... 100 Using breakpoints ................................................................................. 100 Displaying the value of variables ......................................................... 101 Changing code in break mode .............................................................. 102 Continuing and stopping ....................................................................... 102 Stepping ................................................................................................ 102 Watchpoints .......................................................................................... 103 Conditional Compilation ...................................................................... 104 The Call Stack window ......................................................................... 105 The Debug menu .................................................................................. 105 12 OBJECT ORIENTED PROGRAMMING ................................................. 107 Introduction .......................................................................................... 107 Why use object oriented programming? .............................................. 10 7 What are objects? ................................................................................. 108 Using properties .................................................................................... 109 Using methods ...................................................................................... 109 Creating objects at run-time ................................................................. 110 Arrays of forms .................................................................................... 111 Arrays of control variables ................................................................... 112 User-defined classes ............................................................................. 114 Creating and using Collections ............................................................. 116 13 ACTIVEX CONTROLS ........................................................................... 118 Introduction .......................................................................................... 118 What are custom controls? ................................................................... 118 Creating an ActiveX controL .............................................................. 119 The ActiveX clock ................................................................................ 120 Writing the code ................................................................................... 121 Testing the control ................................................................................ 124 Resizing Controls ................................................................................. 126 Incorporating events in a Control ......................................................... 127 Making an OCX file ............................................................................. 130 Control Groups ..................................................................................... 131 The ActiveX Control Interface Wizard ................................................ l31 x Contents Running the Control Interface Wizard ................................................. 134 Testing the ActiveX Control ................................................................ 138 14 CREATING AND USING MENUS .......................................................... 141 Introduction......................... .. .......................................... .......... ..... ... ... 141 Creating a menu bar ............................................................................. 141 Amending the menu ............................................................................. 143 Shortcut keys ........................................................................................ 143 The Checked property .......................................................................... 144 Enabling and disabling controls ........................................................... 144 Pop-up Menus ...................................................................................... 145 15 USING CUSTOM CONTROLS .............................................................. 148 Introduction .......................................................................................... 148 The CommonDialog control... .............................................................. 148 Common Controls ................................................................................ 151 The Calendar control ............................................................................ 152 The Grid Control .................................................................................. 152 Uses of the Grid control ....................................................................... 153 Changing the number ofrows and columns ......................................... 153 Changing grid text ................................................................................ 154 Changing size....................................................................................... 155 Selecting cells ....................................................................................... 156 Adding and removing rows .................................................................. 156 Aligning text. ........................................................................................ 157 Scroll bars ............................................................................................. 158 Editing grid cells .................................................................................. 158 16 CREATING AND USING GRAPHiCS .................................................... 159 Introduction .......................................................................................... 159 The graphics controls ........................................................................... 159 The Line control ................................................................................... 160 The Shape ControL ............................................................................... 161 Properties of the Shape control ............................................................ 161 The co-ordinate system ........................................................................ 162 Twips and other units ........................................................................... 163 Using graphics methods ....................................................................... 163 Controlling individual pixels ................................................................ 164 The Line method .................................................................................. 164 Drawing boxes ..................................................................................... 165 The Circle method ................................................................................. 166 Drawing circles and ellipses ................................................................. 166
Description: