Docsity

Visual Basic 6.0, Notas de estudo de Informática

Toggle sidebar
bg1
About the Author
Chuck Easttom is a software developer and author. You can get to his homepage at
www.chuckeasttom.com He currently holds over 20 different computer industry
certifications and is continually working on more. He lives with his wife Misty, his son
AJ and their two cats Sagan and Monte.
In his spare time (when he is not working, researching, or reading a computer book)
Chuck likes to play chess, read voraciously and is a very bad golfer. You can find him on
his home web site (that also has a pretty decent VB web site) at www.chuckeasttom.com
Here is a picture of the author at work at his computer (Well Sort of…)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Visual Basic 6.0 e outras Notas de estudo em PDF para Informática, somente na Docsity!

About the Author Chuck Easttom is a software developer and author. You can get to his homepage at www.chuckeasttom.com He currently holds over 20 different computer industry certifications and is continually working on more. He lives with his wife Misty, his son AJ and their two cats Sagan and Monte. In his spare time (when he is not working, researching, or reading a computer book) Chuck likes to play chess, read voraciously and is a very bad golfer. You can find him on his home web site (that also has a pretty decent VB web site) at www.chuckeasttom.com Here is a picture of the author at work at his computer (Well Sort of…)

Introduction This book is meant for the beginner. Its primary purpose is to get you up and writing programs in the shortest possible time. For that reason I take a very practical approach (thus the title). My goal is that you be able to function as a competent Visual Basic programmer in at the end of this book, and write useful programs. That means that I don’t go in depth into theory, or into advanced topics. However this book should provide you with the ground work necessary to move on to more advanced programming topics. While some of the code examples and lessons can be accomplished with Visual Basic 4, 5, or 6, much will require Visual Basic 6.0. When you see the icon It denotes a tip that can save you a lot of cursing at your code! New in this edition: Several things are new in this 3rd^ edition. The first, and most obvious, are the increased examples. More examples added to several chapters. Then we have the chapter on object orientation reworked and clarified. Finally a few completely new chapters have been added including a chapter on building your own Active X controls and building your own Active X dll’s. About 30 extra pages are in this edition There have also been a few formatting changes. All the examples are now highlighted so they can more easily be found.

Chapter Objectives:

 Understand Controls and form  Understand Events  Understand Control Properties  Become familiar with terminology  Become comfortable with the development environment

Chapter 1: Introduction

This chapter is designed to introduce you to the essentials of Visual Basic. At the end of this chapter, you should have a basic understanding of forms, controls, properties, and events. You should also be getting comfortable with the programming interface and know how to move around in the tool bar, tool box, and drop down menus. When you first launch Visual Basic you will be confronted with a screen that asks you what type of application you wish to make. For now we will always start with the first option “Standard EXE”. This denotes a Standard Executable or program. When you select this, you will then see the following screen: This is referred to as the IDE or Integrated Development Environment. The top of the screen has the drop down menus and the tool bar. These contain various functions you will use when programming. Functions such as saving, running, and debugging your program. The tool bar looks like this:

The facets of a control that you can change are called properties and are found in the properties box. You can access the properties box by clicking on “View” on the drop down menu at the top of your screen and then clicking on properties, or just by pressing the F4 key. It is important to realize that the properties window will display properties for whatever control currently has focus. You change focus by clicking once on the control you wish to have the focus. The properties are all listed in alphabetical order with the exception of the name property that is listed first. It is vital that you realize that the name property is what the computer will call your control, not what the user will see. The user will see the caption property (for those controls that have one.) Here is an example of a property window: You can simply drag a control from your toolbox and then place it on the form. You then drag it to any spot on the form you want to place it. You can also simply double click on a control in the tool box and it will appear in the center of the form. All controls can also be stretched to alter their size. After you have placed the control on the form you can then proceed to set the properties in the properties window. Below is a picture of what a form looks like:

Common Controls and their properties Some controls are used more often than others. The form is the most basic control. It is the container in which you place all the other controls. It is also what your user will see. The most important properties you will set for a form are its name (What the computer will call it); its caption (What appears in the title bar that the user will see), and its back color (what color do you want the background to be). You may also wish to set its border style (this determines if it can be resized by the user or not). Below are listed some other common controls and the properties you will be most concerned with: Text box : This control allows the end user to enter data. You will want to set its name (What the computer will call it), its text (the default text that initially appears when you run the program), and possibly its font if you wish a different font in the text box. You can also enter something into the text property as a default text. Finally you may want to set its max length property. This determines the maximum number of characters a user can type into the text box. You can also set its password character property to denote a symbol to show in lieu of passwords. The multi-line and scroll bar properties allow you to have a text box that can handle several lines of text. Label : Labels are used to display information to the end user, but the end user cannot change any information in the label. The properties you will set in this control are the name (What the computer will call it), its caption (what will the user see), and its font. Command Button : Command buttons allow the user to click a button. A lot of the code that you write will be associated with command buttons. The properties you are most interested in are: its name (What the computer will call it), its caption (What the user will see), its style (Will your button have a text caption or a picture, if you choose the graphical style you must set the caption equal to nothing and select a picture in the picture property) , and its font. List Box : A list box simply allows you to list items for a user to select from. The property you are concerned with is the font. The items are listed using code and will be discussed later.

A lot of the programming you do will be concerned with using code to alter the properties of some control. The way you do this is you write the control name you wish to alter followed by a period then the name of the property you want to change. A generic example of this is: Controlname. Property = “whatever” A specific example is: Text1.text = “Howdy” Form1.caption = “This is my form” Picturebox1.image = “c:\folder\image.bmp” Data1.recordsource = sSQL Label1.caption = “This is a label” Tip: If your code referencing a property does not work, it is most likely because you misspelled either the control or the property name. Using this basic technique, you can write code to change the properties of any control you want. This is important to remember since much of the code you write in Visual Basic is concerned with altering the properties of various controls. Example 1- Lets try a simple project right now. Start up Visual Basic (If you are using VB 5 or 6 then choose “standard.exe”). It should start off with one form in view. If you don’t have the tool box in view, then you can go to the drop down ;menu and select “View” then select “toolbox”. Now on that form place 1 list box, 1 text box, and 1 command button. In the properties window (either select “view” and “ properties” or press the f4 key) change the command buttons caption to “add text”. Now if you will double click on the command button you will get its code window. Now go to the “click” event. In it place this code:

List1.additem text1.text Now run your program. You can do this by clicking on the run button in the toolbar, it looks like a play button on a CD player or by using the f5 key. You will be prompted to save it and give it a name. Now every time you click on the command button, whatever you have typed into the text box will now be added to the list box! It is important to note that every control has a default property. That is essentially its primary or most important property. If you refer to a control and do not specify the property you wish to refer to, Visual Basic will assume you intend to refer to the default property. For example, you could write the code above as: Text1 = “Howdy” Since the text property is the default property for the text box. However, in this book we will always explicitly state the property we are referring to. Note: An event is just a function that the user calls by doing some action. Glossary: The following is a list of standard Visual Basic and programming terms that you will need to be familiar with. In programming, proper nomenclature (or naming) is simply vital. You cannot understand instructions and you cannot communicate with other programmers without an understanding of proper nomenclature Project : This is all the files in your program. It includes forms, code modules, class modules, etc. Forms: This is the control on which you place other controls and design the User Interface. Controls : These are the various objects or templates that Visual Basic has for you to use in your program. Examples are the textbox, command button, list box, combo box, and picture box. Code: This is the actual programming that you write. Bug : an error in your program. Debug : to remove errors in your program

Save Form: This works just like the Save project option, only it saves the specific form that currently has focus. Save Form As : This option will allow you to save a specific form under a name of your choosing and in the folder/directory that you select. Tip: As with the “Save Project” As option you want to make sure you are saving these forms (and any other project files) to the same location Print: This allows you to print the source code for your project. Make exe: This makes an executable program from your source code. Exit : This will exit Visual Basic Edit : Undo: This option allows you to undo whatever action you last performed. Tip: The Undo option exists in most windows programs. If you do something you wish you had not done, don’t freak out. Simply go to “Undo” Redo : This will let you redo whatever action you last used undo on. Cut : This will cut the code or the control you have selected and place it on the clipboard. Copy: This works much like the cut command except that the original is still in place. Paste: This command lets you paste something that you have previously copied or cut. Find: You can enter a key word and search all your project for it. Find Next : It finds the next instance of whatever you last used find for. Replace : This will go through your code replacing one word or phrase with another.

View: Tip: I always hear at least one student in panic yelling “I can’t find (project/form/code module/etc.). You probably just need to go to “View” and select that object to view. Code : This will give you the underlying code for any object that currently has focus. Object : This lets you view the object of the code you are currently viewing. Project Explorer : Selecting this option will give you the window with a listing of all files in your project. It looks like this Properties Window : This shows you the properties window. It will usually appear to the right hand side of the IDE. You can get to it using the drop down menu option

Add Module : This option lets you add in a blank code module. Add Control : This is another way to add in a control Remove : This will remove whatever currently has focus, be it a form, control, or code module. Components : Visual Basic has many more controls than you see in your toolbox. This option lets you select which controls you want to see in your toolbox. If you select this option you will see a window like the one below from which you can choose which controls you want available for your project: Run: This window simply lets you either run your program, pause it, or stop it. Help: This will let you ask questions and look up topics from Microsoft help. Tip: Microsoft has included an awesome help file. If you ever see a keyword you don’t understand look it up in the help file. You can learn a lot from the help file. Each version of VB seems to have an even better help file. VB 6.0 has an incredible help file.

Short Cut Keys: The following are a few of the most commonly used shortcut keys. You may want to memorize them. F7: View the Code window of an object F4: View an objects property window F3: Find F5: Run CTRL Z: Undo CTRL C: Copy CTRL X: Cut CTRL V: Paste Short Cut: You can also right click on your mouse and a pop up window will display the more commonly used functions. The window looks like this:

When a user causes any event to occur then the code that you have placed inside the subroutine for that event will be initiated. If there is no code inside the events subroutine then nothing will occur. All events begin with no code. You must place the code in them. Each control will have several events you can choose from. Each event corresponds to an action that the user takes or to something that happens in the program. Below are some common events found in many controls and what they mean: Click : This event is triggered whenever a user clicks that control. (this is most often used in command buttons, list boxes, and combo boxes.) Tip: A lot of your code will be in click events. However, when you first open up a command button the first event displayed is the change event. I have seen lots of beginners put some nice code in an event and the spend hours trying to find out why it won’t work, only to find they put the code in the wrong event. Double Click : This event is triggered whenever a user double clicks on that control. Mouse Move : When a user passes the mouse over a control this event is triggered. Get Focus : This event is triggered when you first give focus to a control either by clicking on it or by tabbing to it. Lost Focus : This event is triggered when you have had focus on a control and then attempt to move focus from that control. Load : This event is only in the form control and it is initiated when the form is first loaded. Naming Conventions: Microsoft has established some naming conventions you should use when naming controls. This makes for a more uniform code that is easy to read. By default each of your controls will have a name like: text1, label1, command4, etc. You must go to the “name” property of each control to change its name. Naming conventions simply make it much easier for another programmer to see what you are doing. Below is a chart of some Microsoft naming conventions: Control Type Naming Convention

Text Box TxtWhatever Command Button CmdWhatever Label LblWhatever Panel PnlWhatever Frame FraWhatever Combo Box CmboWhatever List Box LstWhatever Using these naming conventions will make reading your code much easier. Yes I realize that it is easier and faster to just leave your controls with the default name. And while you are working on simple projects it won’t really matter. But when you have to work on real world applications do you really want to spend time trying to figure out that text1 refers to the account number when you could simply have named it txtAccountNum to start with? Example 1- The following is a very simple program that will allow you to build an image viewer. The program is simple but lets you see how controls and their properties can be used. First, start a new project with a single form on it. On that form you will want to place a drive list box directory list box