+ All Categories
Home > Documents > VB Book_ThankGod Egbe (2)

VB Book_ThankGod Egbe (2)

Date post: 08-Mar-2016
Category:
Upload: omotoso-adedapo
View: 71 times
Download: 2 times
Share this document with a friend
Description:
An overview into the understanding of Visual Basics

of 503

Transcript
  • Visual Basic 6.0 For

    Engineers and Scientists

  • Visual Basic 6.0 For

    Engineers and Scientists

    ThankGod Egbe Dept. of Petroleum Engineering

    University of Port Harcourt Port Harcourt, Nigeria

  • Published in Nigeria By

    Visual Basic for Engineers and Scientists Copyright 2003 ThankGod Egbe

    ISBN:

    All Rights Reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording or otherwise, without the written persmission of the copyright owner. No patent liability is assumed with respect to the use of information contained herein and neither is any liability assumed for damages resulting from the use or misuse of the information contained herein.

    Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. The copyright owner cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Windows 95, 98, 2000, NT and XP, Microsoft Word, Microsoft Excel,Microsoft Access, Microsoft Powerpoint, Microsoft Project, Microsoft Visual Basic and Microsoft Visual Studio are registered trademarks of Microsoft Corporation. AutoCad is a registered trademark of Autodesk Inc., CorelDraw is a registered trademark of Corel Corporation.

  • Dedication

    This book is dedicated to My Father

    Mr. Thompson Itua Egbe

    and my Mothers Mrs. Mary T.I.Egbe

    And Mrs. Elizabeth T.I.Egbe

  • Preface

    Visual Basic is a powerful but simple tool for building applications for Windows 9x, Windows 2000, NT and XP. These applications are by far larger and more complex than their predecessors that did not use a graphical user interface. Yet as program size and complexity have grown, programmers effort have actually decreased, at least for programmers who are using the right tools.

    Visual Basic is one of the right tools. The time you invest in learning to use Visual Basic will pay itseft back on your first windows programming project with Visual Basic. Depending on your interest and discipline, the length of time you will use to learn Visual Basic is a function of the nature of your instructors and the nature of books you use. For people with interest in the business world, they will learn faster with text books demonstrating various applications of Visual Basic in the business world. A lot of books exist in this area. However, for people with interest in the Science and Engineering world, they will certainly feel more comfortable using a textbook dedicated to demonstrating various applications of Visual Basic in the Science and Engineering world.

    This text was written primarily for Science, Engineering and Mathematics students as well as practicing professionals. The motivation to write this book stems from the fact that most of the books written in Visual Basic hardly have Science and Engineering applications and hence do not allow Science and Engineering professionals to appreciate the importance of Visual Basic. This book is a teach-yourself guide to learning Science and Engineering-oriented Visual Basic. It was written to meet the needs of students and lecturers in Engineering, Sciences and Mathematics in Universities, Polytechnics, Colleges of Education and Computer training and software development outfits. For those preparing for Microsoft Software Certification(MCSD), you will find this book useful in your Visual Basic Desktop application development certification examination and the certification examination on Analysing Requirements and Defining Solution Architecture.

    The book is in two volumes, Volume 1 covers the introductory and intermediate topics in Visual Basic which will enable beginners develop a powerful standalone scientific applications. Volume 2 covers the advanced topics in Visual Basic and is a must read for those who want to develop state of the art software with Visual Basic. Volume 1 of this book has 10 chapters while Volume 2 has 12 chapters. The chapters in Volume 1 are; User Interface Design Fundamentals, Visual Basic Programming Fundamentals, Control Structures, Data Structures and Strings Arithmetic. Others are Functions and Sub Procedures, Controls Array , Visual Basic for Applications, Numerical Applications and Database Access.

  • In writing this book, it is assumed that the reader does not know anything about Visual Basic or programming, so the book starts off with simple, straightforward and well explained examples. From there, the chapters become more advanced and comprehensively cover the essentials of programming with Visual Basic. Because each chapter builds upon the concepts in the preceding ones, it is important that you understand the material covered in one chapter before moving to the next. An evaluation exercise in the form of problem set has been added at the end of each chapter to enhance the appreciation and understanding of Visual Basic.

    In addition, the book ships with a CD ROM containing code samples,Visual Basic projects and solutions to selected exercises aimed at safisfying the expectations of students as well as consolidating their knowledge and skills. A supporting Website is also available where users can get periodic review, download codes and also get online training

    ThankGod Egbe October, 2003

  • Acknowledgement

    First of all, I would like to thank the Science and Engineering students of University of Port Harcourt, Federal University of Technology Owerri and Rivers State University of Science and Technology for using the manuscript of this book for their Course and Project work and subsequently encouraging and motivating me to produce this text book.

    In producing this book, so many persons have contributed in one way or the other to bring this book to fruition. To Engr. Armstrong Lee , I say a very big thank you to you for providing the computer and comfortable atmosphere used in typesetting this book and spending sleepless nights with me to edit and proof-read the manuscript. To Engr. Joe Kintum of the Shell Petroleum Development Company, Warri, I say another big thank you for reviving the hope of producing this book and your sustained encouragement.

    The assistance and encouragement I received from my cousin, Lucky Stephen is very well acknowledged. To John Otogbo, I deeply acknowledge and appreciate the time you devoted reading this manuscripts especially in its raw form. To Aniebiet Udoh, my Programming companion, I very well appreciate the sugesstion and assistance you have provided to me especially concerning this book and the accompanying CD ROM.

    The encouragement and motivation I received from Dr. Dulu Appah, Dr. Adewale Dosunmu, Engr. Martin Chukwuezi, Mr. Boma Kinigoma and Engr. S. S. Ikiensikimama of the Faculty of Engineering University of Port Harcourt are well acknowledged.

    To Kedei Obeten, Aniekan Udo, Jerome Onwunalu, Nnanna Erasmus, Usman Anes, Adoga Charles, Okeoma Ihunwo, Chiringo Chinda and Ikokwu Ihechi, I thank you all for the various ways you have contributed to make this book a success.

    Also, I wil like to thank my Father, Mr. T. I. Egbe and Mothers Mrs. Mary T. I. Egbe and Mrs. Elizabert T. I. Egbe for their encouragement right from the first day I conceived the idea of writting this book. Ill also like to thank my brothers Richard, Egbe, Bassey, Emmanuel, Eneh and Pringle and my cousins Igwe Johnson, Okoi Inah and James Inah for their sustained support. To all others who have in one way or the other contributed to make this book a success, I say thank you and God bless you all.

    Most importantly, I thank the Almighty God for giving me the enablement to write this book.

    ThankGod Egbe

  • Supporting WebSite

    For more information about the book, CD ROM or Supporting WebSite, send a mail to [email protected]

  • Contents at a Glance

    Chapter Page

    1. Elements of User Interface Design 1

    2. Visual Basic Programming Fundamentals 48

    3. Control Structures 96

    4. Data Structures 155

    5. String Arithmetic 211

    6. Functions and Sub Procedure 265

    7. Controls Array 300

    8. Visual Basic for Applications 330

    9. Numerical Application s 374

    10. Database Access 414

    Appendix A KeyAscii and KeyCode Constants 453

    Appendix B Derived Maths Functions 463

    Appendix C Saving and Opening Visual Projects 465 Appendix D 471

    Index 485

  • Table of Content

    Preface .............................................................................................................. vi 1. Design Fundamentals .................................................................................... 1

    Visual Basic Controls In Windows Applications .............................................. 2 Visual Basic IDE ................................................................................................. 5

    Properties Window.......................................................................................... 7 Toolbar ............................................................................................................ 9 Project Explorer ............................................................................................ 10

    Naming Visual Basic Controls. ....................................................................... 12 Visual Basic Units Of Measurement ............................................................... 13 Designing program interface. .......................................................................... 14

    Formatting Controls ...................................................................................... 16 Designing Windows Interface Using Visual Basic Controls ........................ 17 Introducing the Combo Box And Frame ...................................................... 17 Introducing The Line control, Checkbox control and Picture box control .. 19 Introducing The Text Box and the Vertical Scroll Bar ................................ 20 Introducing the horizontal scroll bar ............................................................. 21 Introducing the list box ................................................................................. 21 Introducing the Data Control ........................................................................ 23 Other intrinsic controls ................................................................................. 23

    Interface Design with Windows Common Controls. ...................................... 24 Adding the Microsoft Windows Common Control to the ToolBox. ............ 25 Configuring the Tabstrip Control.................................................................. 26 The Property Pages ....................................................................................... 27 Configuring the UpDown Control ................................................................ 27 Configuring the Toolbar Control .................................................................. 28 Adding Images to the Toolbar Control. ........................................................ 29 A Closer Look at the Toolbar Buttons .......................................................... 30 Other Controls from Components ................................................................. 31

    Designing Menus ............................................................................................. 32 Notes on Menus/Menu Editor ....................................................................... 33

    Designing Your Own Application.................................................................... 34 Other Input Controls ..................................................................................... 36 The Combo Box ............................................................................................ 38 Enhancing the Van Der Waal Problem. ........................................................ 40 Generalized Conversion Program ................................................................. 41

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe iiNavigational Design ......................................................................................... 42 Problem Set 1 ................................................................................................... 44

    2. Programming Fundamentals ...................................................................... 48 Setting Objects Property Using Code ............................................................. 49 Visual Basic Data Types .................................................................................. 51

    Byte Data Type ............................................................................................. 52 Integer Data Type ......................................................................................... 52 Long Data Type ............................................................................................ 52 Single Data Type........................................................................................... 52 Double Data Type ......................................................................................... 52 Currency Data Type ...................................................................................... 52 String Data Type ........................................................................................... 53 Date Data Type ............................................................................................. 53 Boolean Data Type ....................................................................................... 54 Variant Data Type ......................................................................................... 54 Object Data Type .......................................................................................... 54 Other Data Types .......................................................................................... 54 Variables And Constants............................................................................... 54 Storing Data In Variables/Constants ............................................................. 55 Defining Variables ........................................................................................ 57 Defining Variables Using Objects properties .............................................. 59 Defining Variables Using the InputBox Function ........................................ 63 Defining Variable using Files ...................................................................... 63 Viewing the Content of Variables................................................................. 64 Viewing Variables Value using objects properties ....................................... 64 Print Statement .............................................................................................. 66 Viewing the content of variables using message box ................................... 66 Using the Local and Watch Window ............................................................ 67

    Processing Data................................................................................................ 67 Operators ....................................................................................................... 67 Arithmetic Operators .................................................................................... 68 String Operators ............................................................................................ 68 Comparison operators ................................................................................... 69 Functions ....................................................................................................... 70

    Events and Event Procedures .......................................................................... 72 The Form Module and Event Procedures ..................................................... 73 Creating the Click Event Procedure of a CommandButton .......................... 74 Creating click Event procedure of a TextBox ............................................... 75

  • Contents

    iii

    Changing Object Properties in Event Procedure .......................................... 78 Read Only Properties .................................................................................... 80 Modifying the Caption and Text Properties in Event Procedures using Variables ....................................................................................................... 80

    Scope of Variables ............................................................................................ 84 Procedure Level Variables ............................................................................ 84 Static Variables ............................................................................................. 84 Module Level Variable ................................................................................. 85 Public Variables ............................................................................................ 86

    Programming Accessories ............................................................................... 87 Comments ..................................................................................................... 87 Line Continuation ......................................................................................... 87 Line Termination .......................................................................................... 88 Line Numbers/Label ..................................................................................... 88 With Block .................................................................................................... 89

    Multiple Forms Applications ........................................................................... 89 Problem Set 2 ................................................................................................... 93

    3. Control Structures ....................................................................................... 97 Decision Structures .......................................................................................... 98

    IF Then Else Structure .................................................................................. 98 The Else If Statement .................................................................................. 100

    Constructing Boolean Expressions .................................................................. 105 Nested If Structure ...................................................................................... 108 If Then End If Structure .............................................................................. 110 Select Case Structure .................................................................................. 112 Nested Select case Structure ....................................................................... 116

    Decision Statements ....................................................................................... 118 If Then Statement. ....................................................................................... 119 If Then Else Statement ................................................................................ 119 Decision Functions...................................................................................... 119 IIF Function ................................................................................................ 120 Switch Function .......................................................................................... 120 Choose Function ......................................................................................... 121 For Next Loops ....................................................................................... 123

    Do Loops ................................................................................................... 127 Pre-checked Do Loop .................................................................................... 127

    Post Checked Do Loop .............................................................................. 130 Solving Problems with Do Loops ............................................................... 132

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe ivRecursive problems ..................................................................................... 132 Number Base Conversions .......................................................................... 135 The Exit Statement ...................................................................................... 137 The Go To Statement .................................................................................. 138 Nested Loops .............................................................................................. 138 Dynamic Interface Design .......................................................................... 139

    Problems Set 3 ................................................................................................ 146

    4. Data Structures .......................................................................................... 155 Arrays ............................................................................................................. 156

    Declaring an Array ...................................................................................... 157 Declaring Dynamic Arrays ......................................................................... 157 Array Transfer ............................................................................................. 158 Transfer to a scalar variant variable using the Array Function .................. 160 Array Arithmetic ......................................................................................... 161 Array and Summation ................................................................................. 162 Sorting an Array .......................................................................................... 163 Bubble Sort ................................................................................................. 163 Searching an Array ..................................................................................... 165 Ranking an array ......................................................................................... 166

    Lists ................................................................................................................. 168 Inserting Items into a List at Design time Using the Properties Window .. 169 Inserting Items into a List at Run time Using the AddItem method ........... 169 Inserting Items into a List at Run Time Using the ...................................... 170 Working with the ListItems using the ListIndex Property ......................... 171 Copying Items from a ListBox Using Drag-Drop ...................................... 179 Sorting Data in a List using the Sorted Property ........................................ 180 Synchronising a Sorted List Using The ItemData Property ....................... 180 Selecting Multiple Items from a list............................................................ 182 Manipulating Multiple Selection Using the Selected Property................... 183

    Grids ............................................................................................................... 186 Adding the FlexGrid controls to the ToolBox. ........................................... 188 Understanding Basic Properties of the Grid ............................................... 189 Entering and Retrieving Data in Grids........................................................ 189 Using the TextMatrix Property ................................................................... 190 Using the Text Property .............................................................................. 191 Using the AddItem Method ........................................................................ 192 Using the TextArray Property ..................................................................... 193 Using the ClipBoard/Clip Property ............................................................. 195

  • Contents

    v

    Formatting Cells.......................................................................................... 197 Changing the Font Properties...................................................................... 197 Cell Alignment ............................................................................................ 198 Formatting a range of cells ......................................................................... 199 Merging cells .............................................................................................. 200 Sorting Data in Grids .................................................................................. 202

    Data Files ....................................................................................................... 203 Writing /Reading Arrays/ListItem To files. ................................................ 205

    Problem Set 4 ................................................................................................. 206

    5. Strings Arithmetic ..................................................................................... 212 String Operators ............................................................................................. 213

    Concatenation Operator .............................................................................. 213 Like Operator .............................................................................................. 214

    Option Compare Statement ............................................................................ 216 Option Compare Text ................................................................................. 217 Option Compare Binary .............................................................................. 217

    String Functions ............................................................................................ 217 Val Function............................................................................................... 217 Cstr, Str, Str$ Functions .............................................................................. 218 Len Function ............................................................................................... 218 Ltrim, Rtrim and Trim Functions................................................................ 219 Rtrim Function ............................................................................................ 220 Trim Function ............................................................................................. 220 Simple Application ..................................................................................... 220 UCase, LCase and StrConv Functions ........................................................ 222 ASCII Functions ........................................................................................ 222 The KeyPress Event .................................................................................... 224 String function ............................................................................................ 226 Split and Join Functions .............................................................................. 227 The Mid Function ....................................................................................... 229 Converting Numbers to Base 10 ................................................................. 230 Hexadecimal Numbers ................................................................................ 232 StrReverse Function .................................................................................... 232 Private Sub cmdExecute_Click() .................................................................... 233 The Mid Statement ...................................................................................... 234 Encryption and Decryption ......................................................................... 234 Instr and InstrRev Function ....................................................................... 236 The InstrRev Function ................................................................................ 237

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe viLeft and Right Function .............................................................................. 240 Converting Numbers with Decimal Part to Fraction .................................. 242 Replace function ......................................................................................... 242 Working with fractions ............................................................................... 243 Decimal and Binary Shifting ...................................................................... 243 Scientific Notation ...................................................................................... 244

    Formatting Output ......................................................................................... 244 Format Currency Function .......................................................................... 245 Format Percent Function ............................................................................. 246 FormatNumber Function ............................................................................. 246 FormatDate Time Function ......................................................................... 247 Round Function ........................................................................................... 248 Format Function .......................................................................................... 248 Customizing The Format Function ............................................................. 249

    Message Box ................................................................................................... 250 Message Box Buttons ................................................................................. 251 Message Box Icons ..................................................................................... 251 Using the Message Box as a Function ........................................................ 252 Controlling The Default Button .................................................................. 254 Message Box with Optional Arguments .................................................... 254 String Functions and the Message Box Message ........................................ 255

    Input Validation and Errors .......................................................................... 255 Data Inspection Functions........................................................................... 257 Input Validation using the Validate Event .................................................. 260

    Problem Set 5 ................................................................................................. 261

    6. Functions and Sub Procedures ................................................................. 266 Creating Functions ........................................................................................ 267

    Function Definition .................................................................................... 268 Implementation of Function in Visual Basic .............................................. 269 Writing Programs with User Defined Functions ........................................ 272 Using the ByVal and ByRef Keyword........................................................ 273 Functions With No Argument ..................................................................... 275 Functions With One Argument ................................................................... 275 Functions with two Argument .................................................................... 276 Functions with three Arguments ................................................................. 278 Functions With Indefinite Arguments ........................................................ 278 Functions with Optional arguments ............................................................ 279 Functions with Array As Argument ............................................................ 282

  • Contents

    vii

    Functions Which Return Array ................................................................... 283 Functions with Object as Argument/Return Type ...................................... 284 Recursive Functions .................................................................................... 288

    Sub Procedures .............................................................................................. 290 Passing Objects To Sub Procedure ............................................................. 292 Returning Multiple Values From A Sub Procedure .................................... 293

    Event Sub Procedure ..................................................................................... 295 Sub Main ..................................................................................................... 295

    Problem Set 6 ................................................................................................. 298

    7. Controls Array .......................................................................................... 300 Creating Controls Array ................................................................................ 301

    Creating controls array using copy and paste method ................................ 301 Creating controls array using the Index property ....................................... 302 Creating controls array using the Name property ....................................... 302 Event Procedures of Controls Array ........................................................... 302 Properties of Controls Array ....................................................................... 304 Creating Elements of Controls Array at Runtime ....................................... 305 Unloading controls array Elements at runtime ........................................... 306 Giving Controls Array Element Equal Spacing .......................................... 307

    Array of TextBoxes ........................................................................................ 311 Array of ListBoxes ........................................................................................ 315 Array of Grids................................................................................................. 318 Array of PictureBoxes ................................................................................... 319 Array of Lines................................................................................................. 324 Problem Set 7 ................................................................................................. 326

    8. Visual Basic for Applications ................................................................... 330 VBA Functions ........................................................................................... 330 VBA and OLE automation.......................................................................... 330

    Overview of MS Excel .................................................................................... 331 Window showing Excel cells ...................................................................... 331 Filling formulae in cells .............................................................................. 333 Plotting Graphs ........................................................................................... 333 Using Excels Functions ............................................................................. 337 Working with Array Functions ................................................................... 338

    VBA Functions ............................................................................................... 340 Worksheet and VBA Functions ..................................................................... 342

    Writing VBA Functions with Indefinite Arguments .................................. 343

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe viii VBA Collections ............................................................................................. 343

    Methods of the Range/Selection Object ..................................................... 345 The Selection Object ............................................................................. 346 Reading and Writing from Selections ......................................................... 346 Selection Object .......................................................................................... 346 The Cells Property ...................................................................................... 347 Offset Method ............................................................................................. 349

    VBA Dialog Boxes ......................................................................................... 350 Figure 8.14 .................................................................................................... 352 Figure 8.16 .................................................................................................... 354 Listing 8.6: Evaluating Functions at runtime .................................................... 354

    Macros ............................................................................................................ 355 Using Recorded Macros .............................................................................. 358 Recording a Graph Macro ........................................................................... 358

    VBA and OLE Automation ............................................................................ 362 Visual Basic, VBA , OLE Automation and OLE Control ........................ 367 Adding the OLE Control to the Form ......................................................... 367

    Problem Set 8 ................................................................................................ 370

    9. Numerical Applications ............................................................................ 373 Evaluation of Functions at Runtime ............................................................. 374

    Evaluating Functions at Runtime using Strings .......................................... 374 Excel Application object ............................................................................. 374 Using Script Control ................................................................................... 377 Using Function Box .................................................................................... 379

    Non Linear Equations. .................................................................................. 379 Numerical Differentiation ............................................................................. 384

    Numerical Differentiation with Discrete points .......................................... 386 Numerical Integration ................................................................................... 389

    Multiple Integrals ........................................................................................ 394 Polynomial Interpolation ............................................................................... 398

    Lagrange Interpolation .......................................................................... 399 Divided Difference Interpolation ................................................................ 402

    Introduction to Matrix Algebra ..................................................................... 407 Using Excel Functions to Evaluate Matrices .............................................. 407

    Problem Set 9 ................................................................................................. 409

  • Contents

    ix

    10. Database Access ......................................................................................... 413 Data Control ................................................................................................... 415

    Creating a Simple Database Application .................................................... 416 Displaying Multiple Records In Bound Controls ...................................... 419

    The Recordset Object ..................................................................................... 421 Find Methods .............................................................................................. 421 Types of RecordSet ..................................................................................... 424

    The Visual Data Manager ............................................................................. 424 Relationships ............................................................................................... 425 Adding Records to Tables Using Visdata ................................................... 435 Manipulating Records in Visual Basic with Visdata Data Form Designer 437

    Structured Query Language .......................................................................... 440 Data Manipulation Language Queries ........................................................ 440 Using the Select and From clauses to designate a Recordsource in code ... 441 Using the SQL Where Clause ............................................................. 442 Specifying Multiple Criteria With the Where Clause ................................ 444

    Visual Basic Data Applications ..................................................................... 445 Using the SQL Order By Clause ................................................................. 448 Using the SQL As Clause ........................................................................... 448 Using the SQL Aggregate Functions .......................................................... 449

    Problem Set 10 ............................................................................................... 450

    Appendix A: ASCII Code Constant ................................................................ 452 Appendix B: Key Code Constant..................................................................... 456 Appendix C: Derived Maths Functions .......................................................... 463

  • CHAPTER

    1 DESIGN FUNDAMENTALS

    Visual Basic Controls In Windows Applications

    Visual Basic IDE

    Naming Visual Basic Controls.

    Visual Basic Units Of Measurement

    Designing Visual Basic Interfaces

    Interface design with Windows Common Controls.

    Designing menus

    Application design

    Navigational design

    Problem Set

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 2Learning Visual Basic (or any member of Visual Studio) is interesting at times when you realise that you would be writing programs with familiar Windows like those of MS Word and Excel. Often you use controls (or objects) in MS Office without bothering to know their names, all you are interested in is that it should work for your purpose. Thats OK, infact very OK for appreciation level. After reading this chapter you will depreciate your respect for most of the windows you see because you can design them in minutes and at times better than how celebrated software developers designed theirs. This is because you are now learning the language (or a language similar to that) used to develop applications like Word, Excel and other windows based software, hence your power of thought can make you change things for the better. We are going to begin this book in an unconventional manner. We will start with what you know and tell you something you are not aware of in what you know. This will make our discussion very friendly. Visual Basic Controls In Windows Applications

    One of the most familiar windows in the system is the shut down windows of windows 98. The shut down window is shown in Figure 1.1

    FIGURE 1.1 Shut Down Windows Dialog

    You should be familiar with the function of the window. You select an option and click OK command for windows to perform an action based on the option you have selected. You can also click the cancel command to hide this window or click the Help button to invoke windows help. The buttons used to indicate that an item is selected from a group of mutually exclusive options are called option buttons. While those that enable your program to perform an action when a user clicks them are called the Command Buttons. In the dialog in Figure 1.1, there are 1. Three Command Button Controls 2. Four Option Button Controls 3. One Label Control 4. One Image Control

  • Design Fundamentals

    3

    The label control is used to display textual information to the user while the image control is used to display graphical information to the user. In Visual Basic, the dialog shown in Figure 1.1 is called Form window. We will be using this term very often because virtually nothing happens without the Form for most of the applications you will be developing in this book. It could be called different names in other programs, however always remember that its general name is Form Window. In compiled application it could be called Splash Screen, Dialog Box, Message Box etc. You now know five Visual Basic objects(controls) and at least what they are used for. Before going ahead to introduce more objects, we should pause a bit and discuss properties of object. An objects property defines its appearance and behavior due to the data stored in it. Lets discuss four of the properties of the objects in Figure 1.1. These properties are

    1. Caption 2. Picture

    3. Value 4. Visible The Caption property determines the textual information displayed by objects. In Figure 1.1, the caption of the Form is shut down windows . The command buttons also have captions. The caption of the three command buttons are OK, Cancel, Help . Observe that almost all the objects in the form including the Form itself have the caption property. This property allows objects to display textual information about themselves or other controls without the caption property. The exception is in the image control. Instead of the caption used to display textual information, the image control has the picture property that enables it to display graphical information. The option button has another property called the Value property which is used to indicate the selected option from a group of options. The value property can either be True or False. It is true when the item is the selected item and False when the item is not selected. For option buttons, the value property is a Boolean property because it has a value of True or False. All properties that have values of true or false are Boolean properties. Other examples of boolean properties are the visible, enable and CausesValidation properties. The Visible property determines whether the user sees an object or not. It is true when the object is visible to the user and false when it is invisible. Other properties of objects that are often employed in visual interface design are the Top, the Left, the Width and the Height properties. We are going to illustrate these properties using Figure 1.2

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 4

    FIGURE 1.2 Illustrating Height, Top, Left and Width Properties of a control

    Figure 1.2 shows a form window with the text box at the centre of the form. The text box is an object(or control) similar to the label control in that it is used to display textual information. In addition, it also allows the user to edit this information unlike the label. The text box has almost all the properties of the label in addition to other properties unique to the text box only. The arrows labeled A to D show respectively the top, the height, the width and the left properties of the text box. These properties are defined below

    Top: The top is the vertical distance between the top of the form and the top of the object

    Height: The vertical height of the object Width: The width or breadth of the object Left: The horizontal distance from the left edge of the form to the left edge of the

    object.

    A

    B D

    C

  • Design Fundamentals

    5

    VISUAL BASIC IDE

    Its high time we started using Visual Basic so that we can see the object physically and visualize their properties. We will take the next few steps and show us how to launch the Visual Basic Integrated Development Environment(IDE)

    FIGURE 1.3 Visual Basic new Project dialog

    1. After installing Visual Basic in your system, you open it as follows: Select Start Menu, Programs, Microsoft Visual Studio, Microsoft Visual Basic (or

    select the Visual Basic icon from the desktop or office shortcut if you have created it)

    2. You should see the splash screen, wait until the splash screen disappears. You should see a window similar to Figure 1.3) Select Standard Exe and click open . The Visual Basic integrated development environment as shown in Figure 1.4 appears

    Figure 1.4 shows the Visual Basic IDE. The function of each window is discussed below.

    When you open Visual Basic, not all the windows shown in figure 1.4 appear. You can view those absent using the view menu. e.g if the properties window is not visible, activate the view menu and select properties window from the menu Items. The start up windows are the windows present when Visual Basic was last closed

    NOTE

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 6

    FIGURE 1.4 Visual Basic Integrated Development Environment

    Menu Bar: Used to activate the top level menus. Same function with that of a menu bar in other applications you have used Form Window: The form window is used to create dialog boxes and windows used in application development. Tool Box The tool box houses the objects drawn on the form. The label, option button, command button controls discussed previously are resident in the tool box. You select an object from the tool box and draw an instance( or a copy) of it on the form. By default, there are twenty one objects in the toolbox. Out of these only twenty are programmable objects. These are called intrinsic(or standard or default) controls because they are always present when Visual Basic loads. Other Controls from components and third parties are also placed on the tool box before instances of them can be drawn on the form. Figure 1.5 shows the tool box in its default state.

    Properties Window

    Tool Box

    Form Window

    Form Layout Window Project Explorer Window

    Menu Bar

    Tool Bar

  • Design Fundamentals

    7

    FIGURE 1.5 Tool box showing Standard Controls

    Properties Window As controls are drawn on the form, the properties of these controls can be

    modified using the properties window. Figure 1.6 shows the properties window displaying the properties of the form

    FIGURE 1.6

    Properties Window showing form properties in alphabetical order.

    Label Control Picture Box Command Button Frame Combo Box Option Button

    Check Box

    Horizontal Scroll BarDirectory List BoxDrive List Box Line Control Shape Control OLE Control Data Control

    Vertical Scroll Bar

    Image Box

    File List Box

    Timer

    List Box

    Text Box

    Pointer

    Object List

    Property

    Property value

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 8

    Properties in the properties windows are arranged either alphabetically or in categories. Figure 1.6 shows the properties windows with form properties arranged alphabetically. You change a property by selecting the property and modifying the default(or present) value at the right hand side of the property(property value). Property values can be changed in one of four ways. These are discussed below

    1. By selecting the property and then typing the property value yourself with the keyboard. A typical property that is changed in this manner is the height property.

    2. By selecting the property and modifying the property by selecting an applicable named constant from the drop down list of the property value region. A typical property that can be changed in this manner is the border style property.

    3. By selecting the property and then modifying the property by clicking the ellipsis on the right of the property. This will usually launch a dialog box. A typical property that is changed in this manner is the picture property.

    4. By selecting the property and then open the drop down box in the property list. This list is then filled with constants(data) on different rows. The list property is a typical property changed in this manner.

  • Design Fundamentals

    9

    The properties window with form properties in categories is as shown in Figure 1.7. Properties are set in categories by clicking the categorized tab on the properties window. When properties are arranged in categories, related properties are grouped together under a common heading.

    FIGURE 1.7

    Properties Window showing properties in categories

    Common categories are: Appearance, Behavior, Data, DDE, Font, Miscellaneous, Position and Scale. Some of these categories are absent in some controls. E.g for the intrinsic controls only the Picture Box has the scale category. Properties in this category are scaleheight, scaleleft, scalemode, scaletop and scalewidth. The font category has only one property which has other properties and is set using the font dialog.

    Toolbar Contains buttons that provides quick access to the menu command. Visual Basic has four types of tool bar. These are

    1. Standard Toolbar

    2. Edit Toolbar.

    3. Debug tool bar

    4. Form Editor Toolbar

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 10

    Project Explorer The project explorer provides an easy means of working with the components of the projects. It displays the name and filename of the components in the project. Figure 1.8 shows the project explorer. From the project explorer you can deduce so many things about your project. Some of these include

    It tells you how many forms exist in a project It tells you if a form is saved or not It tells you if the project has been saved or not It tells you if a name has been given to a form or not It tells you if a name has been given to the project or not It reveals the active form in a project. It enables us to identify other components in the project apart from the form. It enables you know if these components have valid names and have been saved It enables you identify a multiproject application and to set the active project in

    such applications

    FIGURE 1.8 Visual Basic project explorer showing project modules

    The project explorer has many other functions and listing all might take scores of lines. From the project explorer you can toggle folder, you can move between projects, you can move from the code window and back to the form. You can also remove a form or a

    View Code View Object Button

    Toggle Folder

  • Design Fundamentals

    11

    project in addition to so many other functions that will become clear to you as you progress in your Visual Basic carrier. The following can be deduced from the project explorer shown in Figure 1.8

    1. There are four forms in the application. You recognize a Visual Basic form with the unique form icon

    2. Three of the forms have been saved. Visual Basic forms are saved with the extension .frm

    3. Two of the forms have been given names. A form in Visual Basic is named with the prefix frm. e.g frmOil

    4. The project has been saved. A Visual Basic Project(Standard Exe ) is saved with the extension .vbp eg PNG505.vbp

    5. The project has not been given a name. Once a project has been given a name, the name appears on top of the project explorer as project - project name. You assign names to projects using the project properties dialog. Select properties from the project menu and you see the project properties dialog.(see figure 1.9). You can type any name say Petroleum in the project name text box of the project properties dialog

    6. The project contains a basic module(standard module) that has not been saved. A standard module is saved with a .bas extension.

    FIGURE 1.9 Visual Basic project properties dialog used to assign names to project and also select startup object among other functions

    To invoke the project property dialog, activate the project menu and select properties in the menu items.

    NOTE

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 12

    Naming Visual Basic Controls. Before designing program interfaces with Visual Basic controls, it is important that we know the convention used to name Visual Basic controls. The name of a control is what Visual Basic uses to interact with the control. The name of a control is not always visible to the user like the caption or the text, it is used internally to manipulate a control. The prefixes used to name some of the Visual Basic controls are as shown in Table 1.1 Table 1.1 Prefixes used in naming Visual Basic controls. S/No Control Prefix S/No Control Prefix 1 CommandButton cmd 16 Shape shp 2 OptionButton opt 17 Line lin 3 TextBox txt 18 FileListBox fil 4 Label lbl 19 DirListBox dir 5 Frame fra 20 DriveListBox drv 6 Form frm 21 Timer tmr 7 CheckBox chk 22 Tabstrip tbs 8 PictureBox pic 23 Menu mnu 9 Image img 24 ToolBar tbr 10 Data dta 25 Statusbar stb 11 ListBox lst 26 ProgressBar pgb 12 Vertical Scroll Bar vsb 27 UpDown Control upd 13 Horizontal Scroll Bar hsb 28 Slider sld 14 OLE ole 29 Script Control sct 15 ComboBox cbo 30 Grid Control grd

    Valid control names using these prefixes are cmdExecute,sctFunction, chkIgnoreError, optKelvin, txtInput. Others are mnuFile,frmMain,lblResults.

    You name an object using the name property in the properties window. Some objects like the menu are however named differently. This will be discussed when creating menus. Always begin the name of an object with the appropriate prefix and ensure that neither space nor special character is part of the name.

    NOTE

  • Design Fundamentals

    13

    Visual Basic Units Of Measurement The importance of units in science and engineering can not be over emphasized. A length measured as two(2) makes no sense becomes it is meaningless. It could be 2 cm or 2 km or even 2 miles. For this reason, it is important that we understand the various unit systems used to measure length and how to set these unit systems using the properties window. To change a unit system, we use the scale mode property of the form. The various settings for the scale mode property is as shown in Table 1.2. Table 1.2: Setting for the Scale mode property Constant Setting Description vbUser 0 Indicates that one or more of the

    ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties are set to custom values.

    vbTwips 1 (Default) Twip (1440 twips per logical inch; 567 twips per logical centimeter).

    vbPoints 2 Point(72 points per logical inch). vbPixels 3 Pixel(smallest unit of monitor or

    printer resolution). vbCharacters 4 Character (horizontal = 120

    twips per unit; vertical = 240 twips per unit).

    vbInches 5 Inch. vbMillimeters 6 Millimeter. vbCentimeters 7 Centimeter. vbHimetric 8 HiMetric vbContainerPosition 9 Units used by the control's

    container to determine the control's position.

    vbContainerSize 10 Units used by the control's container to determine the control's size

    The default Scale Mode property is the twip, you can set it to centimeters or inches.

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 14

    Designing program interface. In order to get used to Visual Basic controls and of course their names , we will take some time and design some of the windows found in common windows applications We will start with the shut down window of Figure 1.1 To design the shut down window, follow the steps outlined below. 1. Start a new Visual Basic Standard Exe project and arrange your windows as shown in

    Figure 1.4 2. Use the properties window to change the following properties of the form

    Name frmShutDown Caption Shut Down Window BorderStyle 4(fixed Toolwindows) Height 2900 twips Width 4800 twips

    3. Bring four option buttons and draw them on the Form. Try to arrange the option buttons so that they will have the alignment as in Figure 1.1. Change the following properties of the option buttons Option Button 1: Name - optStandBy, Caption (S&tand By) Option Button 2: Name - optShutDown, Caption - &Shut Down , Value True Option Button 3: Name - optRestart, Caption - &Restart Option Button 4: Name - optRestartDos, Caption: Restart in &MS Dos Mode

    4. Bring three command buttons and arrange them as in Figure 1 Command Button 1: Name cmdOK, Caption OK , Default True Command Button 2: Name cmdCancel, Caption Cancel , Cancel True Command Button 3: Name cmdHelp, Caption - &Help

    5. Bring a label and place it beside the option buttons. Change the following properties of the label control Name lblName , Caption What do you want the computer to do?, Autosize True

    6. Bring an image control and place it on the form. Change the following properties of the image. Stretch True , Picture C:\Program Files\Microsoft Visual Studio\Common\ Graphics\Metafiles\Business\Computer To change the picture property you click the ellipsis at the right of the picture property of the image control. This will open the load picture dialogue box as shown in Figure 1.10

    You Select C: from the drop down list or type C:\ in the file name Textbox and Click open or Press Enter. It should take you to Drive C. Select Program Files Folder or Type C:\Program Files\

  • Design Fundamentals

    15

    In the File name textbox. select Microsoft Visual Studio from the program files folder or type C:\Program Files\Microsoft Visual Studio Select Common from the Microsoft Visual studio folder or type Common in the Microsoft Visual Studio Folder.

    The ampersand character (&) is used to underline(underscore) any character it precedes. The effect of this is that the control with the underscored character can be activated (clicked) at runtime by pressing Alt + [the underscored character]

    FIGURE 1.10

    Visual Basic load picture dialog used to assign bitmap to controls with picture and icon properties

    From the list of folder inside the common folder select graphics or just type graphics in the file name textbox and press enter. This opens the graphics folder. From the graphics folder you can select any folder to get the desired graphics you need. For our purpose, select metafiles or type metafiles in the file name textbox and press enter. Select the Business folder or type business in the file name folder. This opens the business metafiles graphics. You can now select the computer icon or type it and press enter. Change the height and width of the image to 500 each. Your interface should be as shown in Figure 1.11 Observe that the image in Figure 1.11 is different from the image in Figure 1.1 This is because Visual Basic does not come with the image of Figure 1.1. You can get exactly the same image by visiting yahoo and download icons.

    NOTE

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 16

    FIGURE 1.11 Final Design of Shut Down Windows dialog

    Formatting Controls Observe that the command buttons of Figure 1.11 have different sizes and different tops unlike those of Figure 1.1 You can use the properties window to give the objects same height, width and top. To do this click the reference command button and read its height, Width and Top. then change the height, left and top of the other command buttons to that of the reference buttons. Alternatively, you can use the format menu to make the objects have same width, top, left, right etc. To use the format menu, hold down shift and click the object in questions such that the reference object is clicked last .Then click the Format menu and then select Make Same Size and the select Both ( both height and width). The command buttons should now have the height and width of the reference(or the last selected objects). This is a quicker method than using the properties window. Now make the buttons have same top. Let use the first button as reference. Select the three command buttons such that the first button is selected last. Open the Format menu and select Align Top. Now use the Format menu and make the option buttons have same height. The label, option buttons and OK command buttons have the same left. Using the format menu make the option buttons have the same left as the OK command buttons. Do this by selecting the objects in questions such that the OK command button is selected last and then Align Left using the format menu. Lastly, select the option buttons and use the Format menu to Make Equal Vertical Spacing(i.e give them equal vertical spacing). If the vertical spacing is too small, you can increase it or decrease it using the format menu. Also you can repeat the same for the command buttons and make them have equal horizontal spacing. Your design should be very much similar to the shut down windows dialog of Figure 1.1

  • Design Fundamentals

    17

    Designing Windows Interface Using Visual Basic Controls We are going to continue our exercise on user interface design by designing some of the dialog boxes used in Microsoft application and other software developed by third parties. This will help to introduce the controls step by step and also get us used to how to name the controls and functions of the controls.

    Introducing the Combo Box And Frame

    FIGURE 1.12 Visual Basic Print Setup Dialog Box

    Figure 1.12 shows the Print Setup dialog box in Visual Basic. Here you would be introduced to two new controls: these are the frames and the combo boxes. The combo box is a multipurpose control which is used to display textual information to users in a drop down list. Depending on property settings, this information can be made editable. The drop down list provides a way for the user to select different options from the combo box. In this regard it functions like a group of option buttons. On the other hand, the frame is used to group related information together.

    Other controls on the form are the command buttons, labels, option buttons and image that you have used while designing the windows Shut down dialog. To start designing the interface, we choose an appropriate height and width of the form then follow the steps outlined below

    1. Use the properties window to change the following properties of the form Name frmPrintSetUp Caption Print Set Up

    Whats ThisButton Command Button Combo Box Label

    Frame Image

    Option Button

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 18 BorderStyle 4(fixed Toolwindows) WhatThisButton True Height Use your Judgement Width Use your Judgement

    2. Draw three instances of the frames on the form and configure them as in Figure 1.12 Set the following properties for the frame. Top Frame: Name - fraPrinter Caption Printer Bottom Left frame : Name - fraPaper Caption Paper Bottom Right Frame Name - fraOrientation Caption - Orientation

    3. Draw two instances of the command button control on the form(not frame) and another instance of the command button control on the top frame. Arrange the command button an in Figure1.12. Give the command buttons appropriate names and captions(make use of ampersand)

    4. Use the ToolTip to identify a combo box and draw an instance of it on the first frame and two instances of it on the bottom left frame. Set the following properties for the combo boxes Combo Box #1: Name cboPrinterName : Text: HP Laser Jet 1100 Style - 2 Combo Box #2: Name - cboPaperSize : Text: A4 210 x 297 mm Style - 2 Combo Box #3: Name - cboPaperSource : Text: - Upper Tray Style - 2

    5. Draw two instances of the option button control on bottom right frame and set the following properties. Option Button #1 : Name optPortrait Caption &Portrait Value True Option Button #2 : Name - optLandScape Caption &Landscape

    6. Draw an instance of the image control on the bottom left frame. Use this path to locate the image: C:\ProgramFiles\MicrosoftVisualStudio\Common\Graphics\Icons\Writing \Portrait

    7. Draw eight instances of the label control on the top large frame and two other instances of the label on the bottom left frame. Give the label captions as shown in the figure and names to reflect their caption.

    8. Use the format menu to align the controls as shown in Figure 1.12. Also remember to make related controls same size both as in Figure 1.12

    9. Press F5, your Design is now being viewed at Run Time

  • Design Fundamentals

    19

    Introducing The Line control, Checkbox control and Picture box control We are going to introduce three new controls from the toolbox. These are controls in the Paste Special dialog box of Microsoft Excel. The Controls on the Paste Special Dialog box are Line control, Label control, option button control , command button control and Check Box control. The New controls on the form are the Checkbox Control, the Line control and the Picture box control. See Figure 1.13

    FIGURE 1.13 Visual Basic Controls used in MS Excel Paste Special dialog box

    The Line control is only used for decorative purposes. It is used to separate a group of information in much the same way as the frame. Unlike the frame that has a caption, the line control does not have a caption and as such a label control has to be placed by it to describe the purpose of the grouping. The check box control is like an on/off control used to turn an action (or a feature/setting) on or off. The picture box control is a container control used to group related controls together just like the frame. Your ability to see the frame/picture box depends on the border style setting. If it is set to 0 (i.e. no borders) it means no borders, the frame will just appear on the background and can not be seen at run time. You confirm the presence of picture box if two option buttons have a value of true in the same Form. In Figure 1.13, there is atleast a picture box(or frame) used to separate the option buttons. We now attempt to design the interface of Figure 1.13. We first of all design the form. You should be familiar with how to design the form and bring other controls apart from the three new controls. Use the following steps to design the interface. 1. Design the form as usual 2. Use the ToolTip to identify the picture box control in the toolbox. Then draw two

    instances of the picture box control on the form. Set the following properties for the picture box control

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 203. Draw seven instances of the option button control on the first picture box and five

    instances of the option button on the second picture box. Give the option buttons captions as in the figure 1.13 and names to reflect their captions.

    4. Set the value property of the first option button in each group to true 5. Use the tooltip to identify the line control and draw three instances of it on the form

    and arrange them on the form as shown in Figure 1.13 6. Give the line controls names using standard naming conventions for lines 7. Draw two instances of the label control on the form and place them beside the first

    two lines. Give them caption of Paste and Operation and Names of lblPaste and lblOperation respectively

    8. Use the tooltip to identify the checkbox control and draw two instances of the control on the form. Use the properties window to set the following properties of the checkbox control. CheckBox #1 Name: chkSkipBlank Caption : Skip Blank Value :1 CheckBox #2 Name: chkTranspose Caption: Transpose

    9. Draw three instances of the command button control on the form and arrange them as shown in Figure 1.13 Set the following properties for the command button. Command Button #1: Name: chkOK , Caption : OK, Default : True Command Button #2: Name: chkCancel, Caption : Cancel , Cancel True Command Button #3: Name: chkHelp, Caption: &Paste Link

    10. Use the format menu to set appropriate alignment for the controls and make same size if applicable.

    Introducing The Text Box and the Vertical Scroll Bar The text box control is one of the most important controls in the tool box. The text box is used to display textual information just like the label control. However, unlike the label, the information displayed by the textbox can be edited. This makes the textbox a general purpose input/output control. In most applications you find the textbox as the input control with the label at the left hand side of the textbox describing the input expected from the user. Figure 1.14 shows a data form dialog box from Microsoft Excel with five text boxes and one vertical scroll Bar. Other controls on the form are the seven command button controls and one label. The vertical scroll bar is also used to display information to the user but this information are usually pre-programmed information and can not be edited. Most times the vertical scroll bar is used to enter input in text boxes and labels. Some important properties of the vertical scroll bar are the value property, the Minchange, the Maxchange, min and max properties. Use the tool tip to identify the vertical scrollbar in the tool box and draw an instance of it on the form. Use the format menu to configure your interface as in Figure 1.14

  • Design Fundamentals

    21

    FIGURE 1.14 Visual Basic controls used in MS Excel Data Form dialog

    Introducing the horizontal scroll bar Instances of the horizontal scroll bar in office applications are not common. However, an instance of it was used in MS Office format picture dialog (See Figure Q1.4b )

    Introducing the list box

    The list box is a general-purpose control used in windows application. It is similar to the combo box in that it holds a list of options. Unlike, the combo box the list box shows the items in the list. If the items in the list are more than the height of the list, a scrollbar comes up which is used to scroll up or down the list so that selection can be made. Figure 1.15a and b show the list box control. The scroll bar can be horizontal or vertical depending on the setting of the column property. If it is set to 0 it means you want the scroll bar to be vertical and if it is set to 1 you want the scroll bar to be horizontal. Generally, setting the column property to n (where n >=1) causes the scroll bar to be horizontal with the items in n columns. The load picture dialog shown in Figure 1.15a contains a list box with columns > 0.

    FIGURE 1.15 (a) MS Excel Date and Time dialog showing an instance of the list box control (unchecked style)

    (a)

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 22

    FIGURE 1.15 (b) MS Excel Add-Ins Dialogue showing another instance of a list box control (checked style)

    We can now create an interface similar to that of figure 1.15(b). Use the tool tip property to identify a list box and draw an instance of it on the form. Design the form to get the appearance of Figure 1.15a. and format your controls appropriately. Use table 1.1 as your guide when naming the controls. Give the list box the name: lstFormats. Open the drop down list in the List property of the list box and type the entries in the list box of Figure 1.15 into your list boxs list property. Press Ctrl + Enter after each entry

    FIGURE 1.16

    Properties Window showing the List Property of the ListBox.

    (b)

  • Design Fundamentals

    23

    Introducing the Data Control

    Figure 1.17 shows a Visual Basic form showing the data control. This control is only common in MS Access application.

    FIGURE 1.17 User application showing the Visual Basic data control

    Other intrinsic controls So far we have considered thirteen out of the twenty intrinsic controls found in the tool box. Controls that have not been considered are the horizontal scroll bar, timer, shape, OLE, and the file controls(DirListBox,FileListBox and DriveListBox). This controls are as shown in Figure 1.18

    Data control

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 24

    FIGURE 1.18 User form showing other intrinsic controls

    Interface Design with Windows Common Controls. We now consider some controls collectively called Windows Common controls. They are not usually loaded in the tool box by default but can only be added to the toolbox from components. Common examples of the windows common control are the toolbar, statusbar , updown control ,and the tab strip control.

    FIGURE 1.19 Windows common control in MS Excel options dialog

    HorizontalScrollBar DirListBox

    Shape Control OLE (Object Linking and Embedding)

    FileListBox

    DriveListBox

    Tab Strip Up down control

  • Design Fundamentals

    25

    The Updown control is commonly attached to the textbox control and it is used to increase/decrease the value(text) in the text box. Figure 1.19 shows the updown control in the edit tab of the option dialog in Microsoft PowerPoint 97. Here, the updown control was used to increase/decrease the maximum number of undos in the application. The value in the text box increases by one(or any number depending on the setting in the properties window) when the up arrow is clicked. The tab strip control on the other hand is used to divide the form into different sections. We now demonstrate how to use the updown control and the tab strip control.

    Adding the Microsoft Windows Common Control to the ToolBox. The steps used to add the Microsoft windows common controls to the toolbox are as follows 1. Select [Components] from the [Project] menu. The component dialog box appears as shown in Figure 1.20

    FIGURE 1.20 Visual Basic components dialog used commonly to add non-standard control to the tool box

    Scroll down to Microsoft windows common control and place check marks on all the Microsoft windows common controls and click Apply. All the Microsoft windows common controls are then added to the toolbox. You close the dialog by clicking Close. The toolbox shown in Figure 1.21 shows a fully loaded tool box with default controls and windows common controls. With the aid of tool tip you can identify a Microsoft windows common control in the tool box. Among the popular Microsoft windows common controls are the tab strip control, updown control, the tool bar control, status bar control, image list and the tree view controls. Others are the date time picker, month view and the progress bar..

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 26

    FIGURE 1.21 Tool Box containing standard controls and windows common controls added from controls components

    We now outline how to add and configure the Microsoft windows common control in the Form.

    Configuring the Tabstrip Control

    Figure 1.22a and b show the tab strip control on the Form. In Figure 1.22a the tab strip control is smaller while it was adjusted to get the orientation of Figure 1.22b. Usually, we use the property pages to change the properties of windows common controls instead of using properties window.

    FIGURE 1.22 (a) User form showing the tap strip control

    FIGURE 1.21 (b) User form showing the tap strip control in expanded form

    (b)

    (c)

  • Design Fundamentals

    27

    The Property Pages Properties of windows common controls are usually changed using property pages. To activate the property pages, right click anywhere on the tab strip control and select properties from the pop up menu. This opens the property pages of the tab strip control as shown in Figure 1.23. To add tabs to the tab strip control , activate the tabs tab and type View in caption text box and click Apply. Insert more tabs and give the created tabs appropriate captions. Click OK when done.

    FIGURE 1.23 Property pages of tab strip control

    The properties window is used to change properties of standard controls while the property pages is used to change the properties of most windows common controls. You invoke the property pages by right-clicking a control and selecting properties from the popup menu.

    Configuring the UpDown Control

    The Updown control is part of the Microsoft windows common controls. Configuring the updown control for use is simple relative to the tab strip control and other windows common control. It is commonly used to increment values in text boxes but could be used in other controls apart from the text box. Using the Updown control involves drawing the control on the form and setting the following properties

    NOTE

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 28

    1. Buddycontrol - Specifies the name of the textbox(or control) in which the control is to be attached

    2. Buddy Specifies the name of the property in which the control is to modify 3. Max Specifies the upper bound of the scroll range 4. Min Specifies the lower bound of the scroll range 5. Orientation Specifies the orientation of the control. This could be vertical or

    horizontal.

    Configuring the Toolbar Control The tool bar control is very common in windows applications and is used to provide quick access to the commonly used functions in a program. Most applications developed today have the standard toolbar with familiar images used for related functions. Figure 1.24 shows the standard toolbar of Visual Basic.

    Figure 1.24 Visual Basic standard Tool bar

    The tool bar control is configured in the same way the tab strip was configured using the property pages. However, unlike the tab strip with tabs, the tool bar control has buttons. The buttons can carry texts or images or both. Using tool tip, identify the tool bar control on the tool box and draw an instance of it on the form. Open the property pages of the toolbar control by right clicking the instance of the tool bar control and select properties from the pop up menu. Click the Buttons tab and then click the Insert Button command button to add buttons to the tool bar control(see Figure 1.25). Provisions are made so that you can give captions, keys, tags and images to the buttons just created.

    You add images to windows common control using the imagelist control. The imagelist control is a windows common control that holds images for other controls. It is invisible at runtime

    NOTE

  • Design Fundamentals

    29

    FIGURE 1.25 Property pages of the toolbar control

    Adding Images to the Toolbar Control.

    Images are added to the tool bar control using the image list control. This control is invisible at run time and is a repository for the tool bar images. The image list control is part of the windows common control group. You add the windows common control to your application by drawing an instance of it on the form. Images are added to the image list control the same way buttons are added to the tool bar control or tabs are added to the tab strip control. As usual, open the image list property pages by right clicking the image list control and selecting properties from the pop up menu. Use the path used to add images to the image control to add images to the image list control. Close the property pages of the image list control and then open that of a toolbar control on the form. Under the General tab, select the image list combo and set Buttons tab, type 1 on the image textbox and click apply so that image #1 in the image list control would be added to the present tool bar button. Click the index up down control to move to the next(or previous) button so that you can assign appropriate images to the button. Repeat this until you have added images to all the buttons.

    A tool bar control may have both caption and images though one is sufficient to define a button. Most of the tool bar controls used in windows explorer have buttons and captions. In the standard tool bar as well as other tool bars used in many applications, only images are used to communicate with the user. Most developers use the tool bar just as the command buttons are used, in cases like these the tool bar may have only captions

    NOTE

  • Visual Basic 6.0 for Engineers and Scientists:ThankGod Egbe 30

    A Closer Look at the Toolbar Buttons

    You configure the tool bar by manipulating the property pages of the tool bar control. Essential properties of the buttons are the key property, caption property, image property, tag property and the style property . The key property is used to specify the name of each button while the image property is used to retrieve an image from the image list control associated with the tool bar control. The style of a tool bar button defines the appearance and behavior of the button. Generally, in all tool bar controls, there are six styles supported by the buttons. These include

    1. Default

    2. Check 3. Button-Group 4. Separator 5. PlaceHolder 6. DropDown

    Default: This is the standard style for most buttons in the tool bar control. They are used to launch tasks like opening dialog boxes and executing actions. Examples of buttons with default buttons style are the save button, open button, cut, copy etc.(see Figure 1.26)

    Check: The check style is like an on/off style used to turn a feature(or option) on or off. Examples of the check button style are the bold, underline and italics buttons in the formatting tool bar of MS Word. Here, a buttons appearance indicates the status of the option. The status of the bold button is used to indicate whether bold option has been turned on or not. This also applies to the underline and italics buttons.(see Figure 1.26)

    PlaceHolder: Buttons with this style are commonly used to host other controls like command buttons and combo boxes. Example is the font Size and font name button of the formatting tool bar in MS office applications.

    ButtonGroup: This is similar to the check style in that it indicates the status of an option. However, it applies to more than two options unlike the check style. Here the options are in a mutually exclusive group. An example is the alignment and bullets/numbering buttons.

    Separator: Buttons with separator style are used to separate one button(usually of another style) from the other.

  • Design Fundamentals

    31

    DropDown: This creates a drop down menu which is shown when the button is pressed. Examples are the undo button and the autoshape buttons of Microsoft Office standard and drawing tool bar respectively. Optional properties needed to be set for the tool bar buttons are the tool tip and the caption properties. The tool tip text specifies the text that appears when the mouse pointer rests momentarily on the buttons whi


Recommended