Learn visual basic 6.0 pdf


















Visual Basic r 6 made easy is written by the author of the popular online Visual Basic tutorial at www. This book presents the basics of Visual Basic programming using direct and simple language so that you can learn Visual Basic 6 programming easily and quickly. You will be able to build Visual Basic applications within a short time.

Visual Basic Books. Net Tutorial For Beginners. Microsoft Excel VBA. Tag: Programming Posted on By anonymous. You will be asked if you want to create a control array. Answer Yes. It is perfectly acceptable to draw frames within other frames.

Option buttons within a frame work as a group, independently of option buttons in other frames. Option buttons on the form, and not in frames, work as another independent group.

That is, the form is itself a frame by default. We'll see this in the next example. That is, a control array of option buttons does not work as an independent group just because it is a control array. It would only work as a group if it were the only group of option buttons within a frame or on the form. So, remember physical location, and physical location only, dictates independent operation of option button groups.

We'll build a form where a pizza order can be entered by simply clicking on check boxes and option buttons. Draw three frames. In the first, draw three option buttons, in the second, draw two option buttons, and in the third, draw six check boxes. Draw two option buttons on the form. Add two command buttons. Make things look something like this. Set the properties of the form and each control. Exploring the Visual Basic Toolbox 5.

This initializes the pizza size, crust, and eating location. Attach this code to the three option button array Click events. Next, the code cycles through the six topping check boxes and adds any checked information to the message. The code then displays the pizza order in a message box.

Get the application working. Notice how the different selection buttons work in their individual groups. If you have time, try these modifications: A. Add a new program button that resets the order form to the initial default values. If the number of items exceeds the number that can be displayed, a scroll bar is automatically added. List Array of items in list box. ListCount Number of items in list. ListIndex The number of the most recently selected item in list.

MultiSelect Controls how items may be selected 0-no multiple selection allowed, 1-multiple selection allowed, 2- group selection allowed. Selected Array with elements set equal to True or False, depending on whether corresponding list item is selected. Sorted True means items are sorted in 'Ascii' order, else items appear in order added. Text Text of most recently selected item. DblClick Event triggered when item in list is double-clicked.

Primary way used to process selection. Clear Removes all items from list box. RemoveItem Removes item from list box, as identified by index of item to remove. Examples lstExample. AddItem "This is an added item" ' adds text string to list lstExample. Clear ' clears the list box lstExample. RemoveItem 4 ' removes lstExample.

It's always a good idea to Clear a list box before initializing it. The differences are a combo box includes a text box on top of a list box and only allows selection of one item. In some cases, the user can type in an alternate response. List Array of items in list box portion. Style Selects the combo box form. Examples cboExample. AddItem "This is an added item" ' adds text string to list cboExample. Clear ' clears the combo box cboExample. RemoveItem 4 ' removes cboExample.

In this example, you select a destination city, a seat location, and a meal preference for airline passengers. Place a list box, two combo boxes, three label boxes and two command buttons on the form.

The form should appear similar to this: 3. Clear lstCities. AddItem "San Diego" lstCities. AddItem "Los Angeles" lstCities. AddItem "Orange County" lstCities. AddItem "Ontario" lstCities. AddItem "Bakersfield" lstCities. AddItem "Oakland" lstCities. AddItem "Sacramento" lstCities. AddItem "San Jose" lstCities. AddItem "San Francisco" lstCities. AddItem "Eureka" lstCities. AddItem "Eugene" lstCities. AddItem "Portland" lstCities. AddItem "Spokane" lstCities.

AddItem "Seattle" lstCities. AddItem "Aisle" cboSeat. AddItem "Middle" cboSeat. AddItem "Window" cboSeat. AddItem "Chicken" cboMeal. AddItem "Mystery Meat" cboMeal. AddItem "Kosher" cboMeal. AddItem "Vegetarian" cboMeal. AddItem "Fruit Plate" cboMeal. Run the application. The required input information is: 1. Name 2. Age 3. City of Residence 4.

Sex Male or Female 5. Athletic Level Extreme, Advanced, Intermediate, or Beginner Set up the screen so that only the Name and Age use text boxes and, perhaps, City use a combo box need to be typed; all other inputs should be set with check boxes and option buttons.

When a screen of information is complete, display the summarized profile in a message box. AddItem "Seattle" cboCity. AddItem "Bellevue" cboCity. AddItem "Kirkland" cboCity. AddItem "Everett" cboCity. AddItem "Mercer Island" cboCity. AddItem "Renton" cboCity. AddItem "Issaquah" cboCity. AddItem "Kent" cboCity. AddItem "Bothell" cboCity. AddItem "Tukwila" cboCity. AddItem "West Seattle" cboCity.

AddItem "Edmonds" cboCity. AddItem "Tacoma" cboCity. AddItem "Federal Way" cboCity. AddItem "Burien" cboCity. AddItem "SeaTac" cboCity. We will look at some drawing tools, scroll bars, and tools that allow direct interacti on with drives, directories, and files. In the examples, try to do as much of the building and programming of the applications you can with minimal reference to the notes. This will help you build your programming skills.

And, with this introduction, we need to discuss the idea of display layers. A form's display is actually made up of three layers as sketched below. All information displayed directly on the form by printing or drawing with graphics methods appears on the bottom-layer. Information from label boxes, image boxes, line tools, and shape tools, appears on the middle-layer.

And, all other objects are displayed on the top-layer. For example, text printed on the form would be hidden by a command button placed on top of it. Things drawn with the shape tool are covered by all controls except the image box. That is, say two command buttons are in the same area of a form - which one lies on top of which one?

The order in which objects in the same layer overlay each other is called the Z-order. This order is first established when you draw the form. Items d rawn last lie over items drawn earlier. Once drawn, however, the Z-order can be modified by clicking on the desired object and choosing Bring to Front from Visual Basic's Edit menu. The Send to Back command has the opposite effect.

Note these two commands only work within a layer; middle-layer objects will always appear behind top-layer objects and lower layer objects will always appear behind middle-layer objects. Together with the shape tool discussed next, you can use this tool to 'dress up' your application. BorderStyle Determines the line 'shape'. Lines can be transparent, solid, dashed, dotted, and combinations.

BorderWidth Determines line width. Colors can be used and various fill patterns are available. BackStyle Determines whether the background is transparent or opaque. BorderColor Determines the color of the shape's outline.

BorderStyle Determines the style of the shape's outline. The border can be transparent, solid, dashed, dotted, and combinations. BorderWidth Determines the width of the shape border line. FillColor Defines the interior color of the shape. FillStyle Determines the interior pattern of a shape. Some choices are: solid, transparent, cross, etc.

Shape Determines whether the shape is a square, rectangle, circle, or some other choice. This is a good feature in that you usually use shapes to contain a group of control objects and you'd want them to lie on top of the shape. Scroll bars provide an intuitive way to move through a list of information and make great input devices. Those areas are: End arrow Scroll box thumb Bar area Clicking an end arrow increments the scroll box a small amount, clicking the bar area increments the scroll box a large amount, and dragging the scroll box thumb provides continuous motion.

Using the properties of scroll bars, we can completely specify how one works. The scroll box position is the only output information from a scroll bar. Max The value of the horizontal scroll bar at the far right and the value of the vertical scroll bar at the bottom. Can range from , to 32, Min The other extreme value - the horizontal scroll bar at the left and the vertical scroll bar at the top. SmallChange The increment added to or subtracted from the scroll bar Value property when either of the scroll arrows is clicked.

Value The current position of the scroll box thumb within the scroll bar. If you set this in code, Visual Basic moves the scroll box to the proper position. Note that although the extreme values are called Min and Max, they do not necessarily represent minimum and maximum values. There is nothing to keep the Min value from being greater than the Max value. In fact, with vertical scroll bars, this is the usual case. Visual Basic automatically adjusts the sign on the SmallChange and LargeChange properties to insure proper movement of the scroll box from one extreme to the other.

If you ever change the Value, Min, or Max properties in code, make sure Value is at all times between Min and Max or and the program will stop with an error message. Use this event to retrieve the Value property after any changes in the scroll bar. Scroll Event triggered continuously whenever the scroll box is being moved.

In this project, we convert temperatures in degrees Fahrenheit set using a scroll bar to degrees Celsius. As mentioned in the Review and Preview section, you should try to build this application with minimal reference to the notes.

To that end, let's look at the project specifications. Temperature Conversion Application Specifications The application should have a scroll bar which adjusts temperature in degrees Fahrenheit from some reasonable minimum to some maximum.

As the user changes the scroll bar value, both the Fahrenheit temperature and Celsius temperature you have to calculate this in integer format should be displayed. To change numeric information to strings for display in label or text boxes, use the Str or Format function.

Try to build as much of the application as possible before looking at my approach. Try incorporating lines and shapes into your application if you can. Place a shape, a vertical scroll bar, four labels, and a command button on the form. Put the scroll bar within the shape - since it is in the top-layer of the form, it will lie in the shape. It should resemble this: Shape1 2. Put this code in the general declarations of your code window.

Attach the following code to the scroll bar Scroll event. Value lblTempF. More Exploration of the Visual Basic Toolbox 5. Attach the following code to the scroll bar Change event. This is almost always the case when using scroll bars.

Give the program a try. Make sure it provides correct information at obvious points. For example, 32 F better always be the same as 0 C! Other things to try: A. Can you find a point where Fahrenheit temperature equals Celsius temperature? If you don't know this off the top of your head, it's obvious you've never lived in extremely cold climates. I've actually witnessed one of those bank temperature signs flashing degrees F and degrees C and seeing the same number!

Ever wonder why body temperature is that odd figure of Can your new application give you some insight to an answer to this question? It might be interesting to determine how wind affects perceived temperature - the wind chill. Add a second scroll bar to input wind speed and display both the actual and wind adjusted temperatures. You would have to do some research to find the mathematics behind wind chill computations. This is not a trivial extension of the application. It is best suited for dynamic environments - for example, when doing animation.

They behave very much like small forms within a form, possessing most of the same properties as a form. Font Sets the font size, style, and size of any printing done in the picture box.

Picture Establishes the graphics file to display in the picture box. DblClick Triggered when a picture box is double-clicked. Print Prints information to the picture box. Examples picExample. Cls ' clears the box picExample picExample. It is used to set the Picture property of a picture box at run-time.

Example picExample. The argument in the LoadPicture function must be a legal, complete path and file name, else your program will stop with an error message. Usually has a. Appears in original size. Icon A special type of bitmap file of maximum 32 x 32 size. Has a. Metafile A file that stores an image as a collection of graphical objects lines, circles, polygons rather than pixels.

Metafiles preserve an image more accurately than bitmaps when resized. Resizes itself to fit the picture box area. It is popular on the Internet. It supports up to colors and is popular on the Internet. Image boxes are more suited for static situations - that is, cases where no modifications will be done to the displayed graphics.

Image box graphics can be resized by using the Stretch property. Stretch If False, the image box resizes itself to fit the graphic. If True, the graphic resizes to fit the control area. DblClick Triggered when a image box is double-clicked.

It is used in exactly the same manner as the picture box uses it. And image boxes can load the same file types: bitmap. Quick Example: Picture and Image Boxes 1. Draw one picture box and one image box.

Set the Picture property of the picture and image box to the same file. If you have graphics files installed with Visual Basic, bitmap files can be found in the bitmaps folder, icon files in the icons folder, and metafiles are in the metafile folder. Notice what happens as you resize the two boxes. Notice the layer effect when you move one box on top of the other. Notice the effect of the image box Stretch property.

Play around with different file types - what differences do you see? It displays the available drives in a drop-down combo box. No code is needed to load a drive list box; Visual Basic does this for us. We use the box to get the current drive identification. The directory structure is displayed in a list box. Like, the drive list box, little coding is needed to use the directory list box - Visual Basic does most of the work for us. You may select the types of files you want to display in the file list box.

Path Contains the current path directory. Pattern Contains a string that determines which files will be displayed. PathChange Triggered whenever the path changes in a file list box. As such, it is important that their operation be synchronized to insure the displayed information is always consistent.

For example, if the drive box is named drvExample and the directory box is dirExample, use the code: dirExample. With a file box named filExample, this code is: filExample. This string concatenates the drive, directory, and file name information.

This should be an easy task, except for one problem. If you are at the root directory of your drive, the path name ends with a backslash. If you are not at the root directory, there is no backslash at the end of the path name and you have to add one before tacking on the file name.

FileName End If imgExample. The drive and directory box properties are only used to create changes in the file list box via code. In this application, we search our computer's file structure for graphics files and display the results of our search in an image box.

Once a file is selected, print the corresponding file name on the form and display the graphic file in an image box using the LoadPicture function. Place a drive list box, directory list box, file list box, four label boxes, a line use the line tool and a command button on the form.

We also want to add an image box, but make it look like it's in some kind of frame. Build this display area in these steps: draw a 'large shape', draw another shape within this first shape that is the size of the image display area, and lastly, draw an image box right on top of this last shape.

Since the two shapes and image box are in the same display layer, the image box is on top of the second shape which is on top of the first shape, providing the desired effect of a kind of picture frame. The form should look like this: Line1 Shape1 Shape2 Image1 Note the second shape is directly beneath the image box.

Set properties of the form and each object. Drive End Sub When a new drive is selected, this code forces the directory list box to display directories on that drive. Path End Sub Likewise, when a new directory is chosen, we want to see the files on that directory. It then displays the complete name and loads the picture into the image box. The code is identical because we want to display the image either by double-clicking on the filename or clicking the command button once a file is selected.

Run and try the application. Find bitmaps, icons, and metafiles. Notice how the image box Stretch property affects the different graphics file types. Two common file access routines in Windows-based applications are the Open File and Save File operations. Such boxes are familiar to any Windows user and give your application a professional look.

And, with Windows 95, some context -sensitive help is available while the box is displayed. Appendix II lists many symbolic constants used with common dialog boxes. In normal setup configurations, Visual Basic does this automatically. If the common dialog box does not appear in the Visual Basic toolbox, you need to add it. This is done by selecting Components under the Project menu. You cannot control where the common dialog box appears on your screen. ShowOpen Control to the program returns to the line immediately following this line, once the dialog box is closed in some manner.

Common dialog boxes are system modal. The box is displayed by using the ShowOpen method. Allows you to use error-handling procedures to recognize that Cancel was clicked. DialogTitle The string appearing in the title bar of the dialog box. Default is Open. In the example, the DialogTitle is Open Example. FileName Sets the initial file name that appears in the File name box.

After the dialog box is closed, this property can be read to determine the name of the selected file. Complete filter specifications for forming a Filter can be found using on-line help. FilterIndex Indicates which filter component is default. The example uses a 1 for the FilterIndex the default value. The example uses no Flags value. Place a common dialog control, a label box, and a command button on the form.

ShowOpen lblExample. Save the application. Run it and try selecting file names and typing file names. Notice names can be selected by highlighting and clicking the OK button or just by double-clicking the file name. In this example, clicking the Cancel button is not trapped, so it has the same effect as clicking OK.

Notice once you select a file name, the next time you open the dialog box, that selected name appears as default, since the FileName property is not affected in code. The box is displayed by using the ShowSave method..

DefaultExt Sets the default extension of a file name if a file is listed without an extension. Default is Save As. Filter Used to restrict the filenames that appear in the file list box. Flags Values that control special features of the dialog box see Appendix II. If a file is being saved for the first time, the Save As configuration, with some default name in the FileName property, is used. In the Save configuration, we assume a file has been previously opened with some name.

Hence, when saving the file again, that same name should appear in the FileName property. Be especially aware of whether the user changed the file extension to something your application does not allow. Save the application and run it. Try typing names without extensions and note how. Notice you can also select file names by double- clicking them or using the OK button.

Again, the Cancel button is not trapped, so it has the same effect as clicking OK. Student Name 2. Student Grade 1 through 6 3.

Student Sex Male or Female 4. Student Picture Assume they can be loaded as bitmap files Set up the screen so that only the Name needs to be typed; all other inputs should be set with option buttons, scroll bars, and common dialog boxes. ShowOpen imgStudent. Value lblDay. Value lblYear. Thus far, to run any of the applications studied, we needed Visual Basic. In this class, we learn the steps of developing a stand-alone application that can be run on any Windows-based machine.

What are the inputs and outputs? Develop a framework or flow chart of all your application's processes. Do the built-in Visual Basic tools and functions meet your needs? Do you need to develop some tools or functions of your own?

What do you want your form to look like? Consider appearance and ease of use. Make the interface consistent with other Windows applications. Familarity is good in program design. Make your code readable and traceable - future code modifiers will thank you.

Consider developing reusable code - modules with utility outside your current development. This will save you time in future developments. It's fairly easy to write an application that works properly when the user does everything correctly. It's difficult to write an application that can handle all the possible wrong things a user can do and still not bomb out. There's nothing worse than having a user call you to point out flaws in your application.

A good way to find all the bugs is to let several people try the code - a mini beta-testing program. Most applications have tasks not related to objects that require some code to perform these tasks. Such tasks are usually coded in a general Sub procedure essentially the same as a subroutine in other languages.

This helps meet the above stated goals of readability and reusability. End Sub The definition header names the Sub procedure and defines any arguments passed to the procedure. If there are arguments, they must be declared and typed in the definition header in this form: Var1 As Type1, Var2 As Type2, It then outputs an amount in Mexican pesos MexPesos. You can also use these to call event-driven procedures.

Method 1: Call GenlSubProc Arguments if there are no Arguments, do not type the parentheses Method 2: GenlSubProc Arguments I prefer Method 1 - it's more consistent with calling protocols in other languages and it cleanly delineates the argument list. It seems most Visual Basic programmers use Method 2, though. I guess they hate typing parentheses!

Choose the method you feel more comfortable with. Place the procedure in the form if it has a purpose specifically related to the form. Place it in a module if it is a general purpose procedure that might be used by another form or module or another application.

Whether placing the procedure in a form or module, the methods of creating the procedure are the same. Select or open the form or module's code window. Make sure the window's Object list says General and the Procedure list says Declarations. A window appears allowing you to select Type Sub and enter a name for your procedure. Another way to create a Sub is to go to the last line of the General Declarations section, type Sub followed by a space and the name of your procedure.

Then, hit Enter. With either method for establishing a Sub, Visual Basic will form a template for your procedure. Microsoft visual basic Step by Step. Microsoft Visual Basic 6. Learn Programming Now! Microsoft Visual Basic Express Edition. Microsoft Visual Basic Step by step..

Mastrang Microsoft Visual Basic Quick Introduction to Microsoft Visual Basic. More with book covers. Hide Intellectual property is reserved to the author of the aforementioned book If there is a problem with the book, please report through one of the following links: Report the book or by facebook page The book is published by the Noor-book team Contact us. Share the book :. Ammar Abdul karim Sahib Mubarak. More books like this book.



0コメント

  • 1000 / 1000